Skip to content

Commit 72b3310

Browse files
author
changmuk.im
committed
solve : week 1 with python
1 parent ee1ad8e commit 72b3310

File tree

5 files changed

+374
-0
lines changed

5 files changed

+374
-0
lines changed

contains-duplicate/EGON.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from unittest import TestCase, main
2+
from typing import List
3+
from collections import Counter
4+
5+
6+
class Solution:
7+
def containsDuplicate(self, nums: List[int]) -> bool:
8+
return self.solve_3(nums=nums)
9+
10+
"""
11+
Runtime: 412 ms (Beats 75.17%)
12+
Analyze Complexity: O(n)
13+
Memory: 31.92 MB (Beats 45.93%)
14+
"""
15+
def solve_1(self, nums: List[int]) -> bool:
16+
return len(nums) != len(set(nums))
17+
18+
"""
19+
Runtime: 423 ms (Beats 39.66%)
20+
Analyze Complexity: O(n)
21+
Memory: 34.54 MB (Beats 14.97%)
22+
"""
23+
def solve_2(self, nums: List[int]) -> bool:
24+
counter = {}
25+
for num in nums:
26+
if num in counter:
27+
return True
28+
else:
29+
counter[num] = True
30+
else:
31+
return False
32+
33+
"""
34+
Runtime: 441 ms (Beats 16.59%)
35+
Analyze Complexity: O(n)
36+
Memory: 34.57 MB (Beats 14.97%)
37+
"""
38+
def solve_3(self, nums: List[int]) -> bool:
39+
return Counter(nums).most_common(1)[0][1] > 1
40+
41+
42+
class _LeetCodeTCs(TestCase):
43+
def test_1(self):
44+
nums = [1, 2, 3, 1]
45+
output = True
46+
self.assertEqual(Solution.containsDuplicate(Solution(), nums=nums), output)
47+
48+
def test_2(self):
49+
nums = [1, 2, 3, 4]
50+
output = False
51+
self.assertEqual(Solution.containsDuplicate(Solution(), nums=nums), output)
52+
53+
def test_3(self):
54+
nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]
55+
output = True
56+
self.assertEqual(Solution.containsDuplicate(Solution(), nums=nums), output)
57+
58+
59+
if __name__ == '__main__':
60+
main()
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from typing import Optional, List
2+
from unittest import TestCase, main
3+
from heapq import heappush, heappop
4+
5+
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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
15+
return self.solve_2(root, k)
16+
17+
"""
18+
Runtime: 50 ms (Beats 25.03%)
19+
Analyze Complexity: O(n)
20+
Memory: 19.55 MB (Beats 15.91%)
21+
"""
22+
def solve_1(self, root: Optional[TreeNode], k: int) -> int:
23+
visited = []
24+
stack = [root]
25+
while stack:
26+
curr_node = stack.pop()
27+
heappush(visited, curr_node.val)
28+
if curr_node.left is not None:
29+
stack.append(curr_node.left)
30+
if curr_node.right is not None:
31+
stack.append(curr_node.right)
32+
33+
result = visited[0]
34+
for _ in range(k):
35+
result = heappop(visited)
36+
37+
return result
38+
39+
"""
40+
Runtime: 43 ms (Beats 69.91%)
41+
Analyze Complexity: O(n)
42+
Memory: 19.46 MB (Beats 60.94%)
43+
"""
44+
def solve_2(self, root: Optional[TreeNode], k: int) -> int:
45+
vals = []
46+
47+
def inorder_traverse(root: Optional[TreeNode]):
48+
if root is None:
49+
return
50+
51+
if len(vals) >= k:
52+
return
53+
54+
inorder_traverse(root.left)
55+
vals.append(root.val)
56+
inorder_traverse(root.right)
57+
58+
inorder_traverse(root)
59+
return vals[k - 1]
60+
61+
62+
class _LeetCodeTCs(TestCase):
63+
def test_1(self):
64+
root = TreeNode(
65+
val=3,
66+
left=TreeNode(
67+
val=1,
68+
left=None,
69+
right=TreeNode(
70+
val=2,
71+
left=None,
72+
right=None,
73+
)
74+
),
75+
right=TreeNode(
76+
val=4,
77+
left=None,
78+
right=None
79+
)
80+
)
81+
82+
k = 1
83+
output = 1
84+
self.assertEqual(Solution.kthSmallest(Solution(), root, k), output)
85+
86+
def test_2(self):
87+
root = TreeNode(
88+
val=5,
89+
left=TreeNode(
90+
val=3,
91+
left=TreeNode(
92+
val=2,
93+
left=TreeNode(
94+
val=1
95+
)
96+
),
97+
right=TreeNode(
98+
val=4
99+
)
100+
),
101+
right=TreeNode(
102+
val=6
103+
)
104+
)
105+
k = 3
106+
output = [3]
107+
self.assertEqual(Solution.kthSmallest(Solution(), root, k), output)
108+
109+
110+
if __name__ == '__main__':
111+
main()

number-of-1-bits/EGON.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from unittest import TestCase, main
2+
3+
4+
class Solution:
5+
def hammingWeight(self, n: int) -> int:
6+
return self.solve_4(n=n)
7+
8+
"""
9+
Runtime: 26 ms (Beats 97.13%)
10+
Analyze Complexity: O(log n), bin(int)가 O(log(n))
11+
Memory: 16.56 MB (Beats 22.67%)
12+
"""
13+
def solve_1(self, n: int) -> int:
14+
return bin(n).count('1')
15+
16+
"""
17+
Runtime: 31 ms (Beats 85.00%)
18+
Analyze Complexity: O(log n)
19+
Memory: 16.60 MB (Beats 22.67%)
20+
"""
21+
def solve_2(self, n: int) -> int:
22+
hamming_weight = 0
23+
while n:
24+
hamming_weight += n % 2
25+
n = n >> 1
26+
return hamming_weight
27+
28+
"""
29+
Runtime: 30 ms (Beats 88.73%)
30+
Analyze Complexity: O(k), k는 2진수의 1의 갯수 (== O(log(n)))
31+
Memory: 16.56 MB (Beats 22.67%)
32+
"""
33+
# Brian Kernighan's Algorithm
34+
def solve_3(self, n: int) -> int:
35+
hamming_weight = 0
36+
while n:
37+
n &= (n - 1)
38+
hamming_weight += 1
39+
return hamming_weight
40+
41+
"""
42+
Runtime: 36 ms (Beats 53.56%)
43+
Analyze Complexity: O(k), k는 2진수의 1의 갯수 (== O(log(n)))
44+
Memory: 16.55 MB (Beats 22.67%)
45+
"""
46+
def solve_4(self, n: int) -> int:
47+
hamming_weight = 0
48+
while n:
49+
hamming_weight += n & 1
50+
n >>= 1
51+
return hamming_weight
52+
53+
54+
class _LeetCodeTCs(TestCase):
55+
def test_1(self):
56+
n = 11
57+
output = 3
58+
self.assertEqual(Solution.hammingWeight(Solution(), n=n), output)
59+
60+
def test_2(self):
61+
n = 128
62+
output = 1
63+
self.assertEqual(Solution.hammingWeight(Solution(), n=n), output)
64+
65+
def test_3(self):
66+
n = 2147483645
67+
output = 30
68+
self.assertEqual(Solution.hammingWeight(Solution(), n=n), output)
69+
70+
71+
if __name__ == '__main__':
72+
main()

palindromic-substrings/EGON.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from unittest import TestCase, main
2+
3+
4+
class Solution:
5+
def countSubstrings(self, s: str) -> int:
6+
return self.solve_2(s)
7+
8+
"""
9+
Runtime: 776 ms (Beats 10.15%)
10+
Analyze Complexity: O(n ** 3), for * for * slicing
11+
Memory: 16.43 MB (Beats 73.44%)
12+
"""
13+
def solve_1(self, s: str) -> int:
14+
count = 0
15+
for left in range(len(s)):
16+
for right in range(left, len(s)):
17+
substring = s[left: right + 1]
18+
count += substring == substring[::-1]
19+
20+
return count
21+
22+
"""
23+
Runtime: 373 ms (Beats 19.31%)
24+
Analyze Complexity: O(n ** 2)
25+
Memory: 25.15 MB (Beats 11.85%)
26+
"""
27+
def solve_2(self, s: str) -> int:
28+
dp = [[left == right for left in range(len(s))] for right in range(len(s))]
29+
count = len(s)
30+
31+
for i in range(len(s) - 1):
32+
dp[i][i + 1] = s[i] == s[i + 1]
33+
count += dp[i][i + 1]
34+
35+
for length in range(3, len(s) + 1):
36+
for left in range(len(s) - length + 1):
37+
right = left + length - 1
38+
dp[left][right] = dp[left + 1][right - 1] and s[left] == s[right]
39+
count += dp[left][right]
40+
41+
return count
42+
43+
44+
class _LeetCodeTCs(TestCase):
45+
def test_1(self):
46+
s = "abc"
47+
output = 3
48+
self.assertEqual(Solution.countSubstrings(Solution(), s), output)
49+
50+
def test_2(self):
51+
s = "aaa"
52+
output = 6
53+
self.assertEqual(Solution.countSubstrings(Solution(), s), output)
54+
55+
56+
if __name__ == '__main__':
57+
main()

top-k-frequent-elements/EGON.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from collections import Counter
2+
from typing import List
3+
from unittest import TestCase, main
4+
5+
6+
class Solution:
7+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
8+
return self.solve_3(nums, k)
9+
10+
"""
11+
Runtime: 82 ms (Beats 87.87%)
12+
Analyze Complexity: O(n log n)
13+
most_common의 정렬이 O(n log n)
14+
Memory: 21.13 MB (Beats 60.35%)
15+
"""
16+
def solve_1(self, nums: List[int], k: int) -> List[int]:
17+
return [key for key, value in Counter(nums).most_common(k)]
18+
19+
"""
20+
Runtime: 88 ms (Beats 62.46%)
21+
Analyze Complexity: O(n log n)
22+
counter 생성에 O(n), 정렬에 O(n log n), slicing에 O(k)
23+
Memory: 21.17 MB (Beats 60.35%)
24+
"""
25+
def solve_2(self, nums: List[int], k: int) -> List[int]:
26+
counter = {}
27+
for num in nums:
28+
counter[num] = 1 + counter.get(num, 0)
29+
30+
sorted_counter = sorted(counter.items(), key=lambda item: -item[1])
31+
return [item[0] for item in sorted_counter[:k]]
32+
33+
34+
"""
35+
Runtime: 81 ms (Beats 90.60%)
36+
Analyze Complexity: O(n)
37+
counter 생성이 O(n), counter_matrix 생성이 O(n), reversed는 O(1), early-return으로 O(k)
38+
Memory: 22.10 MB (Beats 12.57%)
39+
"""
40+
def solve_3(self, nums: List[int], k: int) -> List[int]:
41+
counter = {}
42+
for num in nums:
43+
counter[num] = 1 + counter.get(num, 0)
44+
45+
counter_matrix = [[] for _ in range(len(nums) + 1)]
46+
for key, val in counter.items():
47+
counter_matrix[val].append(key)
48+
49+
result = []
50+
for num_list in reversed(counter_matrix):
51+
for num in num_list:
52+
result.append(num)
53+
if len(result) >= k:
54+
return result
55+
else:
56+
return result
57+
58+
59+
class _LeetCodeTCs(TestCase):
60+
def test_1(self):
61+
nums = [1, 1, 1, 2, 2, 3]
62+
k = 2
63+
output = [1, 2]
64+
self.assertEqual(Solution.topKFrequent(Solution(), nums, k), output)
65+
66+
def test_2(self):
67+
nums = [1]
68+
k = 1
69+
output = [1]
70+
self.assertEqual(Solution.topKFrequent(Solution(), nums, k), output)
71+
72+
73+
if __name__ == '__main__':
74+
main()

0 commit comments

Comments
 (0)