Skip to content

[이유진] 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

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions combination-sum/Real-Reason.kt
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]
Copy link
Contributor

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가 커질 경우를 생각해서 정렬도 고려해보시면 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오! 그 조건을 하나 더 걸어주는 것도 좋겠네요~ :)
감사합니다~

nums.add(num)
dfs(candidates, target, i, total + num, nums, result)
nums.removeLast()
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 ArrayDeque가 익숙하지 않은데, removeLast() 메소드 덕분에 가독성이 좋은거 같네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

kotlin 에서는 java 의 Stack 대신에 ArrayDeque 를 사용하는 것을 권장하더라구요 :)
그래서 Stack.pop() 메서드를 대신하는 것이 ArrayDeque.removeLast() 입니다

}
}
29 changes: 29 additions & 0 deletions product-of-array-except-self/Real-Reason.kt
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

rightStartProducts의 0번째에 삽입하는 대신 add(rightStartProducts.first() * rightNum)로 삽입하고, 결과 계산을 할 때 반대로 조회하면 성능을 조금 더 개선할 수 있을 거 같아요 :)

혹은 leftStartProducts, rightStartProducts에 add() 하는 대신 result에 즉시 production을 하는 방법도 고려해보시면 좋을 거 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()
}
16 changes: 16 additions & 0 deletions two-sum/Real-Reason.kt
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")
}
}
Loading