Skip to content

Commit 8ed3ebe

Browse files
author
changmuk.im
committed
Merge branch 'feat/week2'
2 parents 90e72e8 + 816ec49 commit 8ed3ebe

File tree

8 files changed

+572
-0
lines changed

8 files changed

+572
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from typing import List, Optional
2+
from unittest import TestCase, main
3+
4+
5+
# Definition for a binary tree node.
6+
class TreeNode:
7+
def __init__(self, val=0, left=None, right=None):
8+
self.val = val
9+
self.left = left
10+
self.right = right
11+
12+
13+
class Solution:
14+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
15+
return self.solve_1(preorder, inorder)
16+
17+
"""
18+
Runtime: 112 ms (Beats 66.16%)
19+
Time Complexity: O(n ** 2)
20+
Space Complexity: O(n)
21+
Memory: 52.83 MB (Beats 63.14%)
22+
"""
23+
def solve_1(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
24+
index = 0
25+
26+
def build_tree(preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
27+
nonlocal index
28+
29+
if not inorder:
30+
return None
31+
32+
if not 0 <= index < len(preorder):
33+
return None
34+
35+
root = TreeNode(preorder[index])
36+
index += 1
37+
split_index = inorder.index(root.val)
38+
root.left = build_tree(preorder, inorder[:split_index])
39+
root.right = build_tree(preorder, inorder[split_index + 1:])
40+
41+
return root
42+
43+
return build_tree(preorder, inorder)
44+
45+
46+
class _LeetCodeTestCases(TestCase):
47+
def test_1(self):
48+
preorder = [3, 9, 20, 15, 7]
49+
inorder = [9, 3, 15, 20, 7]
50+
output = TreeNode(
51+
val=3,
52+
left=TreeNode(
53+
val=9
54+
),
55+
right=TreeNode(
56+
val=20,
57+
left=TreeNode(val=15),
58+
right=TreeNode(val=7)
59+
)
60+
)
61+
self.assertEqual(Solution.buildTree(Solution(), preorder, inorder), output)
62+
63+
def test_2(self):
64+
preorder = [-1]
65+
inorder = [-1]
66+
output = TreeNode(
67+
val=-1
68+
)
69+
self.assertEqual(Solution.buildTree(Solution(), preorder, inorder), output)
70+
71+
def test_3(self):
72+
preorder = [1, 2]
73+
inorder = [1, 2]
74+
output = TreeNode(
75+
val=1,
76+
right=TreeNode(
77+
val=2
78+
)
79+
)
80+
self.assertEqual(Solution.buildTree(Solution(), preorder, inorder), output)
81+
82+
83+
if __name__ == '__main__':
84+
main()

counting-bits/EGON.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List
2+
from unittest import TestCase, main
3+
4+
5+
class Solution:
6+
def countBits(self, n: int) -> List[int]:
7+
return self.solve_1(n)
8+
9+
"""
10+
Runtime: 78 ms (Beats 31.22%)
11+
Time Complexity: O(n * log n), 크기가 n인 배열의 원소들에 대해 시행마다 크기가 2로 나누어지는 비트연산을 수행하므로
12+
Space Complexity: O(1), 변수 저장 없이 바로 결과 반환
13+
Memory: 23.26 MB (Beats 39.52%)
14+
"""
15+
def solve_1(self, n: int) -> List[int]:
16+
def count_number_of_1(n: int):
17+
count = 0
18+
while n:
19+
n &= (n - 1)
20+
count += 1
21+
return count
22+
23+
return [count_number_of_1(num) for num in range(n + 1)]
24+
25+
26+
class _LeetCodeTestCases(TestCase):
27+
def test_1(self):
28+
n = 2
29+
output = [0, 1, 1]
30+
self.assertEqual(Solution.countBits(Solution(), n), output)
31+
32+
def test_2(self):
33+
n = 5
34+
output = [0, 1, 1, 2, 1, 2]
35+
self.assertEqual(Solution.countBits(Solution(), n), output)
36+
37+
38+
if __name__ == '__main__':
39+
main()

counting-bits/EGON.swift

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import Foundation
2+
3+
class Solution {
4+
func countBits(_ n: Int) -> [Int] {
5+
return solve_2(n)
6+
}
7+
8+
/*
9+
**내장함수를 사용한 풀이**
10+
11+
Runtime: 37 ms (Beats 49.12%)
12+
Time Complexity: O(n * log n)
13+
- 길이가 n인 range를 순회하므로 O(n)
14+
- 정수에 대해 non zero bit를 세는데 O(log n)
15+
> O(n * log n)
16+
Space Complexity: O(n)
17+
- 배열에 값을 저장하며 크기가 n이 되므로 O(n)
18+
Memory: 20.86 MB (Beats 90.106%)
19+
*/
20+
func solve_1(_ n: Int) -> [Int] {
21+
var result: [Int] = []
22+
for num in 0...n {
23+
result.append(num.nonzeroBitCount)
24+
}
25+
return result
26+
}
27+
28+
/*
29+
** Brian Kernighan's Algorithm을 사용한 풀이 **
30+
31+
Runtime: 39 ms (Beats 42.11%)
32+
Time Complexity: O(n * log n)
33+
- 길이가 n인 range를 순회하므로 O(n)
34+
- bitCountWithBrianKernighan 에서 내부 while문 실행마다 num이 절반으로 줄어드므로, 실행횟수는 O(log n)
35+
> O(n * log n)
36+
Space Complexity: O(n)
37+
- 배열에 값을 저장하며 크기가 n이 되므로 O(n)
38+
Memory: 16.01 MB (Beats 67.84%)
39+
*/
40+
func solve_2(_ n: Int) -> [Int] {
41+
42+
func bitCountWithBrianKernighan(_ num: Int) -> Int {
43+
var num = num
44+
var bitCount = 0
45+
while 0 < num {
46+
num &= (num - 1)
47+
bitCount += 1
48+
}
49+
50+
return bitCount
51+
}
52+
53+
var result: [Int] = []
54+
for num in 0...n {
55+
result.append(bitCountWithBrianKernighan(num))
56+
}
57+
return result
58+
}
59+
60+
/*
61+
** MSB에 대해 DP를 사용한 풀이 **
62+
> num의 비트 카운트 = MSB의 비트 카운트(1 고정) + (num - msb)의 비트 카운트
63+
64+
Runtime: 36 ms (Beats 58.48%)
65+
Time Complexity: O(n)
66+
- 0부터 n까지 range를 순회하므로 O(n)
67+
> O(n)
68+
Space Complexity: O(n)
69+
- 길이가 n인 dp 배열을 저장하므로 O(n)
70+
Memory: 21.15 MB (Beats 67.84%)
71+
*/
72+
func solve_3(_ n: Int) -> [Int] {
73+
guard 1 <= n else { return [0] }
74+
75+
var dp = [Int](repeating: 0, count: n + 1)
76+
var msb = 1
77+
for num in 1...n {
78+
if msb << 1 == num {
79+
msb = num
80+
}
81+
82+
dp[num] = 1 + dp[num - msb]
83+
}
84+
85+
return dp
86+
}
87+
88+
/*
89+
** LSB에 대해 DP를 사용한 풀이 **
90+
> num의 비트 카운트 = num을 right shift한 숫자의 비트 카운트 + LSB의 비트 카운트(1 또는 0)
91+
92+
Runtime: 28 ms (Beats 97.08%)
93+
Time Complexity: O(n)
94+
- 0부터 n까지 range를 순회하므로 O(n)
95+
> O(n)
96+
Space Complexity: O(n)
97+
- 길이가 n인 dp 배열을 저장하므로 O(n)
98+
Memory: 21.26 MB (Beats 53.80%)
99+
*/
100+
func solve_4(_ n: Int) -> [Int] {
101+
guard 1 <= n else { return [0] }
102+
103+
var dp = [Int](repeating: 0, count: n + 1)
104+
for num in 1...n {
105+
dp[num] = dp[num >> 1] + (num & 1)
106+
}
107+
108+
return dp
109+
}
110+
}
111+
112+
let solution = Solution()
113+
print(solution.solve_4(0))

decode-ways/EGON.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from typing import List
2+
from unittest import TestCase, main
3+
4+
5+
class Solution:
6+
def numDecodings(self, s: str) -> int:
7+
return self.solve_1(s)
8+
9+
"""
10+
Runtime: 32 ms (Beats 80.36%)
11+
Time Complexity: O(n)
12+
Space Complexity: O(n)
13+
Memory: 16.59 MB (Beats 51.72%)
14+
"""
15+
def solve_1(self, s: str) -> int:
16+
if len(s) == 0:
17+
return 0
18+
19+
if len(s) == 1:
20+
return 0 if int(s) == 0 else 1
21+
22+
if len(s) == 2:
23+
last_one_digit, last_two_digits = int(s[1]), int(s)
24+
if last_one_digit == 0:
25+
return 1 if 10 <= last_two_digits <= 26 else 0
26+
else:
27+
if 0 <= last_two_digits < 10:
28+
return 0
29+
elif 10 <= last_two_digits <= 26:
30+
return 2
31+
else:
32+
return 1
33+
34+
dp = [0] * (len(s) + 1)
35+
dp[0], dp[1], dp[2] = self.solve_1(s[:0]), self.solve_1(s[:1]), self.solve_1(s[:2])
36+
for i in range(3, len(s) + 1):
37+
last_one_digit, last_two_digits = int(s[i - 1]), int(s[i - 2: i])
38+
last_two_digits = int(s[i - 2: i])
39+
if last_one_digit == 0:
40+
dp[i] += dp[i - 2] if 10 <= last_two_digits <= 26 else 0
41+
else:
42+
dp[i] += dp[i - 1] + (dp[i - 2] if 10 <= last_two_digits <= 26 else 0)
43+
44+
return dp[-1]
45+
46+
47+
class _LeetCodeTestCases(TestCase):
48+
def test_1(self):
49+
s = "226"
50+
output = 3
51+
self.assertEqual(Solution.numDecodings(Solution(), s), output)
52+
53+
def test_2(self):
54+
s = "2101"
55+
output = 1
56+
self.assertEqual(Solution.numDecodings(Solution(), s), output)
57+
58+
def test_3(self):
59+
s = "06"
60+
output = 0
61+
self.assertEqual(Solution.numDecodings(Solution(), s), output)
62+
63+
64+
if __name__ == '__main__':
65+
main()

0 commit comments

Comments
 (0)