-
-
Notifications
You must be signed in to change notification settings - Fork 195
[이유진] Week3 #789
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
[이유진] Week3 #789
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,23 @@ | ||
package leetcode_study | ||
|
||
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> { | ||
val result = mutableListOf<List<Int>>() | ||
val nums = ArrayDeque<Int>() | ||
dfs(candidates, target, 0, 0, nums, result) | ||
|
||
return result | ||
} | ||
|
||
private fun dfs(candidates: IntArray, target: Int, startIdx: Int, total: Int, nums: ArrayDeque<Int>, result: MutableList<List<Int>>) { | ||
if (target < total) return | ||
if (target == total) { | ||
result.add(ArrayList(nums)) | ||
return | ||
} | ||
for (i in startIdx..< candidates.size) { | ||
val num = candidates[i] | ||
nums.add(num) | ||
dfs(candidates, target, i, total + num, nums, result) | ||
nums.removeLast() | ||
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. 저는 ArrayDeque가 익숙하지 않은데, removeLast() 메소드 덕분에 가독성이 좋은거 같네요! 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. kotlin 에서는 java 의 Stack 대신에 ArrayDeque 를 사용하는 것을 권장하더라구요 :) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package leetcode_study | ||
|
||
fun productExceptSelf(nums: IntArray): IntArray { | ||
// ex. nums = [1, 2, 3, 4] | ||
val leftStartProducts = mutableListOf(1) | ||
val rightStartProducts = mutableListOf(1) | ||
|
||
// mutableNums = [1, 1, 2, 3, 4] | ||
val mutableNums = nums.toMutableList() | ||
mutableNums.add(0, 1) | ||
mutableNums.add(1) | ||
|
||
// leftStartProducts = [1, 1, 2, 6, 24, 24] | ||
// rightStartProducts = [24, 24, 24, 12, 4, 1] | ||
for (idx in 1..< mutableNums.size) { | ||
val leftNum = mutableNums[idx] | ||
val rightNum = mutableNums[mutableNums.size - 1 - idx] | ||
|
||
leftStartProducts.add(leftStartProducts.last() * leftNum) | ||
rightStartProducts.add(index = 0, element = rightStartProducts.first() * rightNum) | ||
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. rightStartProducts의 0번째에 삽입하는 대신 혹은 leftStartProducts, rightStartProducts에 add() 하는 대신 result에 즉시 production을 하는 방법도 고려해보시면 좋을 거 같습니다! 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 의 0 번째 index 에 추가하는 것보다 add() 를 하는 것이 성능이 더 좋은건가요~? result 에 즉시 production 을 하기 위해서는 누적된 곱셈값을 알고 있어야 해서 매번 계산하는 것보다는 for 문을 한 번 돌면서 leftStartProducts, rightStartProducts 에 기록된 누적곱 값을 사용하는 것이 좋다고 판단했습니다 :) |
||
} | ||
|
||
val result = mutableListOf<Int>() | ||
for (idx in 0..mutableNums.size - 3) { | ||
result.add(leftStartProducts[idx] * rightStartProducts[idx + 2]) | ||
} | ||
|
||
return result.toIntArray() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package leetcode_study | ||
|
||
class `Real-Reason` { | ||
fun twoSum(nums: IntArray, target: Int): IntArray { | ||
for (startIdx in 0..< nums.size - 1) { | ||
val firstNum = nums[startIdx] | ||
for (endIdx in startIdx + 1..< nums.size) { | ||
val secondNum = nums[endIdx] | ||
if (target == firstNum + secondNum) { | ||
return intArrayOf(startIdx, endIdx) | ||
} | ||
} | ||
} | ||
throw RuntimeException("There is no solution") | ||
} | ||
} |
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.
저는 candidates를 정렬해서
if ( num > target )
이면 for문 탐색을 중단하는 방식을 사용했는데, candidates length가 커질 경우를 생각해서 정렬도 고려해보시면 좋을 것 같아요!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.
오! 그 조건을 하나 더 걸어주는 것도 좋겠네요~ :)
감사합니다~