Skip to content

Commit 0af425f

Browse files
Create 2742-painting-the-walls.java
1 parent 551416d commit 0af425f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: java/2742-painting-the-walls.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
Map<String, Integer> dp;
3+
public int paintWalls(int[] cost, int[] time) {
4+
int n = cost.length;
5+
dp = new HashMap<>();
6+
return dfs(0, cost.length, cost, time);
7+
}
8+
private int dfs(int i, int remain, int[] cost, int[] time){
9+
if(remain <= 0)
10+
return 0;
11+
if(i == cost.length)
12+
return (int) 1e9;
13+
String s = i + "," + remain;
14+
if(dp.containsKey(s))
15+
return dp.get(s);
16+
17+
int paint = cost[i] + dfs(i+1, remain-1-time[i], cost, time);
18+
int skip = dfs(i+1, remain, cost, time);
19+
dp.put(s, Math.min(paint, skip));
20+
return dp.get(s);
21+
}
22+
}

0 commit comments

Comments
 (0)