-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1335-MinimumDifficultyOfAJobSchedule.go
More file actions
86 lines (75 loc) · 3.1 KB
/
1335-MinimumDifficultyOfAJobSchedule.go
File metadata and controls
86 lines (75 loc) · 3.1 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
// 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 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.
// Example 1:
// <img src="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
// 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.
// Constraints:
// 1 <= jobDifficulty.length <= 300
// 0 <= jobDifficulty[i] <= 1000
// 1 <= d <= 10
import "fmt"
func minDifficulty(jobDifficulty []int, d int) int {
n := len(jobDifficulty)
if n < d { // 任务比工作天数据还少,无法制定工作计划
return -1
}
min := func (x, y int) int { if x < y { return x; }; return y; }
max := func (x, y int) int { if x > y { return x; }; return y; }
dp, inf := make([]int, n), 1 << 32 -1
dp[0] = jobDifficulty[0]
for i := 1; i < n; i++ {
dp[i] = max(dp[i-1], jobDifficulty[i])
}
for i := 1; i < d; i++ {
for j := n - 1; j >= i; j-- {
dp[j] = inf
mx := 0
for k := j; k >= i; k-- {
mx = max(mx, jobDifficulty[k])
dp[j] = min(dp[j], dp[k-1] + mx)
}
}
}
return dp[n-1]
}
func main() {
// Example 1:
// <img src="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
fmt.Println(minDifficulty([]int{6,5,4,3,2,1}, 2)) // 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.
fmt.Println(minDifficulty([]int{9,9,9}, 4)) // -1
// Example 3:
// Input: jobDifficulty = [1,1,1], d = 3
// Output: 3
// Explanation: The schedule is one job per day. total difficulty will be 3.
fmt.Println(minDifficulty([]int{1,1,1}, 3)) // 3
}