2
// Given an array, cut it into d contiguous subarray and return the minimum sum of max of each4
public int minDifficulty(int[] jobDifficulty, int d) {5
if (d > jobDifficulty.length) {9
int[][] memo = new int[d + 1][jobDifficulty.length];10
for (int[] m : memo) {14
return getMinDays(jobDifficulty, d, memo, 0);17
private int getMinDays(int[] jobDifficulty, int d, int[][] memo, int idx) {20
while (idx < jobDifficulty.length) {21
max = Math.max(max, jobDifficulty[idx]);27
if (memo[d][idx] != -1) return memo[d][idx];30
int res = Integer.MAX_VALUE;31
// [6,5,4,3,2,1], d=5 => we don't want the cut at 4th position in the array because we won't be32
// able to divide it into 5 parts33
for (int i = idx; i < jobDifficulty.length - d + 1; i++) {34
max = Math.max(max, jobDifficulty[i]);35
res = Math.min(res, max + getMinDays(jobDifficulty, d - 1, memo, i + 1));