Skip to content

Commit f0c5939

Browse files
authored
Create 0879-profitable-schemes.kt
1 parent 278ddc4 commit f0c5939

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

kotlin/0879-profitable-schemes.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* DFS with memoization
3+
*/
4+
class Solution {
5+
fun profitableSchemes(n: Int, minProfit: Int, group: IntArray, profit: IntArray): Int {
6+
val mod = 1000000000 + 7
7+
val dp = Array(group.size) { Array(n + 1) { IntArray(minProfit + 1) {-1} } }
8+
9+
fun dfs(i: Int, n: Int, p: Int): Int {
10+
if (i == group.size)
11+
return if (p >= minProfit) 1 else 0
12+
13+
val t = minOf(p, minProfit)
14+
if (dp[i][n][t] != -1)
15+
return dp[i][n][t]
16+
17+
dp[i][n][t] = dfs(i + 1, n, t) % mod
18+
if (n - group[i] >= 0)
19+
dp[i][n][t] += dfs(i + 1, n - group[i], t + profit[i]) % mod
20+
21+
return dp[i][n][t]
22+
}
23+
24+
return dfs(0, n, 0) % mod
25+
}
26+
}

0 commit comments

Comments
 (0)