-
Notifications
You must be signed in to change notification settings - Fork 0
/
0077_combinations.kt
31 lines (29 loc) · 991 Bytes
/
0077_combinations.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
// https://leetcode.com/problems/combinations/description/
class Solution {
fun combine(n: Int, k: Int): List<List<Int>> {
return calculate(n, k).map { it.toList() }
}
private fun calculate(n: Int, k: Int): ArrayDeque<ArrayDeque<Int>> {
if (k == 1) {
val bigDeque = ArrayDeque<ArrayDeque<Int>>()
for (i in 1..n) {
val smallDeque = ArrayDeque<Int>()
smallDeque.add(i)
bigDeque.add(smallDeque)
}
return bigDeque
}
val bigDeque = calculate(n - 1, k - 1)
val size = bigDeque.size
for (i in 1..size) {
val smallDeque = bigDeque.removeFirst()
for (j in smallDeque.last() + 1 .. n) {
val clonedDeque = ArrayDeque<Int>()
clonedDeque.addAll(smallDeque)
clonedDeque.add(j)
bigDeque.add(clonedDeque)
}
}
return bigDeque
}
}