-
-
Notifications
You must be signed in to change notification settings - Fork 247
[prograsshopper] Week 1 Solutions #1720
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
return len(set(nums)) != len(nums) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
from collections import defaultdict | ||
frequent_dict = defaultdict(int) | ||
for num in nums: | ||
frequent_dict[num] += 1 | ||
sorted_dict = dict(sorted(frequent_dict.items(), key=operator.itemgetter(1), reverse=True)) | ||
return list(sorted_dict)[:k] | ||
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. list(dict)는 key만 리스트로 변환되므로, 여기서는 top k frequent elements를 의미하긴 하지만 비효율적입니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
index_dict = {elem: idx for idx, elem in enumerate(nums)} | ||
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. 같은 숫자가 여러 번 등장하면 마지막 인덱스로 덮어씌워질 수 있어 주의가 필요합니다. 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. 앗 제 생각에 이 문제에 한정해선 이렇게 풀어도 문제가 안 될 것 같아서 이렇게 처리했어요. 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. 말씀하신 것처럼 이 문제에 한정해서는 현재 구현 방식이 문제 없이 동작합니다. |
||
result = [] | ||
for idx, num in enumerate(nums): | ||
remain = index_dict.get(target-num, None) | ||
if remain and idx != remain: | ||
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. remain이 0일 경우에도 if remain: 조건이 False가 되어 버그 발생 가능합니다. |
||
result = [idx, remain] | ||
break | ||
return result |
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.
operator 모듈을 사용하고 있지만 import되지 않았습니다.