-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifficulty.java
More file actions
38 lines (36 loc) · 1.31 KB
/
Difficulty.java
File metadata and controls
38 lines (36 loc) · 1.31 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
32
33
34
35
36
37
import java.util.Arrays;
class Solution {
public int minDifficulty(int[] jobDifficulty, int days) {
int length = jobDifficulty.length;
if (days > length) return -1;
int[][] minDifficulties = new int[days][length];
for (int i = 1; i < days; ++i) {
Arrays.fill(minDifficulties[i], Integer.MAX_VALUE);
}
int maxDiff = 0;
int i = 0;
while (i <= length - days) {
maxDiff = Math.max(maxDiff, jobDifficulty[i]);
minDifficulties[0][i] = maxDiff;
++i;
}
int currentDay = 1;
while (currentDay < days) {
int to = currentDay;
while (to <= length - days + currentDay) {
int currentJobDifficulty = jobDifficulty[to];
int result = Integer.MAX_VALUE;
int j = to - 1;
while (j >= currentDay - 1) {
result = Math.min(result, minDifficulties[currentDay - 1][j] + currentJobDifficulty);
currentJobDifficulty = Math.max(currentJobDifficulty, jobDifficulty[j]);
--j;
}
minDifficulties[currentDay][to] = result;
++to;
}
++currentDay;
}
return minDifficulties[days - 1][length - 1];
}
}