-
-
Notifications
You must be signed in to change notification settings - Fork 303
[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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b57ed3
[WHYjun] WEEK 01 solutions - (3/5)
WHYjun 8b97e42
[WHYjun] WEEK 01 solutions - (3/5)
WHYjun 55a53cc
[WHYjun] WEEK 01 solutions - (4/5)
WHYjun 0deab60
[WHYjun] WEEK 01 solutions - (5/5)
WHYjun 1f31fbe
[WHYjun] Address comments to Week 01 solution
WHYjun 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,3 @@ | ||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| return len(set(nums)) != len(nums) |
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,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] | ||
| ) | ||
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,44 @@ | ||
| 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) | ||
| pq = heapq.heapify(list(numsSet)) | ||
|
|
||
| prev = heapq.heappop(pq) | ||
| answer, count = 1, 1 | ||
| for i in range(len(numsSet) - 1): | ||
| popped = heapq.heappop(pq) | ||
| 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.
Show resolved
Hide resolved
|
||
| else: | ||
| count = 1 | ||
| while num + count in numsSet: | ||
| count += 1 | ||
| answer = max(answer, count) | ||
|
|
||
| return answer | ||
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,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] |
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,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 [] |
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.
Uh oh!
There was an error while loading. Please reload this page.