-
-
Notifications
You must be signed in to change notification settings - Fork 303
[6kitty] WEEK 01 solutions #1992
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
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,20 @@ | ||
| # | ||
| # @lc app=leetcode id=217 lang=python3 | ||
| # | ||
| # [217] Contains Duplicate | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| # list로 하니까 시간 초과 떠서 set으로 변경 | ||
| idx=set() | ||
| for i in nums: | ||
| if i in idx: | ||
| return True | ||
| idx.add(i) | ||
| # null 방지 | ||
| return False | ||
|
|
||
| # @lc code=end | ||
|
|
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 @@ | ||
| # | ||
| # @lc app=leetcode id=198 lang=python3 | ||
| # | ||
| # [198] House Robber | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| pre=0 | ||
| cur=0 | ||
| for num in nums: | ||
| pre, cur = cur, max(num + pre, cur) | ||
| return cur | ||
|
|
||
| # @lc code=end | ||
|
|
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,28 @@ | ||
| # | ||
| # @lc app=leetcode id=128 lang=python3 | ||
| # | ||
| # [128] Longest Consecutive Sequence | ||
| # | ||
| from typing import List | ||
| # @lc code=start | ||
| class Solution: | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| nums.sort() | ||
| long=0 | ||
|
|
||
| leng=1 | ||
| for i in range(len(nums)-1): | ||
| if nums[i]==nums[i+1]: | ||
| continue | ||
| if nums[i]+1==nums[i+1]: | ||
| leng+=1 | ||
| else: | ||
| long=max(long,leng) | ||
| leng=1 | ||
| # 아래를 추가해야 for문 다 돌았을 때 leng이 최고인 경우 반영 | ||
| long=max(long,leng) | ||
| return long | ||
|
|
||
|
|
||
| # @lc code=end | ||
|
|
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,25 @@ | ||
| # | ||
| # @lc app=leetcode id=347 lang=python3 | ||
| # | ||
| # [347] Top K Frequent Elements | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| sol = {} | ||
| for i in nums: | ||
| sol[i] = sol.get(i, 0) + 1 | ||
|
|
||
| sorted_items = sorted(sol.items(), key=lambda x: x[1], reverse=True) | ||
|
|
||
| ssol = [] | ||
| for i in range(k): | ||
| ssol.append(sorted_items[i][0]) | ||
|
|
||
| return ssol | ||
|
|
||
|
|
||
|
|
||
| # @lc code=end | ||
|
|
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,16 @@ | ||
| # | ||
| # @lc app=leetcode id=1 lang=python3 | ||
| # | ||
| # [1] Two Sum | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| for i,num in enumerate(nums): | ||
| for j in range(i+1,len(nums)): | ||
| if num + nums[j] == target: | ||
| return [i,j] | ||
|
|
||
| # @lc code=end | ||
|
|
||
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.
for 문을 2중 루프로 돌아서 해결하는 방법도 있지만 달레님 해설에도 나온 것 처럼 해시 테이블이나 아니면 left, right 를 사용해서 1번의 for 문 으로도 해결할 수 있을 것 같습니다.
나머지 문제들은 정석대로 잘 풀어주신 것 같습니다!