-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathminDifficulty.java
More file actions
31 lines (29 loc) · 1.04 KB
/
minDifficulty.java
File metadata and controls
31 lines (29 loc) · 1.04 KB
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
28
29
30
31
class Solution {
public int minDifficulty(int[] jobDifficulty, int d) {
int n = jobDifficulty.length;
if(d > n) return -1;
int[][] dp = new int[n][d + 1];
for(int[] e: dp) {
Arrays.fill(e, -1);
}
int result = dfs(jobDifficulty, d, 0, dp);
return result == Integer.MAX_VALUE ? -1 : result;
}
private int dfs(int[] jobDifficulty, int d, int index, int[][] dp) {
if(index >= jobDifficulty.length && d == 0) return 0;
if(index >= jobDifficulty.length || d == 0) return Integer.MAX_VALUE;
if(dp[index][d] >= 0) {
return dp[index][d];
}
int max = 0;
int result = Integer.MAX_VALUE;
for(int i = index; i < jobDifficulty.length; i++) {
int next = dfs(jobDifficulty, d - 1, i + 1, dp);
max = Math.max(max, jobDifficulty[i]);
if(next == Integer.MAX_VALUE) continue;
result = Math.min(result, max + next);
}
dp[index][d] = result;
return result;
}
}