Skip to content

Commit e0508f0

Browse files
Merge branch 'DaleStudy:main' into main
2 parents 137a017 + 99e7ce5 commit e0508f0

29 files changed

+772
-0
lines changed

โ€Žcounting-bits/Leo.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countBits(self, n: int) -> List[int]:
3+
counter = [0]
4+
5+
for i in range(1, n + 1):
6+
counter.append(counter[i >> 1] + i % 2)
7+
8+
return counter
9+
10+
## TC: O(n), SC: O(n)
11+
## this question should be under math section tbh

โ€Žcounting-bits/WhiteHyun.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// 338. Counting Bits
3+
// https://leetcode.com/problems/counting-bits/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/19.
7+
//
8+
9+
final class Solution {
10+
11+
// MARK: - Time Complexity: O(n), Space Complexity: O(n)
12+
13+
func countBits(_ n: Int) -> [Int] {
14+
var array: [Int] = .init(repeating: 0, count: n + 1)
15+
for i in stride(from: 1, through: n, by: 1) {
16+
array[i] = array[i >> 1] + (i & 1)
17+
}
18+
return array
19+
}
20+
21+
// MARK: - nonzeroBitCount
22+
23+
func countBits2(_ n: Int) -> [Int] {
24+
return (0...n).map(\.nonzeroBitCount)
25+
}
26+
27+
}

โ€Žcounting-bits/bhyun-kim.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
338. Counting Bits
3+
https://leetcode.com/problems/counting-bits/description/
4+
5+
Solution 1:
6+
- Convert the number to binary string
7+
- Sum the number of 1s in the binary string
8+
- Append the sum to the output list
9+
- Return the output list
10+
11+
Time complexity: O(nlogn)
12+
- The for loop runs n times
13+
- The sum function runs O(logn) times
14+
15+
Space complexity: O(n)
16+
- The output list has n elements
17+
18+
class Solution:
19+
def countBits(self, n: int) -> List[int]:
20+
output = []
21+
for i in range(n + 1):
22+
_str = str(bin(i))[2:]
23+
_sum = sum(map(int, _str.strip()))
24+
output.append(_sum)
25+
26+
return output
27+
"""
28+
29+
"""
30+
Solution 2
31+
We can solve this problem with dynamic programming.
32+
1. Initialize output with n elements
33+
2. The first element is 0 because iteration starts from zero.
34+
3. Iterate from 1 to n+1
35+
4. The last digit of each number is 0 for even number 1 for odd number
36+
So add (i & 1) to the output
37+
5. The digits except the last one can be found when the number is divided by two.
38+
Instead for division by two, we can use one step of bit shift to the right.
39+
40+
0 = 00000
41+
1 = 00001
42+
2 = 00010
43+
3 = 00011
44+
4 = 00100
45+
5 = 00101
46+
6 = 00110
47+
7 = 00111
48+
49+
Time complexity: O(n)
50+
- The for loop runs n times
51+
52+
Space complexity: O(n)
53+
- The output list has n elements
54+
"""
55+
56+
from typing import List
57+
58+
class Solution:
59+
def countBits(self, n: int) -> List[int]:
60+
output = [0] * (n+1)
61+
62+
for i in range(1, n+1):
63+
output[i] = output[i >> 1] + (i & 1)
64+
65+
return output

โ€Žcounting-bits/invidam.go.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
์ด์ „ ๊ฐ’๋“ค์„ ์žฌํ™œ์šฉํ•œ๋‹ค.
4+
# Approach
5+
<!-- Describe your approach to solving the problem. -->
6+
1. ์—ฃ์ง€ ์ผ€์ด์Šค๋Š” 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
7+
2. 0, 1์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•œ๋‹ค.
8+
3. `>>`์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ + ์ง/ํ™€ ์—ฌ๋ถ€๋กœ ์ธํ•œ 1์„ ๋”ํ•ด์„œ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.
9+
- ์ด์ง„์ˆ˜ `1001`์˜ ๊ฒฝ์šฐ `100` ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ด๊ฐ’์—์„œ `1`์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
10+
- ์ด์ง„์ˆ˜ `1010`์˜ ๊ฒฝ์šฐ `101` ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ด๊ฐ’์—์„œ `0`์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
11+
12+
- ์†”๋ฃจ์…˜ ์ฐธ๊ณ : `i & (i-1)` ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ณ„์‚ฐํ•œ๋‹ค.
13+
- 2์˜ ์ œ๊ณฑ์ˆ˜์ธ ๊ฒฝ์šฐ `0`์ด ๋‚˜์™€ 1์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.
14+
- ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” ์•„์ง์€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.
15+
# Complexity
16+
- Time complexity: $$O(n)$$
17+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
18+
:`n`ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋ชจ๋‘๋ฅผ ์ˆœํšŒํ•œ๋‹ค.
19+
- Space complexity: $$O(n)$$
20+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
21+
:ํฌ๊ธฐ `n`์˜ ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
22+
# Code
23+
```go
24+
func countBits(n int) []int {
25+
if n == 0 {
26+
return []int{0}
27+
}
28+
ans := make([]int, n+1, n+1)
29+
30+
ans[0], ans[1] = 0, 1
31+
32+
for i := 2; i <= n; i++ {
33+
ans[i] = ans[i>>1] + i&1
34+
}
35+
return ans
36+
}
37+
38+
func countBitsSolution(n int) []int {
39+
res := make([]int, n+1)
40+
for i := 1; i <= n; i++ {
41+
res[i] = res[i&(i-1)] + 1
42+
}
43+
return res
44+
}
45+
```

โ€Žcounting-bits/nhistory.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var countBits = function (n) {
2+
// Create array which has 0 element length of n
3+
const dp = new Array(n + 1).fill(0);
4+
let offset = 1;
5+
6+
for (let i = 1; i <= n; i++) {
7+
if (offset * 2 === i) offset = i;
8+
dp[i] = 1 + dp[i - offset];
9+
}
10+
return dp;
11+
};
12+
13+
/**
14+
0 -> 0000 -> dp[0] = 0
15+
1 -> 0001 -> dp[1] = 1 + dp[1-1] = 1
16+
2 -> 0010 -> dp[2] = 1 + dp[2-2] = 1
17+
3 -> 0011 -> dp[3] = 1 + dp[3-2] = 2
18+
4 -> 0100 -> dp[4] = 1 + dp[4-4] = 1
19+
5 -> 0101 -> dp[5] = 1 + dp[5-4] = 2
20+
6 -> 0110 -> dp[6] = 1 + dp[6-4] = 2
21+
7 -> 0111 -> dp[7] = 1 + dp[7-4] = 3
22+
8 -> 1000 -> dp[8] = 1 + dp[8-8] = 1
23+
*/
24+
25+
// TC: O(n)
26+
// SC: O(1)

โ€Žcounting-bits/yolophg.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var countBits = function(n) {
5+
// initialize an array to hold the result.
6+
let ans = new Array(n + 1).fill(0);
7+
8+
// iterate through all numbers from 1 to n.
9+
for (let i = 1; i <= n; i++) {
10+
ans[i] = ans[i >> 1] + (i & 1);
11+
}
12+
return ans;
13+
};

โ€Žgroup-anagrams/Leo.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
ans = collections.defaultdict(list)
4+
5+
for s in strs:
6+
ans[str(sorted(s))].append(s)
7+
8+
return list(ans.values())
9+
10+
## TC: O(n * klogk), SC: O(n * k), where n is len(strs) and k is len(longest_s)
11+
12+
# ans = {}
13+
14+
# for s in strs:
15+
# sorted_s = ''.join(sorted(s))
16+
17+
# if sorted_s not in ans:
18+
# ans[sorted_s] = []
19+
20+
# ans[sorted_s].append(s)
21+
22+
# return list(ans.values())

โ€Žgroup-anagrams/WhiteHyun.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// 49. Group Anagrams
3+
// https://leetcode.com/problems/group-anagrams/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/19.
7+
//
8+
9+
final class Solution {
10+
func groupAnagrams(_ strs: [String]) -> [[String]] {
11+
var dictionary: [String: [String]] = [:]
12+
for str in strs {
13+
dictionary[String(str.sorted()), default: []].append(str)
14+
}
15+
16+
return Array(dictionary.values)
17+
}
18+
}

โ€Žgroup-anagrams/bhyun-kim.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
49. Group Anagrams
3+
https://leetcode.com/problems/group-anagrams/description/
4+
5+
Solution:
6+
- Create a hash table and a list of counters
7+
- For each string in the input list:
8+
- Sort the string
9+
- If the sorted string is in the hash table:
10+
- Append the string to the corresponding counter list
11+
- Else:
12+
- Add the sorted string to the hash table
13+
- Create a new counter list and append the string
14+
- Return the list of counters
15+
16+
Time complexity: O(nmlogm)
17+
- The for loop runs n times
18+
- The sorted function runs O(mlogm) times
19+
- m is the length of the longest string in the input list
20+
21+
Space complexity: O(n)
22+
- The output list has n elements
23+
- The hash table has n elements
24+
- The list of counters has n elements
25+
"""
26+
27+
from typing import List
28+
29+
30+
class Solution:
31+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
32+
33+
hash_table = dict()
34+
output = []
35+
36+
for s in strs:
37+
count_s = ''.join(sorted(s))
38+
if count_s in hash_table:
39+
idx = hash_table[count_s]
40+
output[idx].append(s)
41+
else:
42+
hash_table[count_s] = len(output)
43+
output.append([s])
44+
45+
return output
46+

โ€Žgroup-anagrams/invidam.go.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Intuition
2+
์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ๊ทธ๋ฃน ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ํ™•์ธํ•œ๋‹ค.
3+
# Approach
4+
1. `{์ •๋ ฌ๋œ ๋ฌธ์ž์—ด, ๊ทธ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค}`๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
5+
2. ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ, ์ธ๋ฑ์Šค๋ฅผ struct๋กœ ํ•˜์—ฌ ๋ฐฐ์—ด์— ์‚ฝ์ž…ํ•œ๋‹ค.
6+
- ์ธ๋ฑ์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ์ด์œ ๋Š” ์›๋ณธ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. (์ •๋ ฌ์„ ํ–ˆ๊ธฐ์— ์ด๋ ‡์ง€ ์•Š์œผ๋ฉด ํ™•์ธ์ด ์–ด๋ ต๋‹ค.)
7+
3. ๋ชจ๋“  struct๊ฐ€ ์‚ฝ์ž…๋œ ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
8+
4. ๋ฐ˜๋ณต๋ฌธ์„ ์ˆœํšŒํ•˜๋ฉฐ `์ด์ „ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€์ง€` ์—ฌ๋ถ€๋ฅผ ๊ทธ๋ฃน์„ ํŒ๋‹จํ•˜์—ฌ ์ธ๋ฑ์Šค๋“ค์„ ๊ทธ๋ฃจํ•‘ํ•ด ๋ชจ์•„๋†“๋Š”๋‹ค.
9+
5. ๊ทธ๋ฃจํ•‘ํ•œ ์ธ๋ฑ์Šค๋“ค์„ ๋ฌธ์ž์—ด(์›๋ณธ ๋ฌธ์ž์—ด์„ ์ฐธ์กฐํ•ด์„œ)๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
10+
# Complexity
11+
- Time complexity: $$O(nklog(n))$$
12+
<!-- Add your time complexity here, e.g. $$O(nklog(n))$$ -->
13+
: ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`. ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ์ด๋‹ค.
14+
- Space complexity: $$O(nk)$$
15+
16+
: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฐฐ์—ด์ด ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`, (์ฝ”๋“œ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด, ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ ๋ชจ๋‘ `n`, `k`์ด๋‹ค.)
17+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
18+
19+
# Code
20+
```go
21+
type StrAndIdx struct {
22+
Str string
23+
Idx int
24+
}
25+
26+
func sortSring(s string) string {
27+
runes := []rune(s)
28+
29+
sort.Slice(runes, func(i, j int) bool {
30+
return runes[i] < runes[j]
31+
})
32+
33+
return string(runes)
34+
}
35+
36+
func groupAnagrams(strs []string) [][]string {
37+
38+
strAndIdxs := make([]StrAndIdx, 0, len(strs)+5)
39+
40+
for i, s := range strs {
41+
strAndIdxs = append(strAndIdxs, StrAndIdx{sortSring(s), i})
42+
}
43+
44+
sort.Slice(strAndIdxs, func(i, j int) bool {
45+
return strAndIdxs[i].Str < strAndIdxs[j].Str
46+
})
47+
48+
groupedIdxs := make([][]int, 0, len(strAndIdxs)/4)
49+
50+
group := make([]int, 0)
51+
defaultString := "NOT_EXIST_STRING"
52+
prev := defaultString
53+
for _, sAI := range strAndIdxs {
54+
curr := sAI.Str
55+
idx := sAI.Idx
56+
if prev == curr {
57+
group = append(group, idx)
58+
} else {
59+
if prev != defaultString {
60+
groupedIdxs = append(groupedIdxs, group)
61+
}
62+
group = []int{idx}
63+
}
64+
prev = curr
65+
}
66+
67+
if len(group) != 0 {
68+
groupedIdxs = append(groupedIdxs, group)
69+
}
70+
71+
groupedStrs := make([][]string, 0, len(groupedIdxs))
72+
for _, idxs := range groupedIdxs {
73+
74+
groupStr := make([]string, 0, len(idxs))
75+
for _, idx := range idxs {
76+
groupStr = append(groupStr, strs[idx])
77+
}
78+
groupedStrs = append(groupedStrs, groupStr)
79+
}
80+
81+
return groupedStrs
82+
}
83+
```
84+
85+
# **To** Learn
86+
- GoLang์—์„œ ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•˜๋Š”์ง€.
87+
- ํ•ด๋‹น ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์žˆ์–ด์„œ ์‚ฝ์งˆํ•œ ๊ฒƒ์€ ๋ฌด์—‡์ด์—ˆ๋Š”์ง€. ์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ์•ˆํ•  ์ˆ˜ ์žˆ๋Š”์ง€.

0 commit comments

Comments
ย (0)