-
-
Notifications
You must be signed in to change notification settings - Fork 245
[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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 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,10 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
index_dict = {elem: idx for idx, elem in enumerate(nums)} | ||
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 |
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.
같은 숫자가 여러 번 등장하면 마지막 인덱스로 덮어씌워질 수 있어 주의가 필요합니다.
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.
말씀하신 것처럼 이 문제에 한정해서는 현재 구현 방식이 문제 없이 동작합니다.
문제에서 항상 정답이 존재하고, 같은 숫자를 두 번 사용할 수 없다는 제약이 있기 때문에 마지막 인덱스로 덮어써도 앞에서부터 탐색하면 자연스럽게 유효한 쌍을 찾게 되죠.
제가 드렸던 코멘트는 각 문제 조건에 따라 다르겠지만 이후에 예를 들어 같은 숫자를 두 번 사용하거나, 모든 가능한 쌍을 찾아야 하는 문제로 확장할 경우 이 방식이 의도치 않게 오동작할 수도 있다는 점을 염두에 두고 드린 조언이었습니다!