Skip to content

Commit 5d37cb5

Browse files
committed
maximum-subarray solution
1 parent 946129e commit 5d37cb5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

combination-sum/acious.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
3+
val result = mutableListOf<List<Int>>()
4+
val current = mutableListOf<Int>()
5+
6+
fun dfs(start: Int, total: Int) {
7+
if (total > target) return
8+
9+
if (total == target) {
10+
result.add(ArrayList(current))
11+
return
12+
}
13+
14+
for (i in start until candidates.size) {
15+
val num = candidates[i]
16+
current.add(num)
17+
dfs(i, total + num)
18+
current.removeAt(current.size - 1)
19+
}
20+
}
21+
22+
dfs(0, 0)
23+
return result
24+
}
25+
}

0 commit comments

Comments
 (0)