Skip to content

Conversation

prograsshopper
Copy link
Contributor

@prograsshopper prograsshopper commented Jul 26, 2025

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

@prograsshopper prograsshopper requested review from a team and chordpli July 26, 2025 14:23
Copy link
Contributor

@printjin-gmailcom printjin-gmailcom left a 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)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같은 숫자가 여러 번 등장하면 마지막 인덱스로 덮어씌워질 수 있어 주의가 필요합니다.

Copy link
Contributor Author

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]의 형태로 나오게 되니 괜찮다고 생각해서 이렇게 처리한건데 혹시 더 일반화된 문제 풀이를 가정하고 주신 답변이실까요?

Copy link
Contributor

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:
Copy link
Contributor

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))
Copy link
Contributor

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]
Copy link
Contributor

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를 의미하긴 하지만 비효율적입니다.

@prograsshopper prograsshopper merged commit b8207ce into DaleStudy:main Jul 27, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from In Review to Completed in 리트코드 스터디 5기 Jul 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

2 participants