Skip to content

Commit 337ca17

Browse files
authored
Merge pull request #1140 from ayosecu/main
[ayosecu] WEEK 01 solutions
2 parents 9aefc67 + bcbb7f8 commit 337ca17

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

contains-duplicate/ayosecu.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(nums)
6+
- Space Complexity: O(N)
7+
- N = len(set_check) = The number of unique numbers
8+
- If there is no duplicated numbers, N = n
9+
"""
10+
def containsDuplicate(self, nums: List[int]) -> bool:
11+
set_check = set()
12+
13+
for num in nums:
14+
if num in set_check:
15+
return True
16+
else:
17+
set_check.add(num)
18+
19+
return False
20+
21+
tc = [
22+
([1, 2, 3, 1], True),
23+
([1, 2, 3, 4], False),
24+
([1,1,1,3,3,4,3,2,4,2], True)
25+
]
26+
27+
for i, (t, e) in enumerate(tc, 1):
28+
sol = Solution()
29+
result = sol.containsDuplicate(t)
30+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")

house-robber/ayosecu.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(nums)
6+
- Space Complexity: O(n), n = len(nums)
7+
"""
8+
def rob(self, nums: List[int]) -> int:
9+
# DP Formation
10+
# money[0] = 0
11+
# money[1] = nums[0]
12+
# money[i+1] = max(money[i-1] + nums[i], money[i])
13+
14+
money = [0] * (len(nums) + 1)
15+
money[1] = nums[0]
16+
for i in range(1, len(nums)):
17+
money[i+1] = max(money[i-1] + nums[i], money[i])
18+
19+
return money[-1]
20+
21+
tc = [
22+
([1, 2, 3, 1], 4),
23+
([2, 7, 9, 3, 1], 12),
24+
([1, 2, 0, 5, 10], 12)
25+
]
26+
27+
for i, (nums, e) in enumerate(tc, 1):
28+
sol = Solution()
29+
result = sol.rob(nums)
30+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(set_nums) = The number of unique numbers.
6+
- Space Complexity: O(n)
7+
"""
8+
def longestConsecutive(self, nums: List[int]) -> int:
9+
set_nums = set(nums)
10+
longest = 0
11+
12+
for num in set_nums:
13+
if num - 1 not in set_nums:
14+
# Only check for the start number
15+
cnt = 1
16+
next_num = num + 1
17+
while next_num in set_nums:
18+
cnt += 1
19+
next_num += 1
20+
longest = max(longest, cnt)
21+
22+
return longest
23+
24+
tc = [
25+
([100,4,200,1,3,2], 4),
26+
([0,3,7,2,5,8,4,6,0,1], 9),
27+
([1,0,1,2], 3)
28+
]
29+
30+
for i, (nums, e) in enumerate(tc, 1):
31+
sol = Solution()
32+
result = sol.longestConsecutive(nums)
33+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed!, Expected: {e}, Result: {result}")

top-k-frequent-elements/ayosecu.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
from collections import Counter
3+
4+
class Solution:
5+
"""
6+
- Time Complexity: O(nlogk), n = len(nums)
7+
- Counter(nums) => O(n)
8+
- Counter.most_common(k) => O(nlogk)
9+
- O(n) + O(nlogk) => O(nlogk)
10+
- Space Complexity: O(N)
11+
- N = len(counter_nums) = The number of unique numbers
12+
- Worst case => No duplicated numbers => N = n
13+
"""
14+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
15+
c = Counter(nums)
16+
return [key for (key, val) in c.most_common(k)]
17+
18+
tc = [
19+
([1,1,1,2,2,3], 2, [1, 2]),
20+
([1], 1, [1])
21+
]
22+
23+
for i, (nums, k, e) in enumerate(tc, 1):
24+
sol = Solution()
25+
result = sol.topKFrequent(nums, k)
26+
result.sort()
27+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")

two-sum/ayosecu.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(nums)
6+
- Space Complexity: O(N)
7+
- N = len(dic) = The number of unique numbers
8+
- Worst case, it would be n. => O(N) => O(n)
9+
"""
10+
def twoSum(self, nums: List[int], target: int) -> List[int]:
11+
# Save "num:index" key-value to a dictionary
12+
dic = {}
13+
14+
for i, num in enumerate(nums):
15+
# Check diff value is already in a dictionary
16+
diff = target - num
17+
if diff in dic:
18+
# If there is a diff value, return indices of pair
19+
return [dic[diff], i]
20+
dic[num] = i
21+
22+
return []
23+
24+
tc = [
25+
([2, 7, 11, 15], 9, [0, 1]),
26+
([3, 2, 4], 6, [1, 2]),
27+
([3, 3], 6, [0, 1])
28+
]
29+
30+
for i, (nums, target, e) in enumerate(tc, 1):
31+
sol = Solution()
32+
result = sol.twoSum(nums, target)
33+
result.sort()
34+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")

0 commit comments

Comments
 (0)