Skip to content

[ayosecu] WEEK 01 solutions #1140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions contains-duplicate/ayosecu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import List

class Solution:
"""
- Time Complexity: O(n), n = len(nums)
- Space Complexity: O(N)
- N = len(set_check) = The number of unique numbers
- If there is no duplicated numbers, N = n
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제마다 시간복잡도/공간복잡도 추가해주시는거 너무 좋은데요?!!
저는 계산해도 시간복잡도만 체크했는데 덕분에 공간복잡도 계산하는 것도 배우고 갑니다!! 👍

def containsDuplicate(self, nums: List[int]) -> bool:
set_check = set()

for num in nums:
if num in set_check:
return True
else:
set_check.add(num)

return False

tc = [
([1, 2, 3, 1], True),
([1, 2, 3, 4], False),
([1,1,1,3,3,4,3,2,4,2], True)
]

for i, (t, e) in enumerate(tc, 1):
sol = Solution()
result = sol.containsDuplicate(t)
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")
30 changes: 30 additions & 0 deletions house-robber/ayosecu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import List

class Solution:
"""
- Time Complexity: O(n), n = len(nums)
- Space Complexity: O(n), n = len(nums)
"""
def rob(self, nums: List[int]) -> int:
# DP Formation
# money[0] = 0
# money[1] = nums[0]
# money[i+1] = max(money[i-1] + nums[i], money[i])

money = [0] * (len(nums) + 1)
money[1] = nums[0]
for i in range(1, len(nums)):
money[i+1] = max(money[i-1] + nums[i], money[i])

return money[-1]

tc = [
([1, 2, 3, 1], 4),
([2, 7, 9, 3, 1], 12),
([1, 2, 0, 5, 10], 12)
]

for i, (nums, e) in enumerate(tc, 1):
sol = Solution()
result = sol.rob(nums)
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")
33 changes: 33 additions & 0 deletions longest-consecutive-sequence/ayosecu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import List

class Solution:
"""
- Time Complexity: O(n), n = len(set_nums) = The number of unique numbers.
- Space Complexity: O(n)
"""
def longestConsecutive(self, nums: List[int]) -> int:
set_nums = set(nums)
longest = 0

for num in set_nums:
if num - 1 not in set_nums:
# Only check for the start number
cnt = 1
next_num = num + 1
while next_num in set_nums:
cnt += 1
next_num += 1
longest = max(longest, cnt)

return longest

tc = [
([100,4,200,1,3,2], 4),
([0,3,7,2,5,8,4,6,0,1], 9),
([1,0,1,2], 3)
]

for i, (nums, e) in enumerate(tc, 1):
sol = Solution()
result = sol.longestConsecutive(nums)
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed!, Expected: {e}, Result: {result}")
27 changes: 27 additions & 0 deletions top-k-frequent-elements/ayosecu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import List
from collections import Counter

class Solution:
"""
- Time Complexity: O(nlogk), n = len(nums)
- Counter(nums) => O(n)
- Counter.most_common(k) => O(nlogk)
- O(n) + O(nlogk) => O(nlogk)
- Space Complexity: O(N)
- N = len(counter_nums) = The number of unique numbers
- Worst case => No duplicated numbers => N = n
"""
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
c = Counter(nums)
return [key for (key, val) in c.most_common(k)]

tc = [
([1,1,1,2,2,3], 2, [1, 2]),
([1], 1, [1])
]

for i, (nums, k, e) in enumerate(tc, 1):
sol = Solution()
result = sol.topKFrequent(nums, k)
result.sort()
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")
34 changes: 34 additions & 0 deletions two-sum/ayosecu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import List

class Solution:
"""
- Time Complexity: O(n), n = len(nums)
- Space Complexity: O(N)
- N = len(dic) = The number of unique numbers
- Worst case, it would be n. => O(N) => O(n)
"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
# Save "num:index" key-value to a dictionary
dic = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dictionary 이용하니까 for문 이중으로 쓰지 않아도 되서 시간복잡도도 줄어들고 좋네요!!
index도 같이 저장할 수 있구요!


for i, num in enumerate(nums):
# Check diff value is already in a dictionary
diff = target - num
if diff in dic:
# If there is a diff value, return indices of pair
return [dic[diff], i]
dic[num] = i

return []

tc = [
([2, 7, 11, 15], 9, [0, 1]),
([3, 2, 4], 6, [1, 2]),
([3, 3], 6, [0, 1])
]

for i, (nums, target, e) in enumerate(tc, 1):
sol = Solution()
result = sol.twoSum(nums, target)
result.sort()
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")