Skip to content

Commit 9e77c59

Browse files
authored
Merge pull request #88 from bhyun-kim/main
[bhyun-kim, 김병현] Week 4 solutions
2 parents 8d0f2c5 + da16bab commit 9e77c59

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

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

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+

missing-number/bhyun-kim.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
268. Missing Number
3+
https://leetcode.com/problems/missing-number/description/
4+
5+
Solution:
6+
- Sort the input list
7+
- For each index in the input list:
8+
- If the index is not equal to the element:
9+
- Return the index
10+
- Return the length of the input list
11+
12+
Time complexity: O(nlogn+n)
13+
- The sort function runs O(nlogn) times
14+
- The for loop runs n times
15+
16+
Space complexity: O(1)
17+
- No extra space is used
18+
"""
19+
from typing import List
20+
21+
22+
class Solution:
23+
def missingNumber(self, nums: List[int]) -> int:
24+
nums.sort()
25+
for i in range(len(nums)):
26+
if i != nums[i]:
27+
return i
28+
29+
return len(nums)

number-of-1-bits/bhyun-kim.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
191. Number of 1 Bits
3+
https://leetcode.com/problems/number-of-1-bits/
4+
5+
Solution:
6+
- Create a counter
7+
- For each bit in the input number:
8+
- If the bit is 1:
9+
- Increment the counter
10+
- Return the counter
11+
12+
Time complexity: O(1)
13+
- The while loop runs 32 times
14+
15+
Space complexity: O(1)
16+
- No extra space is used
17+
"""
18+
19+
20+
class Solution:
21+
def hammingWeight(self, n: int) -> int:
22+
output = 0
23+
24+
i = 1 << 31
25+
while i > 0:
26+
if (n & i) != 0:
27+
output += 1
28+
29+
i = i >> 1
30+
31+
return output

reverse-bits/bhyun-kim.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
190. Reverse Bits
3+
https://leetcode.com/problems/reverse-bits/description/
4+
5+
Solution:
6+
- Convert the number to binary string
7+
- Reverse the binary string
8+
- Convert the reversed binary string to integer
9+
- Return the integer
10+
11+
Time complexity: O(1)
12+
- The bin function runs once
13+
- The reversed function runs once
14+
- The int function runs once
15+
16+
Space complexity: O(1)
17+
- No extra space is used
18+
19+
"""
20+
21+
22+
class Solution:
23+
def reverseBits(self, n: int) -> int:
24+
n_bin = bin(n)[2:].zfill(32)
25+
n_bin = "".join(reversed(n_bin))
26+
return int(n_bin, base=2)

0 commit comments

Comments
 (0)