-
-
Notifications
You must be signed in to change notification settings - Fork 335
[WHYjun] WEEK 01 solutions #1978
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
Changes from 4 commits
6b57ed3
8b97e42
55a53cc
0deab60
1f31fbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| return len(set(nums)) != len(nums) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| # memo for DP | ||
| dp = {} | ||
| return self.robs(nums, dp, len(nums) - 1) | ||
|
|
||
| def robs(self, nums: List[int], dp: Dict[int, int], houseToRob: int) -> int: | ||
| if houseToRob < 0: | ||
| return 0 | ||
|
|
||
| if houseToRob - 2 not in dp: | ||
| dp[houseToRob - 2] = self.robs(nums, dp, houseToRob - 2) | ||
|
|
||
| if houseToRob - 1 not in dp: | ||
| dp[houseToRob - 1] = self.robs(nums, dp, houseToRob - 1) | ||
|
|
||
|
|
||
| return max( | ||
| dp[houseToRob - 2] + nums[houseToRob], | ||
| dp[houseToRob - 1] | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import heapq | ||
|
|
||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| # remove duplicates by using set | ||
| numsSet = set(nums) | ||
|
|
||
| if len(numsSet) == 0 or len(numsSet) == 1: | ||
| return len(numsSet) | ||
|
|
||
| # Use priority queue to sort in O(n) | ||
| sorted = [] | ||
|
Contributor
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.
Contributor
Author
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. 그렇네요. 인터뷰용으로만 쓰다 보니 이런 부분을 놓치네요. 감사합니다. |
||
| for num in numsSet: | ||
| heapq.heappush(sorted, num) | ||
|
|
||
| prev = heapq.heappop(sorted) | ||
|
Contributor
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. 힙큐를 이용해 단순 정렬을 원하시면
Contributor
Author
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. 감사합니다. 일단 heapify로 해두었습니다. 기록용으로 heapq 풀이법 먼저 생각해두었던 것을 남겨둔 거라서요. |
||
| answer, count = 1, 1 | ||
|
|
||
| for i in range(len(numsSet) - 1): | ||
| popped = heapq.heappop(sorted) | ||
| if prev + 1 == popped: | ||
| count += 1 | ||
| else: | ||
| count = 1 | ||
|
|
||
| prev = popped | ||
| answer = max(answer, count) | ||
|
|
||
| return answer | ||
|
|
||
| def longestConsecutiveUsingSetOnly(self, nums: List[int]) -> int: | ||
| # remove duplicates by using set | ||
| numsSet = set(nums) | ||
|
|
||
| answer = 0 | ||
|
|
||
| for num in numsSet: | ||
| # continue if it's not the longest consecutive | ||
| if num - 1 in numsSet: | ||
| continue | ||
|
WHYjun marked this conversation as resolved.
|
||
| else: | ||
| count = 1 | ||
| while num + count in numsSet: | ||
| count += 1 | ||
| answer = max(answer, count) | ||
|
|
||
| return answer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| # Time complexity: O(nlog(n)) | ||
| # Space Complexity: O(n) | ||
|
|
||
| if len(nums) <= k: | ||
| return list(set(nums)) | ||
|
|
||
| counts = {} | ||
| for num in nums: | ||
| if num not in counts: | ||
| counts[num] = 0 | ||
| counts[num] += 1 | ||
|
|
||
| # The key of dictionary is unique. | ||
| sortedKeys = sorted(list(counts.keys()), key=lambda key: counts[key], reverse = True) | ||
| return sortedKeys[:k] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| memo = {} | ||
|
|
||
| for i, num in enumerate(nums): | ||
| diff = target - num | ||
| if diff in memo: | ||
| # Each input would have exactly one solution | ||
| return [memo[diff], i] | ||
| memo[num] = i | ||
|
|
||
| return [] |
Uh oh!
There was an error while loading. Please reload this page.