Skip to content

Commit 03b5623

Browse files
authored
Merge pull request #322 from lymchgmk/feat/week1
[EGON] Week 01 Solutions
2 parents 3780d9f + 90e72e8 commit 03b5623

File tree

10 files changed

+690
-0
lines changed

10 files changed

+690
-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()

โ€Žcontains-duplicate/EGON.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
3+
class Solution {
4+
func containsDuplicate(_ nums: [Int]) -> Bool {
5+
return solve_2(nums)
6+
}
7+
8+
/*
9+
Runtime: 246 ms (Beats 68.44%)
10+
Analyze Complexity: O(n)
11+
Memory: 19.94 MB (Beats 76.01%)
12+
*/
13+
func solve_1(_ nums: [Int]) -> Bool {
14+
return nums.count != Set(nums).count
15+
}
16+
17+
/*
18+
Runtime: 240 ms (Beats 90.56%)
19+
Analyze Complexity: O(n)
20+
Memory: 22.56 MB (Beats 33.43%)
21+
*/
22+
func solve_2(_ nums: [Int]) -> Bool {
23+
var counter: [Int: Bool] = [:]
24+
for num in nums {
25+
if counter[num] != nil {
26+
return true
27+
} else {
28+
counter[num] = true
29+
}
30+
}
31+
32+
return false
33+
}
34+
}
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 * log n), ์ˆœํšŒ์— n, heap push์— log 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()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Foundation
2+
3+
public class TreeNode {
4+
public var val: Int
5+
public var left: TreeNode?
6+
public var right: TreeNode?
7+
public init() { self.val = 0; self.left = nil; self.right = nil; }
8+
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9+
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10+
self.val = val
11+
self.left = left
12+
self.right = right
13+
}
14+
}
15+
16+
class Solution {
17+
18+
/*
19+
Runtime: 28 ms (Beats 46.95%)
20+
Analyze Complexity: O(n)
21+
Memory: 16.52 MB (Beats 53.05%)
22+
*/
23+
func kthSmallest(_ root: TreeNode?, _ k: Int) -> Int {
24+
func inorderTraverse(_ root: TreeNode?, _ k: Int) {
25+
guard let root = root, visited.count < k else {
26+
return
27+
}
28+
29+
inorderTraverse(root.left, k)
30+
if visited.count < k {
31+
visited.append(root.val)
32+
}
33+
inorderTraverse(root.right, k)
34+
}
35+
36+
var visited: [Int] = []
37+
inorderTraverse(root, k)
38+
39+
return visited[k - 1]
40+
}
41+
}

โ€Ž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()

โ€Žnumber-of-1-bits/EGON.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Foundation
2+
3+
class Solution {
4+
func hammingWeight(_ n: Int) -> Int {
5+
return solve_2(n)
6+
}
7+
8+
/*
9+
Runtime: 5 ms (Beats 38.73%)
10+
Analyze Complexity: O(k), k๋Š” 2์ง„์ˆ˜์˜ 1์˜ ๊ฐฏ์ˆ˜
11+
Memory: 14.88 MB (Beats 98.27%)
12+
*/
13+
func solve_1(_ n: Int) -> Int {
14+
return n.nonzeroBitCount
15+
}
16+
17+
/*
18+
Runtime: 3 ms (Beats 65.90%)
19+
Analyze Complexity: O(k), k๋Š” 2์ง„์ˆ˜์˜ 1์˜ ๊ฐฏ์ˆ˜
20+
Memory: 15.08 MB (Beats 89.02%)
21+
*/
22+
func solve_2(_ n: Int) -> Int {
23+
var num = n
24+
var hammingWeight = 0
25+
while 0 < num {
26+
num &= num - 1
27+
hammingWeight += 1
28+
}
29+
30+
return hammingWeight
31+
}
32+
}

0 commit comments

Comments
ย (0)