We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aaf69d7 commit b56cbc8Copy full SHA for b56cbc8
kotlin/0377-combination-sum-iv.kt
@@ -1,3 +1,4 @@
1
+//dp solution
2
class Solution {
3
fun combinationSum4(nums: IntArray, target: Int): Int {
4
val dp = IntArray(target + 1)
@@ -12,3 +13,26 @@ class Solution {
12
13
return dp[target]
14
}
15
16
+
17
+//recursion + memoization solution
18
+class Solution {
19
+ fun combinationSum4(nums: IntArray, target: Int): Int {
20
+ val dp = HashMap<Int, Int>()
21
22
+ fun dfs(sum: Int): Int {
23
+ if (sum == target) return 1
24
+ if (sum in dp) return dp[sum]!!
25
26
+ var res = 0
27
+ for (num in nums) {
28
+ if (sum + num <= target)
29
+ res += dfs(sum + num)
30
+ }
31
32
+ dp[sum] = res
33
+ return res
34
35
36
+ return dfs(0)
37
38
+}
0 commit comments