> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Difficulty of a Job Schedule

> Tested Python solution for LeetCode 1335 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1335, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1335   # by problem number
lcpy gen -s minimum_difficulty_of_a_job_schedule   # by problem name
```

## 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).

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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/01/16/untitled.png)

```
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
```

```
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.
```

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_difficulty_of_a_job_schedule/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_difficulty_of_a_job_schedule/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from itertools import accumulate


class Solution:
    def min_difficulty(self, job_difficulty: list[int], d: int) -> int:
        n = len(job_difficulty)
        if n < d:
            return -1
        inf = 10**9
        dp = list(accumulate(job_difficulty, max))
        for day in range(1, d):
            ndp = [inf] * n
            for i in range(day, n):
                run_max = 0
                for j in range(i, day - 1, -1):
                    run_max = max(run_max, job_difficulty[j])
                    ndp[i] = min(ndp[i], dp[j - 1] + run_max)
            dp = ndp
        return dp[n - 1]
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.