-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0d73a28
- Solution for "217. Contains Duplicate".
ayosecu 1343e82
- Solution for Two Sum #219
ayosecu 192ef04
- Solution for "Top K Frequent Elements #237"
ayosecu 91e2247
- Solution for "Longest Consecutive Sequence #240".
ayosecu bcbb7f8
- Solution for "House Robber #264".
ayosecu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" | ||
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}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dictionary 이용하니까 for문 이중으로 쓰지 않아도 되서 시간복잡도도 줄어들고 좋네요!! |
||
|
||
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}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문제마다 시간복잡도/공간복잡도 추가해주시는거 너무 좋은데요?!!
저는 계산해도 시간복잡도만 체크했는데 덕분에 공간복잡도 계산하는 것도 배우고 갑니다!! 👍