g0201_0300.s0216_combination_sum_iii.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0201_0300.s0216_combination_sum_iii
// #Medium #Array #Backtracking #Udemy_Backtracking/Recursion
// #2022_10_25_Time_175_ms_(90.91%)_Space_34.4_MB_(72.73%)
class Solution {
fun combinationSum3(k: Int, n: Int): List> {
val a = MutableList(0) { MutableList(0) { 0 } }
comb(a, k, n, 1, MutableList(0) { 0 })
return a
}
fun comb(a: MutableList>, k: Int, n: Int, index: Int, tmp: MutableList) {
if (n < 0) {
return
}
if (tmp.size > k) {
return
}
if (n == 0 && tmp.size == k) {
a.add(tmp.map { it -> it }.toMutableList())
return
}
for (i in index until 10) {
tmp.add(i)
comb(a, k, n - i, i + 1, tmp)
tmp.removeAt(tmp.size - 1)
}
}
}