-
-
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
Conversation
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.
첫 주 수고하셨습니다. 2주차도 화이팅입니다 :)
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
앗 제 생각에 이 문제에 한정해선 이렇게 풀어도 문제가 안 될 것 같아서 이렇게 처리했어요.
문제에서 두 개의 숫자의 합이라고 명시되어있고, 제약조건에서 1. 동일한 요소를 사용하지 말 것 2. 답의 순서는 상관없음 인데, 그렇다면 같은 숫자가 여러번 등장하게 되더라도 루프는 앞에서부터 돌게 되므로 [same_num_1, same_num_last]의 형태로 나오게 되니 괜찮다고 생각해서 이렇게 처리한건데 혹시 더 일반화된 문제 풀이를 가정하고 주신 답변이실까요?
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.
말씀하신 것처럼 이 문제에 한정해서는 현재 구현 방식이 문제 없이 동작합니다.
문제에서 항상 정답이 존재하고, 같은 숫자를 두 번 사용할 수 없다는 제약이 있기 때문에 마지막 인덱스로 덮어써도 앞에서부터 탐색하면 자연스럽게 유효한 쌍을 찾게 되죠.
제가 드렸던 코멘트는 각 문제 조건에 따라 다르겠지만 이후에 예를 들어 같은 숫자를 두 번 사용하거나, 모든 가능한 쌍을 찾아야 하는 문제로 확장할 경우 이 방식이 의도치 않게 오동작할 수도 있다는 점을 염두에 두고 드린 조언이었습니다!
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 comment
The reason will be displayed to describe this comment to others. Learn more.
remain이 0일 경우에도 if remain: 조건이 False가 되어 버그 발생 가능합니다.
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)) |
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되지 않았습니다.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
list(dict)는 key만 리스트로 변환되므로, 여기서는 top k frequent elements를 의미하긴 하지만 비효율적입니다.
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!