Minimum Difficulty of a Job Schedule — LeetCode 1335 Python Solution
- Problem
- #1335
- Pattern
- Dynamic Programming
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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).
Example
- 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.
Python solution
class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n = len(jobDifficulty)
f = [[inf] * (d + 1) for _ in range(n + 1)]
f[0][0] = 0
for i in range(1, n + 1):
for j in range(1, min(d + 1, i + 1)):
mx = 0
for k in range(i, 0, -1):
mx = max(mx, jobDifficulty[k - 1])
f[i][j] = min(f[i][j], f[k - 1][j - 1] + mx)
return -1 if f[n][d] >= inf else f[n][d]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n^2 \times d) |
| Space | O(n \times d) auxiliary |
Pattern: Dynamic Programming
Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 1335. Minimum Difficulty of a Job Schedule is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.
The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1335. Minimum Difficulty of a Job Schedule?
- LeetCode 1335. Minimum Difficulty of a Job Schedule is rated Hard on LeetCode.
- What is the time complexity of LeetCode 1335. Minimum Difficulty of a Job Schedule?
- The Python solution on this page runs in O(n^2 \times d).
- What is the space complexity of LeetCode 1335. Minimum Difficulty of a Job Schedule?
- The Python solution on this page uses O(n \times d) auxiliary space.
- What topics does LeetCode 1335. Minimum Difficulty of a Job Schedule cover?
- LeetCode 1335. Minimum Difficulty of a Job Schedule is tagged Array and Dynamic Programming on LeetCode.