Skip to content

Commit 8b6b789

Browse files
committed
添加 problem 1235
1 parent 3991404 commit 8b6b789

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
type job struct {
6+
startTime int
7+
endTime int
8+
profit int
9+
}
10+
11+
func jobScheduling(startTime []int, endTime []int, profit []int) int {
12+
jobs, dp := []job{}, make([]int, len(startTime))
13+
for i := 0; i < len(startTime); i++ {
14+
jobs = append(jobs, job{startTime: startTime[i], endTime: endTime[i], profit: profit[i]})
15+
}
16+
sort.Sort(sortJobs(jobs))
17+
dp[0] = jobs[0].profit
18+
for i := 1; i < len(jobs); i++ {
19+
low, high := 0, i-1
20+
for low < high {
21+
mid := low + (high-low)>>1
22+
if jobs[mid+1].endTime <= jobs[i].startTime {
23+
low = mid + 1
24+
} else {
25+
high = mid
26+
}
27+
}
28+
if jobs[low].endTime <= jobs[i].startTime {
29+
dp[i] = max(dp[i-1], dp[low]+jobs[i].profit)
30+
} else {
31+
dp[i] = max(dp[i-1], jobs[i].profit)
32+
}
33+
}
34+
return dp[len(startTime)-1]
35+
}
36+
37+
type sortJobs []job
38+
39+
func (s sortJobs) Len() int {
40+
return len(s)
41+
}
42+
func (s sortJobs) Less(i, j int) bool {
43+
if s[i].endTime == s[j].endTime {
44+
return s[i].profit < s[j].profit
45+
}
46+
return s[i].endTime < s[j].endTime
47+
}
48+
func (s sortJobs) Swap(i, j int) {
49+
s[i], s[j] = s[j], s[i]
50+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1235 struct {
9+
para1235
10+
ans1235
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1235 struct {
16+
startTime []int
17+
endTime []int
18+
profit []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans1235 struct {
24+
one int
25+
}
26+
27+
func Test_Problem1235(t *testing.T) {
28+
29+
qs := []question1235{
30+
31+
question1235{
32+
para1235{[]int{1, 2, 3, 3}, []int{3, 4, 5, 6}, []int{50, 10, 40, 70}},
33+
ans1235{120},
34+
},
35+
36+
question1235{
37+
para1235{[]int{1, 2, 3, 4, 6}, []int{3, 5, 10, 6, 9}, []int{20, 20, 100, 70, 60}},
38+
ans1235{150},
39+
},
40+
41+
question1235{
42+
para1235{[]int{1, 1, 1}, []int{2, 3, 4}, []int{5, 6, 4}},
43+
ans1235{6},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 1235------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans1235, q.para1235
51+
fmt.Printf("【input】:%v 【output】:%v\n", p, jobScheduling(p.startTime, p.endTime, p.profit))
52+
}
53+
fmt.Printf("\n\n\n")
54+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# [1235. Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)
2+
3+
4+
## 题目:
5+
6+
We have `n` jobs, where every job is scheduled to be done from `startTime[i]` to `endTime[i]`, obtaining a profit of `profit[i]`.
7+
8+
You're given the `startTime` , `endTime` and `profit` arrays, you need to output the maximum profit you can take such that there are no 2 jobs in the subset with overlapping time range.
9+
10+
If you choose a job that ends at time `X` you will be able to start another job that starts at time `X`.
11+
12+
**Example 1:**
13+
14+
![https://assets.leetcode.com/uploads/2019/10/10/sample1_1584.png](https://assets.leetcode.com/uploads/2019/10/10/sample1_1584.png)
15+
16+
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
17+
Output: 120
18+
Explanation: The subset chosen is the first and fourth job.
19+
Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
20+
21+
**Example 2:**
22+
23+
![https://assets.leetcode.com/uploads/2019/10/10/sample22_1584.png](https://assets.leetcode.com/uploads/2019/10/10/sample22_1584.png)
24+
25+
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
26+
Output: 150
27+
Explanation: The subset chosen is the first, fourth and fifth job.
28+
Profit obtained 150 = 20 + 70 + 60.
29+
30+
**Example 3:**
31+
32+
![https://assets.leetcode.com/uploads/2019/10/10/sample3_1584.png](https://assets.leetcode.com/uploads/2019/10/10/sample3_1584.png)
33+
34+
Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
35+
Output: 6
36+
37+
**Constraints:**
38+
39+
- `1 <= startTime.length == endTime.length == profit.length <= 5 * 10^4`
40+
- `1 <= startTime[i] < endTime[i] <= 10^9`
41+
- `1 <= profit[i] <= 10^4`
42+
43+
## 题目大意
44+
45+
46+
你打算利用空闲时间来做兼职工作赚些零花钱。这里有 n 份兼职工作,每份工作预计从 startTime[i] 开始到 endTime[i] 结束,报酬为 profit[i]。给你一份兼职工作表,包含开始时间 startTime,结束时间 endTime 和预计报酬 profit 三个数组,请你计算并返回可以获得的最大报酬。注意,时间上出现重叠的 2 份工作不能同时进行。如果你选择的工作在时间 X 结束,那么你可以立刻进行在时间 X 开始的下一份工作。
47+
48+
49+
提示:
50+
51+
- 1 <= startTime.length == endTime.length == profit.length <= 5 * 10^4
52+
- 1 <= startTime[i] < endTime[i] <= 10^9
53+
- 1 <= profit[i] <= 10^4
54+
55+
56+
57+
## 解题思路
58+
59+
- 给出一组任务,任务有开始时间,结束时间,和任务收益。一个任务开始还没有结束,中间就不能再安排其他任务。问如何安排任务,能使得最后收益最大?
60+
- 一般任务的题目,区间的题目,都会考虑是否能排序。这一题可以先按照任务的结束时间从小到大排序,如果结束时间相同,则按照收益从小到大排序。`dp[i]` 代表前 `i` 份工作能获得的最大收益。初始值,`dp[0] = job[1].profit` 。对于任意一个任务 `i` ,看能否找到满足 `jobs[j].enTime <= jobs[j].startTime && j < i` 条件的 `j`,即查找 `upper_bound` 。由于 `jobs` 被我们排序了,所以这里可以使用二分搜索来查找。如果能找到满足条件的任务 j,那么状态转移方程是:`dp[i] = max(dp[i-1], jobs[i].profit)`。如果能找到满足条件的任务 j,那么状态转移方程是:`dp[i] = max(dp[i-1], dp[low]+jobs[i].profit)`。最终求得的解在 `dp[len(startTime)-1]` 中。

0 commit comments

Comments
 (0)