All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0216_combination_sum_iii.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
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 }.toMutableList())
            return
        }
        for (i in index until 10) {
            tmp.add(i)
            comb(a, k, n - i, i + 1, tmp)
            tmp.removeAt(tmp.size - 1)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy