前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >1335. Minimum Difficulty of a Job Schedule

1335. Minimum Difficulty of a Job Schedule

作者头像
ppxai
发布于 2023-11-18 00:29:50
发布于 2023-11-18 00:29:50
16800
代码可运行
举报
文章被收录于专栏:皮皮星球皮皮星球
运行总次数:0
代码可运行

1335. 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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: jobDifficulty = [1,1,1], d = 3
Output: 3
Explanation: The schedule is one job per day. total difficulty will be 3.

Example 4:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: jobDifficulty = [7,1,7,1,7,1], d = 3
Output: 15

Example 5:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

思路:

题目意思是给定一个数组,每个元素代表一个任务的完成时间,再给出一个天数,让分配这些任务,每天完成的任务数不限制,耗时按照最大的计算,保证所有天数加起来耗时最小。 可以从划分的角度考虑,对于j个任务,第k天完成的任务的最小值,等于前j-k天完成的最小值,加上后面任务完成所需的最大值。那状态转移方程就可以是: dp[i][k] = min(dp[j][k-1] + max(jobs[j+1, i])) (k - 1 <= j < i)。j个任务的取值范围有下界是因为任务数量一定要大于天数,不然题目是无解的。

代码:

golang:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func minDifficulty(jobs []int, d int) int {
    m := len(jobs)
    if m < d {
        return -1
    }
    
    var dp = make([][]int, m + 1)
    for i := range dp {
        dp[i] = make([]int, d + 1)
        for j := range dp[i] {
            dp[i][j] = math.MaxInt16
        }
    }
    
    dp[0][0] = 0
    for i := 1; i <= m; i++ {
        for k := 1; k <= d; k++ {
            maxDay := 0
            for j := i - 1; j >= k - 1; j-- {
                maxDay = max(maxDay, jobs[j])
                dp[i][k] = min(dp[i][k], dp[j][k-1] + maxDay)
            } 
        }
    }
    
    return dp[m][d]
    
}

func max(i, j int) int {
    if i > j {
        return i
    }
    return j
}

func min(i, j int) int {
    if i < j {
        return i
    }
    return j
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【DP】983. Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
echobingo
2019/05/14
3290
LWC 51:683. K Empty Slots
该文讲述了通过使用树集合来查找两个花朵之间的间隔为k的天数,同时维护一个有序状态,快速找到符合要求的天数。
用户1147447
2018/01/02
8190
Dynamic Programming - 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
ppxai
2020/09/23
3370
Codeforces 789A Anastasia and pebbles(数学,思维题)
A. Anastasia and pebbles time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began
Angel_Kitty
2018/04/08
6560
POJ-2336 Ferry Loading II(简单DP)
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a g
ShenduCC
2018/04/26
5260
HDU3572_Task Schedule(网络流最大流)[通俗易懂]
工厂有m台机器,须要做n个任务。对于一个任务i。你须要花费一个机器Pi天,并且,開始做这个任务的时间要>=Si,完毕这个任务的时间<=Ei。
全栈程序员站长
2022/07/08
2330
Codeforces 839A Arya and Bran【暴力】
A. Arya and Bran time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies. At f
Angel_Kitty
2018/04/09
7150
LeetCode Weekly Contest 38解题思路
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/73718744
用户1147447
2019/05/26
4460
ZOJ 3941 Kpop Music Party(省赛, 贪心)
Kpop Music Party ---- Time Limit: 2 Seconds      Memory Limit: 65536 KB ---- Marjar University often hosts Kpop music festival. A Kpop music festival will last several days. During a Kpop festival, there will be a Kpop party every day. Kpop music is very p
ShenduCC
2018/04/26
7900
LeetCode Weekly Contest 30解题思路
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/71023781
用户1147447
2019/05/26
4340
F2. Animal Observation (hard version)
time limit per test:3 seconds memory limit per test:512 megabytes inputstandard input outputstandard output
某些人
2020/04/09
3420
图论--网络流--最大流 HDU 3572 Task Schedule(限流建图,超级源汇)
Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days. Now she wonders whether he has a feasible schedule to finish all the tasks in time. She turns to you for help.
风骨散人Chiam
2020/10/28
4160
CodeForces 732B Cormen — The Best Friend Of a Man
B. Cormen — The Best Friend Of a Man time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troub
ShenduCC
2018/04/27
1.1K0
CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)
ACM思维题训练集合 A new Berland businessman Vitaly is going to open a household appliances’ store. All he’s got to do now is to hire the staff.
风骨散人Chiam
2020/10/29
4200
CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)
动态规划之区间DP
区间dp,顾名思义,在区间上dp,大多数题目的状态都是由区间(类似于dp[l][r]这种形式)构成的,就是我们可以把大区间转化成小区间来处理,然后对小区间处理后再回溯的求出大区间的值,主要的方法有两种,记忆化搜索和递推。
王脸小
2019/12/07
4560
HOJ 2317 Pimp My Ride(状态压缩DP)
Pimp My Ride My Tags (Edit) Source : TUD 2005 Time limit : 3 sec Memory limit : 64 M Submitted : 63, Accepted : 34 Today, there are quite a few cars, motorcycles, trucks and other vehicles out there on the streets that would seriously need
ShenduCC
2018/04/26
6410
Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. Howeve
Angel_Kitty
2018/04/09
7700
Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
Codeforces 706B Interesting drink
B. Interesting drink time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the fa
Angel_Kitty
2018/04/08
1K0
Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
卡尔曼和玻尔兹曼谁曼
2019/02/05
5740
2018-2019 ICPC, NEERC, Southern Subregional Contest D. Garbage Disposal
Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it.
glm233
2020/09/28
3590
相关推荐
【DP】983. Minimum Cost For Tickets
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档