LeetCode 1335. Minimum Difficulty of a Job Schedule

Question

You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i).

You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day.

You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i].

Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.

Example 1:

lc1335-example1

1
2
3
4
5
Input: jobDifficulty = [6,5,4,3,2,1], d = 2
Output: 7
Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
Second day you can finish the last job, total difficulty = 1.
The difficulty of the schedule = 6 + 1 = 7

Example 2:

1
2
3
Input: jobDifficulty = [9,9,9], d = 4
Output: -1
Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.

Example 3:

1
2
3
Input: jobDifficulty = [1,1,1], d = 3
Output: 3
Explanation: The schedule is one job per day. total difficulty will be 3.

Constraints:

  • 1 <= jobDifficulty.length <= 300
  • 0 <= jobDifficulty[i] <= 1000
  • 1 <= d <= 10

Source: https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/

Solution

In this problem, jobs are dependent so we can only complete jobs sequentially. For a day, we only need to consider the start job index m and the end job index n, which means on that day we complete all jobs between job m and job n (inclusive).

cache[i][j] represents the minimum sum of difficulties when completing the first j+1 jobs in i+1 days.

In the following code block, we traverse the start job index from high to low. It gets the max difficulty of jobs on that day more efficiently.

1
2
3
4
for (int k = j; k >= i; k--) {
dayMax = Math.max(dayMax, jobDifficulty[k]);
cache[i][j] = Math.min(cache[i][j], cache[i - 1][k - 1] + dayMax);
}
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
// bottom-up 2D DP, time complexity O(nnd), space complexity O(nd)
public int minDifficulty(int[] jobDifficulty, int d) {
// parameter validation
if (d < 1 || jobDifficulty == null || jobDifficulty.length < 1 || jobDifficulty.length < d) {
return -1;
}
int n = jobDifficulty.length; // number of jobs
// cache for DP
// cache[i][j] represents the min sum of difficulties when completing jobs up to j job on i day
int[][] cache = new int[d][n];
cache[0][0] = jobDifficulty[0];
for (int j = 1; j < n; j++) {
cache[0][j] = Math.max(cache[0][j - 1], jobDifficulty[j]);
}
for (int i = 1; i < d; i++) {
// only use a half of the cache because we must complete at least one job per day
for (int j = i; j < n; j++) {
cache[i][j] = Integer.MAX_VALUE;
int dayMax = Integer.MIN_VALUE;
// k is the index of start job on i day
for (int k = j; k >= i; k--) {
dayMax = Math.max(dayMax, jobDifficulty[k]);
cache[i][j] = Math.min(cache[i][j], cache[i - 1][k - 1] + dayMax);
}
}
}
return cache[d - 1][n - 1];
}
Author

Weihao Ye

Posted on

2022-01-08

Updated on

2022-01-10

Licensed under