-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_256.java
More file actions
27 lines (24 loc) · 712 Bytes
/
Copy pathP_256.java
File metadata and controls
27 lines (24 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package leetcode.easy;
public class P_256 {
public int minCost(int[][] costs) {
return dfs(costs, 0, -1, new Integer[costs.length][3]);
}
private static int dfs(int[][] costs, int idx, int prev, Integer[][] dp) {
if (idx == costs.length) {
return 0;
}
if (prev != -1 && dp[idx][prev] != null) {
return dp[idx][prev];
}
int res = (int) 1e9;
for (int j = 0; j < costs[idx].length; j++) {
if (j != prev) {
res = Math.min(res, costs[idx][j] + dfs(costs, idx + 1, j, dp));
}
}
if (prev != -1) {
dp[idx][prev] = res;
}
return res;
}
}