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:
1 | Input: jobDifficulty = [6,5,4,3,2,1], d = 2 |
Example 2:
1 | Input: jobDifficulty = [9,9,9], d = 4 |
Example 3:
1 | Input: jobDifficulty = [1,1,1], d = 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 | for (int k = j; k >= i; k--) { |
1 | // bottom-up 2D DP, time complexity O(nnd), space complexity O(nd) |
LeetCode 1335. Minimum Difficulty of a Job Schedule
http://yenotes.org/2022/01/08/LeetCode-1335-Minimum-Difficulty-of-a-Job-Schedule/