你需要制定一份 d 天的工作计划表。工作之间存在依赖,要想执行第 i 项工作,你必须完成全部 j 项工作( 0 <= j < i)。
你每天 至少 需要完成一项任务。工作计划的总难度是这 d 天每一天的难度之和,而一天的工作难度是当天应该完成工作的最大难度。
给你一个整数数组 jobDifficulty 和一个整数 d,分别代表工作难度和需要计划的天数。第 i 项工作的难度是 jobDifficulty[i]。
返回整个工作计划的 最小难度 。如果无法制定工作计划,则返回 -1 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-difficulty-of-a-job-schedule
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
动态规划
import java.util.Arrays;
class Solution {
public int minDifficulty(int[] jobDifficulty, int d) {
int n = jobDifficulty.length;
if (d > n) {
return -1;
}
int[][] dp = new int[n][d];
int INF = Integer.MAX_VALUE - 1000;
dp[0][0] = jobDifficulty[0];
int max = jobDifficulty[0];
for (int i = 1; i < n; i++) {
max = Math.max(max, jobDifficulty[i]);
dp[i][0] = max;
for (int j = 1; j <= Math.min(d - 1, i); j++) {
int regionMax = 0;
dp[i][j] = INF;
for (int k = i; k >= j; k--) {
regionMax = Math.max(regionMax, jobDifficulty[k]);
dp[i][j] = Math.min(dp[i][j], dp[k - 1][j - 1] + regionMax);
}
}
}
return dp[n - 1][d - 1];
}
public int minDifficulty(int[] jobDifficulty, int d) {
int n = jobDifficulty.length;
if (n < d) {
return -1;
}
int INF = Integer.MAX_VALUE - 1000;
int[][] dp = new int[d][n];
for (int i = 0; i < d; ++i) {
Arrays.fill(dp[i], INF);
}
int max = 0;
for (int i = 0; i < n; ++i) {
max = Math.max(max, jobDifficulty[i]);
dp[0][i] = max;
}
for (int i = 1; i < d; ++i) {
for (int j = i; j < n; ++j) {
int regionMax = 0;
for (int k = j; k >= i; --k) {
regionMax = Math.max(regionMax, jobDifficulty[k]);
dp[i][j] = Math.min(dp[i][j], regionMax + dp[i - 1][k - 1]);
}
}
}
return dp[d - 1][n - 1];
}
public int minDifficulty(int[] jobDifficulty, int d) {
int n = jobDifficulty.length;
if (n < d) {
return -1;
}
int INF = Integer.MAX_VALUE - 1000;
int[] dp = new int[n];
Arrays.fill(dp, INF);
int max = 0;
for (int i = 0; i < n; ++i) {
max = Math.max(max, jobDifficulty[i]);
dp[i] = max;
}
for (int i = 1; i < d; ++i) {
int[] helper = new int[n];
for (int j = i; j < n; ++j) {
int regionMax = 0;
helper[j] = INF;
for (int k = j; k >= i; --k) {
regionMax = Math.max(regionMax, jobDifficulty[k]);
helper[j] = Math.min(helper[j], regionMax + dp[k - 1]);
}
}
System.arraycopy(helper, 0, dp, 0, n);
}
return dp[n - 1];
}
}
单调栈
标签:1335,max,Math,最低,int,难度,jobDifficulty,dp,regionMax
From: https://www.cnblogs.com/tianyiya/p/17404316.html