-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMinimum Difficulty of a Job Schedule.java
39 lines (33 loc) · 1.26 KB
/
Minimum Difficulty of a Job Schedule.java
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
32
33
34
35
36
37
38
39
class Solution {
// Given an array, cut it into d contiguous subarray and return the minimum sum of max of each subarray.
public int minDifficulty(int[] jobDifficulty, int d) {
if(d>jobDifficulty.length){
return -1;
}
int[][] memo = new int[d+1][jobDifficulty.length];
for(int[] m : memo){
Arrays.fill(m, -1);
}
return getMinDays(jobDifficulty, d, memo, 0);
}
private int getMinDays(int[] jobDifficulty, int d, int[][] memo, int idx){
if(d==1){
int max=0;
while(idx < jobDifficulty.length){
max=Math.max(max, jobDifficulty[idx]);
idx++;
}
return max;
}
if(memo[d][idx] != -1) return memo[d][idx];
int max=0;
int res=Integer.MAX_VALUE;
// [6,5,4,3,2,1], d=5 => we don't want the cut at 4th position in the array because we won't be able to divide it into 5 parts
for(int i=idx; i<jobDifficulty.length-d+1; i++){
max = Math.max(max, jobDifficulty[i]);
res = Math.min(res, max + getMinDays(jobDifficulty, d-1, memo, i+1));
}
memo[d][idx]=res;
return memo[d][idx];
}
}