-
-
Notifications
You must be signed in to change notification settings - Fork 338
[hyeri0903] WEEK 01 solutions #2363
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
9 commits
Select commit
Hold shift + click to select a range
254e4c7
two sum solution
hyeri0903 0307632
개행추가
hyeri0903 f551b03
add new line
hyeri0903 12e62d0
contain duplicate solution
hyeri0903 8fa2c60
top-k-frequent-elements solution
hyeri0903 7f99b92
top-l-frequent-elements solution2
hyeri0903 8c2adbc
longest-consecutive-sequence solution
hyeri0903 6a0c851
longestConsecutive solution2
hyeri0903 1f51b19
house robber solution
hyeri0903 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,13 @@ | ||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| ans = False | ||
| dic = dict() | ||
|
|
||
| for n in nums: | ||
| if n in dic: | ||
| return True | ||
| else: | ||
| dic[n] = 1 | ||
| 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,14 @@ | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| dp = {} | ||
| dp[0] = nums[0] | ||
| if len(nums) >= 2: | ||
| dp[1] = max(nums[0], nums[1]) | ||
| else: | ||
| return nums[0] | ||
|
|
||
| for i in range(2, len(nums)): | ||
| dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
|
|
||
| res = list(dp.values())[-1] | ||
| return res |
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,36 @@ | ||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| if len(nums) <= 0: | ||
| return 0 | ||
|
|
||
| res = 0 | ||
| length = 1 | ||
| nums.sort() | ||
|
|
||
| for i in range(len(nums)-1): | ||
| if nums[i] == nums[i+1]: | ||
| continue | ||
| if nums[i] + 1 == nums[i+1]: | ||
| length += 1 | ||
| else: | ||
| res = max(res, length) | ||
| length = 1 | ||
|
|
||
| res = max(res, length) | ||
| return res | ||
|
|
||
| #set을 사용한 풀이 | ||
| # if len(nums) <= 0: | ||
| # return 0 | ||
| # res, cnt = 1, 1 | ||
| # sorted_nums = list(set(nums)) | ||
| # sorted_nums.sort() | ||
| # print(sorted_nums) | ||
|
|
||
| # for i in range(len(sorted_nums) - 1): | ||
| # if sorted_nums[i + 1] - sorted_nums[i] == 1: | ||
| # cnt += 1 | ||
| # else: | ||
| # cnt = 1 | ||
| # res = max(res, cnt) | ||
| # return res |
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,24 @@ | ||
| import heapq | ||
|
|
||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| table = {} | ||
| for n in nums: | ||
| if n in table: | ||
| table[n] += 1 | ||
| else: | ||
| table[n] = 1 | ||
| #sol 1) sorting 사용한 case | ||
| # sorted_table = sorted(table.items(), key=lambda x:x[1], reverse = True) | ||
| # return [ key for key, freq in sorted_table[:k]] | ||
|
|
||
| #sol 2) min heap 사용한 case | ||
| #convert the frequencies to negative values so it return first | ||
| heap = [(-freq, key) for key, freq in table.items()] | ||
| heapq.heapify(heap) | ||
|
|
||
| res = [] | ||
| for _ in range(k): | ||
| freq, key = heapq.heappop(heap) | ||
| res.append(key) | ||
| return res |
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,8 @@ | ||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
|
|
||
| for i in range(len(nums)): | ||
| for j in range(i+1, len(nums)): | ||
| if nums[i] + nums[j] == target: | ||
| return [i, j] | ||
|
|
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.
굉장히 잘푸시지만 코드의 상단에 접근 방법, 시간복잡도, 공간복잡도를 미리 작성하고나서 솔루션을 함께 기록하면 어떨까하는 자그마한 제안거리 드려봅니다 ㅎㅎ