Skip to content

Commit 0cf38f0

Browse files
Create 1335-minimum-difficulty-of-a-job-schedule.java
1 parent fdb565d commit 0cf38f0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: java/1335-minimum-difficulty-of-a-job-schedule.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
Map<String, Integer> cache;
3+
4+
public int minDifficulty(int[] jobDifficulty, int d) {
5+
if(d > jobDifficulty.length)
6+
return -1;
7+
cache = new HashMap<>();
8+
return dfs(0, d, -1, jobDifficulty);
9+
}
10+
11+
private int dfs(int i, int d, int cur_max, int[] jobDiff){
12+
if(i == jobDiff.length)
13+
return (d == 0)? 0: (int)1e9;
14+
if(d == 0)
15+
return (int)1e9;
16+
String cur_state = i + "," + d + "," + cur_max;
17+
if(cache.containsKey(cur_state))
18+
return cache.get(cur_state);
19+
20+
cur_max = Math.max(cur_max, jobDiff[i]);
21+
int res = Math.min(
22+
dfs(i + 1, d, cur_max, jobDiff),
23+
cur_max + dfs(i + 1, d - 1, -1, jobDiff)
24+
);
25+
cache.put(cur_state, res);
26+
return res;
27+
}
28+
}

0 commit comments

Comments
 (0)