# 1335.Minimum-Difficulty-of-a-Job-Schedule

## 1335. Minimum Difficulty of a Job Schedule

## 题目地址

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

## 题目描述

```
You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the i-th 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 in that day.

Given an array of integers jobDifficulty and an integer d. The difficulty of the i-th 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:
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:
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:
Input: jobDifficulty = [1,1,1], d = 3
Output: 3
Explanation: The schedule is one job per day. total difficulty will be 3.

Example 4:
Input: jobDifficulty = [7,1,7,1,7,1], d = 3
Output: 15

Example 5:
Input: jobDifficulty = [11,111,22,222,33,333,44,444], d = 6
Output: 843

Constraints:
1 <= jobDifficulty.length <= 300
0 <= jobDifficulty[i] <= 1000
1 <= d <= 10
```

## 代码

### Approach #1 DFS

Time: O(nnd) && Space: O(nd)

```java
class Solution {
    public int minDifficulty(int[] jobDifficulty, int d) {
        int n = jobDifficulty.length;
        if (d > n) {
            return -1;
        }

        int[][] memo = new int[d + 1][n];

        for (int i = 0; i <= d; i++) {
            Arrays.fill(memo[i], -1);
        }

        return dfs(d, 0, jobDifficulty, memo);
    }

    private int dfs(int day, int index, int[] jobs, int[][] memo) {
        final int n = jobs.length;
        if (day == 0 && index == n) {
            return 0;
        }
        if (day == 0 || index == n) {
            return Integer.MAX_VALUE;
        }

        if (memo[day][index] != -1) {
            return memo[day][index];
        }

        int curMax = jobs[index];
        int min = Integer.MAX_VALUE;
        for (int i = index; i < n; i++) {
            curMax = Math.max(jobs[i], curMax);
            int temp = dfs(day - 1, i + 1, jobs, memo);
            if (temp != Integer.MAX_VALUE) {
                min = Math.min(min, temp + curMax);
            }
        }

        memo[day][index] = min;

        return min;
    }
}
```

### Approach #2 DP

Time: O(nnd) && Space: O(nd)

```java
class Solution {
  public int minDifficulty(int[] jobDifficulty, int d) {
    int n = jobDifficulty.length;
    if (d > n)  return -1;
    int[][] dp = new int[d + 1][n + 1];

      // 初始化条件很重要
    for (int i = 0; i <= d; i++) {
       Arrays.fill(dp[i], Integer.MAX_VALUE);
    }
    dp[0][0] = 0;
    for (int day = 1; day <= d; day++) {
        for (int task = day; task <= n; task++) {
            int localMax = jobDifficulty[task-1];
            for (int s = task; s >= day; s--) {
                localMax = Math.max(localMax, jobDifficulty[s-1]);

                if (dp[day-1][s-1] != Integer.MAX_VALUE) { // 注意越界
                    dp[day][task] = Math.min(dp[day][task], dp[day-1][s-1] + localMax);
                }

            }
        }
    }

    return dp[d][n];
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/dynamic-programming/qu-jian-xing/1335.minimum-difficulty-of-a-job-schedule.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
