IPO LeetCode solution in Java

1
class Solution {
2
static int[][] dp;
3

4
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
5
dp = new int[k + 1][profits.length + 1];
6
for (int[] row : dp) {
7
Arrays.fill(row, -1);
8
}
9
return w + help(k, w, 0, profits, capital);
10
}
11

12
public int help(int k, int w, int i, int[] profits, int[] capital) {
13
if (k == 0 || i >= profits.length) return 0;
14
if (dp[k][i] != -1) return dp[k][i];
15
int res = Integer.MIN_VALUE;
16
if (capital[i] <= w) {
17
res =
18
Math.max(
19
res,
20
Math.max(
21
profits[i] + help(k - 1, w + profits[i], i + 1, profits, capital),
22
help(k, w, i + 1, profits, capital)));
23
} else {
24
res = Math.max(res, help(k, w, i + 1, profits, capital));
25
}
26
return dp[k][i] = res;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0