-
-
Notifications
You must be signed in to change notification settings - Fork 335
[changhyumm] WEEK 01 solutions #1994
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
7 commits
Select commit
Hold shift + click to select a range
c384610
two sum solution
changhyumm 908be5c
contains-duplicate solution
changhyumm c7f9bb4
fix: posix
changhyumm d35809f
fix: 줄바꿈
changhyumm 05c8def
longest-consecutive-sequence
changhyumm dc00511
top-k-frequent-elements
changhyumm f0a962d
house-robber
changhyumm 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,9 @@ | ||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| dic = dict() | ||
| for num in nums: | ||
| if num in dic: | ||
| return True | ||
| else: | ||
| dic[num] = True | ||
| return False |
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,20 @@ | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| n = len(nums) | ||
| # 집이 하나면 최대 profit은 그 자체 | ||
| if n == 1: | ||
| return nums[0] | ||
|
|
||
| # dp[i]를 i+1 집까지 털었을때의 max라고 정의 | ||
| # dp[i] = max(dp[i-1], nums[i] + dp[i-2]) | ||
| dp = [0] * n | ||
| dp[0] = nums[0] | ||
| dp[1] = max(nums[0], nums[1]) | ||
|
|
||
| # dp를 누적 | ||
| for i in range(2, n): | ||
| dp[i] = max(dp[i-1], nums[i] + dp[i-2]) | ||
|
|
||
| # time O(n) - loop 1번 | ||
| # space O(n) - dp 배열 | ||
| return dp[-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,12 @@ | ||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| num_set = set(nums) | ||
| max_length = 0 | ||
|
|
||
| for num in num_set: | ||
| if num - 1 not in num_set: | ||
| length = 1 | ||
| while num + length in num_set: | ||
| length += 1 | ||
| max_length = max(max_length, length) | ||
| return max_length |
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]: | ||
| freq = dict() | ||
| for num in nums: | ||
| if num in freq: | ||
| freq[num] += 1 | ||
| else: | ||
| freq[num] = 1 | ||
|
|
||
| result = sorted(freq.items(), key=lambda x: x[1], reverse=True) | ||
| ans = [] | ||
| idx = 0 | ||
| while k > 0: | ||
| ans.append(result[idx][0]) | ||
| idx += 1 | ||
| k -= 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,12 @@ | ||
| 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] | ||
| dic = dict() | ||
|
changhyumm marked this conversation as resolved.
|
||
| for idx, num in enumerate(nums): | ||
| if target - num in dic: | ||
| return [idx, dic[target-num]] | ||
| else: | ||
| dic[num] = idx | ||
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.