forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.kt
34 lines (30 loc) · 965 Bytes
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package g0001_0100.s0077_combinations
// #Medium #Backtracking #Algorithm_I_Day_11_Recursion_Backtracking
// #2022_09_24_Time_244_ms_(100.00%)_Space_40.5_MB_(99.00%)
import java.util.Stack
class Solution {
fun combine(n: Int, k: Int): List<List<Int>> {
val ans: MutableList<List<Int>> = ArrayList()
// Boundary case
if (n > 20 || k < 1 || k > n) {
return ans
}
backtrack(ans, n, k, 1, Stack())
return ans
}
private fun backtrack(ans: MutableList<List<Int>>, n: Int, k: Int, s: Int, stack: Stack<Int>) {
// Base case
// If k becomes 0
if (k == 0) {
ans.add(ArrayList(stack))
return
}
// Start with s till n-k+1
for (i in s..n - k + 1) {
stack.push(i)
// Update start for recursion and decrease k by 1
backtrack(ans, n, k - 1, i + 1, stack)
stack.pop()
}
}
}