-
Notifications
You must be signed in to change notification settings - Fork 20
/
Combinations.kt
71 lines (63 loc) · 1.61 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
*
* For example,
* If n = 4 and k = 2, a solution is:
*
* [
* [2,4],
* [3,4],
* [2,3],
* [1,2],
* [1,3],
* [1,4],
* ]
*/
class Combinations {
// Iterative solution.
// Accepted.
fun combine(n: Int, k: Int): List<List<Int>> {
var results = mutableListOf<List<Int>>()
if (n == 0 || k == 0 || k > n) {
return results
}
(1..n + 1 - k).mapTo(results) {
listOf(it)
}
for (i in 2..k) {
val tmp = mutableListOf<List<Int>>()
for (list in results) {
for (m in list[list.size - 1] + 1..n - (k - i)) {
val newList = mutableListOf<Int>()
newList.addAll(list)
newList.add(m)
tmp.add(newList)
}
}
results = tmp
}
return results
}
// Recursive solution.
// Accepted.
/*fun combine(n: Int, k: Int): List<List<Int>> {
val results = mutableListOf<List<Int>>()
if (n == 0 || k == 0 || k > n) {
return results
}
if (k == 1) {
(1..n).mapTo(results) {
listOf(it)
}
return results
}
for (list in combine(n, k - 1)) {
for (i in list[list.size - 1] until n) {
val tmp = mutableListOf<Int>()
tmp.addAll(list)
tmp.add(i + 1)
results.add(tmp)
}
}
return results*/
}