-
-
Notifications
You must be signed in to change notification settings - Fork 301
[ppxyn1] WEEK 01 solutions #1982
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
+146
−0
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
390b1b0
[:solved] two leetcode problem
ppxyn1 6670e7e
[:fixed] changed one file to pull request
ppxyn1 a054fb1
[:solved] top-k-frequent-elements
ppxyn1 f6c4477
[:solved] longest-consecutive-sequence
ppxyn1 88f0079
[:fixed] error
ppxyn1 91b0076
[:fixed] linelint
ppxyn1 c75360b
[:fixed] linelint
ppxyn1 c0b7825
[:fixed] linelint
ppxyn1 ea0550c
[:solved] house-robber
ppxyn1 ef38f39
[:fixed] linelint
ppxyn1 2d4f7c2
[:fixed] logest-consecutive-sequence
ppxyn1 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,19 @@ | ||
| # idea: Hash | ||
|
|
||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| count_dict={} | ||
| for i in range(len(nums)): | ||
| # print(count_dict) | ||
| if nums[i] in count_dict.keys(): | ||
| return True | ||
| else: | ||
| count_dict[nums[i]] = 1 | ||
| return False | ||
|
|
||
|
|
||
| ''' | ||
| Trial and error | ||
| Printing I/O inside the loop may cause Output Limit Exceeded | ||
| ''' | ||
|
|
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 @@ | ||
| # idea: - | ||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| sorted_nums = sorted(list(set(nums))) | ||
| # sorted_nums = sorted(nums) # duplicate | ||
| max_len = 1 | ||
| tmp = 1 | ||
|
|
||
| for i in range(1, len(sorted_nums)): | ||
| if sorted_nums[i] == sorted_nums[i - 1] + 1: | ||
| tmp += 1 | ||
| else: | ||
| max_len = max(max_len, tmp) | ||
| tmp = 1 | ||
| ans = max(max_len, tmp) | ||
| return ans | ||
|
|
||
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 @@ | ||
| # idea: dictonary | ||
|
|
||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| count_dict = {} | ||
| ans = [] | ||
| for idx, val in enumerate(nums): | ||
| if val not in count_dict: | ||
| count_dict[val] = 1 | ||
| else: | ||
| count_dict[val] +=1 | ||
| sorted_items = sorted(count_dict.items(), key=lambda x: x[1], reverse=True) #sorted return list / dict.items() is tuple | ||
| # print(sorted_items) | ||
| for i in range(k): | ||
| ans.append(sorted_items[i][0]) | ||
| return ans | ||
|
|
||
| ''' | ||
| Similar way : Using Counter() function | ||
| ''' | ||
|
|
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 @@ | ||
| # idea: For each number n in nums, check if (target - n) exists in the remaining elements. | ||
|
|
||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| for idx, num in enumerate(nums): | ||
| required_num = target - num | ||
| if required_num in nums[idx+1:]: | ||
| return [idx, nums.index(required_num, idx+1)] | ||
|
|
||
|
|
||
| ''' | ||
| Trial and error | ||
| idea : two pointer | ||
| I struggled to handle the indices of the original array after sorting it. | ||
| The code below fails when negative numbers are involved. | ||
| I realized it would be tricky to solve this problem with the two-pointer. | ||
| ''' | ||
|
|
||
| # class Solution: | ||
| # def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| # sorted_nums = sorted(nums) | ||
| # left, right = 0, len(nums) - 1 | ||
|
|
||
| # while left < right: | ||
| # s = sorted_nums[left] + sorted_nums[right] | ||
| # if s == target: | ||
| # left_idx = nums.index(sorted_nums[left]) | ||
| # right_idx = nums.index(sorted_nums[right], left_idx + 1) | ||
| # return [left_idx, right_idx] | ||
| # elif s < target: | ||
| # left += 1 | ||
| # else: | ||
| # right -= 1 | ||
|
|
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.
You must write an algorithm that runs in O(n) time.