题目描述
你需要制定一份 d
天的工作计划表。工作之间存在依赖,要想执行第 i
项工作,你必须完成全部 j
项工作( 0 <= j < i
)。
你每天 至少 需要完成一项任务。工作计划的总难度是这 d
天每一天的难度之和,而一天的工作难度是当天应该完成工作的最大难度。
给你一个整数数组 jobDifficulty
和一个整数 d
,分别代表工作难度和需要计划的天数。第 i
项工作的难度是 jobDifficulty[i]
。
返回整个工作计划的 最小难度 。如果无法制定工作计划,则返回 -1 。
示例 1:
输入:jobDifficulty = [6,5,4,3,2,1], d = 2
输出:7
解释:第一天,您可以完成前 5 项工作,总难度 = 6.
第二天,您可以完成最后一项工作,总难度 = 1.
计划表的难度 = 6 + 1 = 7
示例 2:
输入:jobDifficulty = [9,9,9], d = 4
输出:-1
解释:就算你每天完成一项工作,仍然有一天是空闲的,你无法制定一份能够满足既定工作时间的计划表。
示例 3:
输入:jobDifficulty = [1,1,1], d = 3
输出:3
解释:工作计划为每天一项工作,总难度为 3 。
示例 4:
输入:jobDifficulty = [7,1,7,1,7,1], d = 3
输出:15
示例 5:
输入:jobDifficulty = [11,111,22,222,33,333,44,444], d = 6
输出:843
提示:
1 <= jobDifficulty.length <= 300
0 <= jobDifficulty[i] <= 1000
1 <= d <= 10
解法
方法一:动态规划
我们定义 $f[i][j]$ 表示完成前 $i$ 项工作,且一共用了 $j$ 天的最小难度。初始时 $f[0][0] = 0$,其余 $f[i][j]$ 均为 $\infty$。
考虑第 $j$ 天的工作安排,我们可以枚举第 $j$ 天完成的工作 $[k,..i]$,那么有状态转移方程:
$$
f[i][j] = \min_{k \in [1,i]} {f[k-1][j-1] + \max_{k \leq t \leq i} {jobDifficulty[t-1]}}
$$
最终答案即为 $f[n][d]$。
时间复杂度 $O(n^2 \times d)$,空间复杂度 $O(n \times d)$。其中 $n$ 和 $d$ 分别为工作数量和需要计划的天数。
1
2
3
4
5
6
7
8
9
10
11
12 | 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]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | class Solution {
public int minDifficulty(int[] jobDifficulty, int d) {
final int inf = 1 << 30;
int n = jobDifficulty.length;
int[][] f = new int[n + 1][d + 1];
for (var g : f) {
Arrays.fill(g, inf);
}
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= Math.min(d, i); ++j) {
int mx = 0;
for (int k = i; k > 0; --k) {
mx = Math.max(mx, jobDifficulty[k - 1]);
f[i][j] = Math.min(f[i][j], f[k - 1][j - 1] + mx);
}
}
}
return f[n][d] >= inf ? -1 : f[n][d];
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class Solution {
public:
int minDifficulty(vector<int>& jobDifficulty, int d) {
int n = jobDifficulty.size();
int f[n + 1][d + 1];
memset(f, 0x3f, sizeof(f));
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= min(d, i); ++j) {
int mx = 0;
for (int k = i; k; --k) {
mx = max(mx, jobDifficulty[k - 1]);
f[i][j] = min(f[i][j], f[k - 1][j - 1] + mx);
}
}
}
return f[n][d] == 0x3f3f3f3f ? -1 : f[n][d];
}
};
|
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 | func minDifficulty(jobDifficulty []int, d int) int {
n := len(jobDifficulty)
f := make([][]int, n+1)
const inf = 1 << 30
for i := range f {
f[i] = make([]int, d+1)
for j := range f[i] {
f[i][j] = inf
}
}
f[0][0] = 0
for i := 1; i <= n; i++ {
for j := 1; j <= min(d, i); j++ {
mx := 0
for k := i; k > 0; k-- {
mx = max(mx, jobDifficulty[k-1])
f[i][j] = min(f[i][j], f[k-1][j-1]+mx)
}
}
}
if f[n][d] == inf {
return -1
}
return f[n][d]
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | function minDifficulty(jobDifficulty: number[], d: number): number {
const n = jobDifficulty.length;
const inf = 1 << 30;
const f: number[][] = new Array(n + 1).fill(0).map(() => new Array(d + 1).fill(inf));
f[0][0] = 0;
for (let i = 1; i <= n; ++i) {
for (let j = 1; j <= Math.min(d, i); ++j) {
let mx = 0;
for (let k = i; k > 0; --k) {
mx = Math.max(mx, jobDifficulty[k - 1]);
f[i][j] = Math.min(f[i][j], f[k - 1][j - 1] + mx);
}
}
}
return f[n][d] < inf ? f[n][d] : -1;
}
|