-
-
Notifications
You must be signed in to change notification settings - Fork 338
[doh6077] WEEK 01 solutions #2370
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
+90
−23
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e890414
217. Contains Duplicate Solution
doh6077 03641f9
줄바꿈
doh6077 7755c5d
Two Sum solution
doh6077 f4cafbc
Top K Frequent Elements solution
doh6077 3e89531
comment out the previous solution
doh6077 f51e31c
Longest Consecutive Sequence Solution
doh6077 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 |
|---|---|---|
| @@ -1,10 +1,7 @@ | ||
| # https://leetcode.com/problems/contains-duplicate/description/ | ||
|
|
||
| # set에 저장하면서 중복 여부 확인하기 | ||
| class Solution: | ||
| def containsDuplicate(self, nums: list[int]) -> bool: | ||
| hashset = set() | ||
| for i in nums: | ||
| if i in hashset: | ||
| return True | ||
| hashset.add(i) | ||
| return False | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| # Create a set to store unique numbers from nums | ||
| nums_set = set(nums) | ||
| return len(nums_set) != 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
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 |
|---|---|---|
| @@ -1,12 +1,36 @@ | ||
|
|
||
| # 6기 | ||
| # class Solution: | ||
| # # dictionary use | ||
| # def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| # result = {} # key: 원소, value: 등장 횟수 | ||
| # for n in nums: | ||
| # if n in result: | ||
| # result[n] = result[n] + 1 | ||
| # else: | ||
| # result[n] = 1 | ||
|
|
||
| # # 가장 자주 등장한 원소 k개 반환 | ||
| # return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k] | ||
|
|
||
| # 7기 | ||
| class Solution: | ||
| # dictionary use | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| result = {} # key: 원소, value: 등장 횟수 | ||
| for n in nums: | ||
| if n in result: | ||
| result[n] = result[n] + 1 | ||
| # 1. Count frequency of each number | ||
| freq = {} | ||
|
|
||
| for num in nums: | ||
| if num not in freq: | ||
| freq[num] = 1 | ||
| else: | ||
| result[n] = 1 | ||
| freq[num] += 1 | ||
|
|
||
| # 2. Sort by frequency in descending order | ||
| sorted_items = sorted(freq.items(), key=lambda item: item[1], reverse=True) | ||
|
|
||
| # 3. Take the first k elements | ||
| result = [] | ||
| for i in range(k): | ||
| result.append(sorted_items[i][0]) | ||
|
|
||
| # 가장 자주 등장한 원소 k개 반환 | ||
| return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k] | ||
| return result | ||
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 |
|---|---|---|
| @@ -1,8 +1,24 @@ | ||
| # 6기 | ||
| # class Solution: | ||
| # def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
| # prevMap = {} # val : index | ||
| # for i, n in enumerate(nums): | ||
| # diff = target - n | ||
| # if diff in prevMap: | ||
| # return [prevMap[diff], i] | ||
| # prevMap[n] = i | ||
|
|
||
| # 7기 | ||
| # https://leetcode.com/problems/two-sum/description/ | ||
| class Solution: | ||
| def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
| prevMap = {} # val : index | ||
| for i, n in enumerate(nums): | ||
| diff = target - n | ||
| if diff in prevMap: | ||
| return [prevMap[diff], i] | ||
| prevMap[n] = i | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| # use Hash map to save num and index | ||
| nums_hm = {} | ||
|
|
||
| for i, num in enumerate(nums): | ||
|
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. enumerate를 사용해서 index와 num을 동시에 가져온 점이 좋았습니다. |
||
| find_val = target - num | ||
|
|
||
| if find_val in nums_hm: | ||
| return [nums_hm[find_val], i] | ||
|
|
||
| nums_hm[num] = i | ||
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.
sorted를 사용할 때 reverse 옵션 외에 key 옵션을 넣어 값을 비교할 수 있다는 점을 알았습니다.