Skip to content

Commit b4dc59f

Browse files
authored
Create Painting the Walls.java
1 parent 8392986 commit b4dc59f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Hard/Painting the Walls.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
3+
private static final int INVALID_VALUE = 1000_000_000;
4+
5+
public int paintWalls(int[] cost, int[] time) {
6+
int n = cost.length;
7+
Integer[][] dp = new Integer[n][n + 1];
8+
return compute(0, n, cost, time, dp, n);
9+
}
10+
11+
private int compute(int idx, int remain, int[] cost, int[] time, Integer[][] dp, int n) {
12+
if (remain <= 0) {
13+
return 0;
14+
}
15+
if (idx == n) {
16+
return INVALID_VALUE;
17+
}
18+
if (dp[idx][remain] != null) {
19+
return dp[idx][remain];
20+
}
21+
int paint = cost[idx] + compute(idx + 1, remain - 1 - time[idx], cost, time, dp, n);
22+
int noPaint = compute(idx + 1, remain, cost, time, dp, n);
23+
dp[idx][remain] = Math.min(paint, noPaint);
24+
return dp[idx][remain];
25+
}
26+
}

0 commit comments

Comments
 (0)