-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
29 changed files
with
772 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def countBits(self, n: int) -> List[int]: | ||
counter = [0] | ||
|
||
for i in range(1, n + 1): | ||
counter.append(counter[i >> 1] + i % 2) | ||
|
||
return counter | ||
|
||
## TC: O(n), SC: O(n) | ||
## this question should be under math section tbh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// 338. Counting Bits | ||
// https://leetcode.com/problems/counting-bits/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/05/19. | ||
// | ||
|
||
final class Solution { | ||
|
||
// MARK: - Time Complexity: O(n), Space Complexity: O(n) | ||
|
||
func countBits(_ n: Int) -> [Int] { | ||
var array: [Int] = .init(repeating: 0, count: n + 1) | ||
for i in stride(from: 1, through: n, by: 1) { | ||
array[i] = array[i >> 1] + (i & 1) | ||
} | ||
return array | ||
} | ||
|
||
// MARK: - nonzeroBitCount | ||
|
||
func countBits2(_ n: Int) -> [Int] { | ||
return (0...n).map(\.nonzeroBitCount) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
338. Counting Bits | ||
https://leetcode.com/problems/counting-bits/description/ | ||
Solution 1: | ||
- Convert the number to binary string | ||
- Sum the number of 1s in the binary string | ||
- Append the sum to the output list | ||
- Return the output list | ||
Time complexity: O(nlogn) | ||
- The for loop runs n times | ||
- The sum function runs O(logn) times | ||
Space complexity: O(n) | ||
- The output list has n elements | ||
class Solution: | ||
def countBits(self, n: int) -> List[int]: | ||
output = [] | ||
for i in range(n + 1): | ||
_str = str(bin(i))[2:] | ||
_sum = sum(map(int, _str.strip())) | ||
output.append(_sum) | ||
return output | ||
""" | ||
|
||
""" | ||
Solution 2 | ||
We can solve this problem with dynamic programming. | ||
1. Initialize output with n elements | ||
2. The first element is 0 because iteration starts from zero. | ||
3. Iterate from 1 to n+1 | ||
4. The last digit of each number is 0 for even number 1 for odd number | ||
So add (i & 1) to the output | ||
5. The digits except the last one can be found when the number is divided by two. | ||
Instead for division by two, we can use one step of bit shift to the right. | ||
0 = 00000 | ||
1 = 00001 | ||
2 = 00010 | ||
3 = 00011 | ||
4 = 00100 | ||
5 = 00101 | ||
6 = 00110 | ||
7 = 00111 | ||
Time complexity: O(n) | ||
- The for loop runs n times | ||
Space complexity: O(n) | ||
- The output list has n elements | ||
""" | ||
|
||
from typing import List | ||
|
||
class Solution: | ||
def countBits(self, n: int) -> List[int]: | ||
output = [0] * (n+1) | ||
|
||
for i in range(1, n+1): | ||
output[i] = output[i >> 1] + (i & 1) | ||
|
||
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Intuition | ||
<!-- Describe your first thoughts on how to solve this problem. --> | ||
์ด์ ๊ฐ๋ค์ ์ฌํ์ฉํ๋ค. | ||
# Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
1. ์ฃ์ง ์ผ์ด์ค๋ 0์ ๋ฐํํ๋ค. | ||
2. 0, 1์ ๋ฏธ๋ฆฌ ๊ณ์ฐํ๋ค. | ||
3. `>>`์ ์ํํ ๊ฒฐ๊ณผ + ์ง/ํ ์ฌ๋ถ๋ก ์ธํ 1์ ๋ํด์ ํด๊ฒฐํด์ค๋ค. | ||
- ์ด์ง์ `1001`์ ๊ฒฝ์ฐ `100` ๊ณ์ฐํ ๊ฒฐ๊ด๊ฐ์์ `1`์ ๋ํด์ฃผ๋ฉด ๋๋ค. | ||
- ์ด์ง์ `1010`์ ๊ฒฝ์ฐ `101` ๊ณ์ฐํ ๊ฒฐ๊ด๊ฐ์์ `0`์ ๋ํด์ฃผ๋ฉด ๋๋ค. | ||
|
||
- ์๋ฃจ์ ์ฐธ๊ณ : `i & (i-1)` ์ฐ์ฐ์ ํตํด ๊ณ์ฐํ๋ค. | ||
- 2์ ์ ๊ณฑ์์ธ ๊ฒฝ์ฐ `0`์ด ๋์ 1์ ๋ํ๋ฉด ๋๋ค. | ||
- ์๋ ๊ฒฝ์ฐ๋ ์์ง์ ์ ๋ชจ๋ฅด๊ฒ ๋ค. | ||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
<!-- Add your time complexity here, e.g. $$O(n)$$ --> | ||
:`n`ํฌ๊ธฐ์ ๋ฐฐ์ด์ ๋ชจ๋๋ฅผ ์ํํ๋ค. | ||
- Space complexity: $$O(n)$$ | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
:ํฌ๊ธฐ `n`์ ๋ฐฐ์ด์ ์ ์ธํ๋ค. | ||
# Code | ||
```go | ||
func countBits(n int) []int { | ||
if n == 0 { | ||
return []int{0} | ||
} | ||
ans := make([]int, n+1, n+1) | ||
|
||
ans[0], ans[1] = 0, 1 | ||
|
||
for i := 2; i <= n; i++ { | ||
ans[i] = ans[i>>1] + i&1 | ||
} | ||
return ans | ||
} | ||
|
||
func countBitsSolution(n int) []int { | ||
res := make([]int, n+1) | ||
for i := 1; i <= n; i++ { | ||
res[i] = res[i&(i-1)] + 1 | ||
} | ||
return res | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var countBits = function (n) { | ||
// Create array which has 0 element length of n | ||
const dp = new Array(n + 1).fill(0); | ||
let offset = 1; | ||
|
||
for (let i = 1; i <= n; i++) { | ||
if (offset * 2 === i) offset = i; | ||
dp[i] = 1 + dp[i - offset]; | ||
} | ||
return dp; | ||
}; | ||
|
||
/** | ||
0 -> 0000 -> dp[0] = 0 | ||
1 -> 0001 -> dp[1] = 1 + dp[1-1] = 1 | ||
2 -> 0010 -> dp[2] = 1 + dp[2-2] = 1 | ||
3 -> 0011 -> dp[3] = 1 + dp[3-2] = 2 | ||
4 -> 0100 -> dp[4] = 1 + dp[4-4] = 1 | ||
5 -> 0101 -> dp[5] = 1 + dp[5-4] = 2 | ||
6 -> 0110 -> dp[6] = 1 + dp[6-4] = 2 | ||
7 -> 0111 -> dp[7] = 1 + dp[7-4] = 3 | ||
8 -> 1000 -> dp[8] = 1 + dp[8-8] = 1 | ||
*/ | ||
|
||
// TC: O(n) | ||
// SC: O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Time Complexity: O(n) | ||
// Space Complexity: O(n) | ||
|
||
var countBits = function(n) { | ||
// initialize an array to hold the result. | ||
let ans = new Array(n + 1).fill(0); | ||
|
||
// iterate through all numbers from 1 to n. | ||
for (let i = 1; i <= n; i++) { | ||
ans[i] = ans[i >> 1] + (i & 1); | ||
} | ||
return ans; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
ans = collections.defaultdict(list) | ||
|
||
for s in strs: | ||
ans[str(sorted(s))].append(s) | ||
|
||
return list(ans.values()) | ||
|
||
## TC: O(n * klogk), SC: O(n * k), where n is len(strs) and k is len(longest_s) | ||
|
||
# ans = {} | ||
|
||
# for s in strs: | ||
# sorted_s = ''.join(sorted(s)) | ||
|
||
# if sorted_s not in ans: | ||
# ans[sorted_s] = [] | ||
|
||
# ans[sorted_s].append(s) | ||
|
||
# return list(ans.values()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// 49. Group Anagrams | ||
// https://leetcode.com/problems/group-anagrams/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/05/19. | ||
// | ||
|
||
final class Solution { | ||
func groupAnagrams(_ strs: [String]) -> [[String]] { | ||
var dictionary: [String: [String]] = [:] | ||
for str in strs { | ||
dictionary[String(str.sorted()), default: []].append(str) | ||
} | ||
|
||
return Array(dictionary.values) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
49. Group Anagrams | ||
https://leetcode.com/problems/group-anagrams/description/ | ||
Solution: | ||
- Create a hash table and a list of counters | ||
- For each string in the input list: | ||
- Sort the string | ||
- If the sorted string is in the hash table: | ||
- Append the string to the corresponding counter list | ||
- Else: | ||
- Add the sorted string to the hash table | ||
- Create a new counter list and append the string | ||
- Return the list of counters | ||
Time complexity: O(nmlogm) | ||
- The for loop runs n times | ||
- The sorted function runs O(mlogm) times | ||
- m is the length of the longest string in the input list | ||
Space complexity: O(n) | ||
- The output list has n elements | ||
- The hash table has n elements | ||
- The list of counters has n elements | ||
""" | ||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
|
||
hash_table = dict() | ||
output = [] | ||
|
||
for s in strs: | ||
count_s = ''.join(sorted(s)) | ||
if count_s in hash_table: | ||
idx = hash_table[count_s] | ||
output[idx].append(s) | ||
else: | ||
hash_table[count_s] = len(output) | ||
output.append([s]) | ||
|
||
return output | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Intuition | ||
์ ๋ ฌ๋ ๋ฌธ์์ด์ ํตํด ๊ทธ๋ฃน ์ฌ๋ถ๋ฅผ ์ฝ๊ฒ ํ์ธํ๋ค. | ||
# Approach | ||
1. `{์ ๋ ฌ๋ ๋ฌธ์์ด, ๊ทธ ๋ฌธ์์ด์ ์ธ๋ฑ์ค}`๋ฅผ ์ ์งํ๋ ๋ฐฐ์ด์ ์ ์ธํ๋ค. | ||
2. ์๋ณธ ๋ฐฐ์ด์ ์ํํ๋ฉฐ ์ ๋ ฌํ ๊ฒฐ๊ณผ, ์ธ๋ฑ์ค๋ฅผ struct๋ก ํ์ฌ ๋ฐฐ์ด์ ์ฝ์ ํ๋ค. | ||
- ์ธ๋ฑ์ค๋ฅผ ์ ์งํ๋ ์ด์ ๋ ์๋ณธ ๋ฐฐ์ด์ ๋ฌธ์์ด ๊ฐ์ ํ์ธํ๊ธฐ ์ํจ์ด๋ค. (์ ๋ ฌ์ ํ๊ธฐ์ ์ด๋ ์ง ์์ผ๋ฉด ํ์ธ์ด ์ด๋ ต๋ค.) | ||
3. ๋ชจ๋ struct๊ฐ ์ฝ์ ๋ ๋ฐฐ์ด์ ๋ฌธ์์ด์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ค. | ||
4. ๋ฐ๋ณต๋ฌธ์ ์ํํ๋ฉฐ `์ด์ ๋ฌธ์์ด๊ณผ ๊ฐ์์ง` ์ฌ๋ถ๋ฅผ ๊ทธ๋ฃน์ ํ๋จํ์ฌ ์ธ๋ฑ์ค๋ค์ ๊ทธ๋ฃจํํด ๋ชจ์๋๋๋ค. | ||
5. ๊ทธ๋ฃจํํ ์ธ๋ฑ์ค๋ค์ ๋ฌธ์์ด(์๋ณธ ๋ฌธ์์ด์ ์ฐธ์กฐํด์)๋ก ์นํํ์ฌ ์ต์ข ์ ์ผ๋ก ๋ฐํํ๋ค. | ||
# Complexity | ||
- Time complexity: $$O(nklog(n))$$ | ||
<!-- Add your time complexity here, e.g. $$O(nklog(n))$$ --> | ||
: ๋ฐฐ์ด์ ๊ธธ์ด `n`, ๋ฌธ์์ด์ ๊ธธ์ด `k`. ๋ฌธ์์ด์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ๋ ๋ฐ์ํ๋ ๋น์ฉ์ด๋ค. | ||
- Space complexity: $$O(nk)$$ | ||
|
||
: ์ฃผ์ด์ง ๋ฐฐ์ด์ ๊ธธ์ด `n`, ๋ฐฐ์ด์ด ๊ฐ์ง๋ ๋ฌธ์์ด์ ๊ธธ์ด `k`, (์ฝ๋๋ก ์์ฑํ๋ ๋ฐฐ์ด์ ๊ธธ์ด, ๋ฌธ์์ด์ ํฌ๊ธฐ ๋ชจ๋ `n`, `k`์ด๋ค.) | ||
<!-- Add your space complexity here, e.g. $$O(n)$$ --> | ||
|
||
# Code | ||
```go | ||
type StrAndIdx struct { | ||
Str string | ||
Idx int | ||
} | ||
|
||
func sortSring(s string) string { | ||
runes := []rune(s) | ||
|
||
sort.Slice(runes, func(i, j int) bool { | ||
return runes[i] < runes[j] | ||
}) | ||
|
||
return string(runes) | ||
} | ||
|
||
func groupAnagrams(strs []string) [][]string { | ||
|
||
strAndIdxs := make([]StrAndIdx, 0, len(strs)+5) | ||
|
||
for i, s := range strs { | ||
strAndIdxs = append(strAndIdxs, StrAndIdx{sortSring(s), i}) | ||
} | ||
|
||
sort.Slice(strAndIdxs, func(i, j int) bool { | ||
return strAndIdxs[i].Str < strAndIdxs[j].Str | ||
}) | ||
|
||
groupedIdxs := make([][]int, 0, len(strAndIdxs)/4) | ||
|
||
group := make([]int, 0) | ||
defaultString := "NOT_EXIST_STRING" | ||
prev := defaultString | ||
for _, sAI := range strAndIdxs { | ||
curr := sAI.Str | ||
idx := sAI.Idx | ||
if prev == curr { | ||
group = append(group, idx) | ||
} else { | ||
if prev != defaultString { | ||
groupedIdxs = append(groupedIdxs, group) | ||
} | ||
group = []int{idx} | ||
} | ||
prev = curr | ||
} | ||
|
||
if len(group) != 0 { | ||
groupedIdxs = append(groupedIdxs, group) | ||
} | ||
|
||
groupedStrs := make([][]string, 0, len(groupedIdxs)) | ||
for _, idxs := range groupedIdxs { | ||
|
||
groupStr := make([]string, 0, len(idxs)) | ||
for _, idx := range idxs { | ||
groupStr = append(groupStr, strs[idx]) | ||
} | ||
groupedStrs = append(groupedStrs, groupStr) | ||
} | ||
|
||
return groupedStrs | ||
} | ||
``` | ||
|
||
# **To** Learn | ||
- GoLang์์ ๋ฌธ์์ด์ ์ด๋ป๊ฒ ๋น๊ตํ๋์ง. | ||
- ํด๋น ๋ฌธ์ ํด๊ฒฐ์ ์์ด์ ์ฝ์งํ ๊ฒ์ ๋ฌด์์ด์๋์ง. ์์ผ๋ก ์ด๋ป๊ฒ ํด์ผ ์ํ ์ ์๋์ง. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var groupAnagrams = function (strs) { | ||
// Declare hash map to store sorted strs | ||
let map = new Map(); | ||
|
||
for (let str of strs) { | ||
// Sorted each str | ||
const sortedStr = str.split("").sort().join(""); | ||
|
||
// If there is alread sortedStr on the map, pushed str | ||
if (map.has(sortedStr)) { | ||
map.get(sortedStr).push(str); | ||
} else { | ||
// If there is no sortedStr on the map, insert [str] | ||
map.set(sortedStr, [str]); | ||
} | ||
} | ||
return Array.from(map.values()); | ||
}; | ||
|
||
// TC: O(n*klogk) | ||
// SC: O(n*k) | ||
// n -> length of strs array | ||
// k -> amount of character for each element |
Oops, something went wrong.