좋다#149
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request implements a two-pointer solution for the 'Good Number' problem in Swift. The feedback suggests removing a large block of commented-out code to improve maintainability and moving the 'target' variable declaration outside the while loop to optimize performance by reducing redundant array accesses.
Comment on lines
+1
to
+33
| // 잘못된 풀이 -> 음수를 생각안함 | ||
| // let n = Int(readLine()!)! | ||
| // let sortedNums = readLine()!.split(separator: " ").compactMap { Int($0) }.sorted() | ||
|
|
||
| // func good(_ nums: [Int], target: Int) -> Bool { | ||
| // var left = 0 | ||
| // var right = nums.count - 1 | ||
|
|
||
| // while left < right { | ||
| // let sum = nums[left] + nums[right] | ||
|
|
||
| // if sum < target { | ||
| // left += 1 | ||
| // } else if sum > target { | ||
| // right -= 1 | ||
| // } else { | ||
| // return true | ||
| // } | ||
| // } | ||
|
|
||
| // return false | ||
| // } | ||
|
|
||
| // var result = 0 | ||
| // for i in 2..<n { | ||
| // let nums = Array(sortedNums[0..<i]) | ||
|
|
||
| // if good(nums, target: sortedNums[i]) { | ||
| // result += 1 | ||
| // } | ||
| // } | ||
| // print(result) | ||
|
|
Contributor
Comment on lines
+42
to
+55
|
|
||
| while left < right { | ||
| if left == targetIndex { | ||
| left += 1 | ||
| continue | ||
| } | ||
|
|
||
| if right == targetIndex { | ||
| right -= 1 | ||
| continue | ||
| } | ||
|
|
||
| let sum = nums[left] + nums[right] | ||
| let target = nums[targetIndex] |
Contributor
There was a problem hiding this comment.
target 변수는 while 루프 내부에서 값이 변하지 않는 불변값입니다. 이를 루프 외부로 이동시키면 매 반복마다 배열 인덱스에 접근하는 연산을 줄일 수 있어 효율적입니다.
Suggested change
| while left < right { | |
| if left == targetIndex { | |
| left += 1 | |
| continue | |
| } | |
| if right == targetIndex { | |
| right -= 1 | |
| continue | |
| } | |
| let sum = nums[left] + nums[right] | |
| let target = nums[targetIndex] | |
| let target = nums[targetIndex] | |
| while left < right { | |
| if left == targetIndex { | |
| left += 1 | |
| continue | |
| } | |
| if right == targetIndex { | |
| right -= 1 | |
| continue | |
| } | |
| let sum = nums[left] + nums[right] |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🔗 문제 링크
✔️ 소요된 시간
45m
📚 새롭게 알게된 내용