Skip to content

Commit 98c5414

Browse files
committed
Add solution 630
1 parent 76753e8 commit 98c5414

24 files changed

+536
-256
lines changed

README.md

Lines changed: 190 additions & 190 deletions
Large diffs are not rendered by default.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
import (
4+
"container/heap"
5+
"sort"
6+
)
7+
8+
func scheduleCourse(courses [][]int) int {
9+
sort.Slice(courses, func(i, j int) bool {
10+
return courses[i][1] < courses[j][1]
11+
})
12+
maxHeap, time := &Schedule{}, 0
13+
heap.Init(maxHeap)
14+
for _, c := range courses {
15+
if time+c[0] <= c[1] {
16+
time += c[0]
17+
heap.Push(maxHeap, c[0])
18+
} else if (*maxHeap).Len() > 0 && (*maxHeap)[0] > c[0] {
19+
time -= heap.Pop(maxHeap).(int) - c[0]
20+
heap.Push(maxHeap, c[0])
21+
}
22+
}
23+
return (*maxHeap).Len()
24+
}
25+
26+
type Schedule []int
27+
28+
func (s Schedule) Len() int { return len(s) }
29+
func (s Schedule) Less(i, j int) bool { return s[i] > s[j] }
30+
func (s Schedule) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
31+
func (s *Schedule) Pop() interface{} {
32+
n := len(*s)
33+
t := (*s)[n-1]
34+
*s = (*s)[:n-1]
35+
return t
36+
}
37+
func (s *Schedule) Push(x interface{}) {
38+
*s = append(*s, x.(int))
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question630 struct {
9+
para630
10+
ans630
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para630 struct {
16+
courses [][]int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans630 struct {
22+
one int
23+
}
24+
25+
func Test_Problem630(t *testing.T) {
26+
27+
qs := []question630{
28+
29+
{
30+
para630{[][]int{{100, 200}, {200, 1300}, {1000, 1250}, {2000, 3200}}},
31+
ans630{3},
32+
},
33+
}
34+
35+
fmt.Printf("------------------------Leetcode Problem 630------------------------\n")
36+
37+
for _, q := range qs {
38+
_, p := q.ans630, q.para630
39+
fmt.Printf("【input】:%v 【output】:%v\n", p, scheduleCourse(p.courses))
40+
}
41+
fmt.Printf("\n\n\n")
42+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [630. Course Schedule III](https://leetcode.com/problems/course-schedule-iii/)
2+
3+
## 题目
4+
5+
There are `n` different online courses numbered from `1` to `n`. You are given an array `courses` where `courses[i] = [durationi, lastDayi]` indicate that the `ith` course should be taken **continuously** for `durationi` days and must be finished before or on `lastDayi`.
6+
7+
You will start on the `1st` day and you cannot take two or more courses simultaneously.
8+
9+
Return *the maximum number of courses that you can take*.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
15+
Output: 3
16+
Explanation:
17+
There are totally 4 courses, but you can take 3 courses at most:
18+
First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.
19+
Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day.
20+
Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.
21+
The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.
22+
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: courses = [[1,2]]
29+
Output: 1
30+
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: courses = [[3,2],[4,3]]
37+
Output: 0
38+
39+
```
40+
41+
**Constraints:**
42+
43+
- `1 <= courses.length <= 104`
44+
- `1 <= durationi, lastDayi <= 104`
45+
46+
## 题目大意
47+
48+
这里有 n 门不同的在线课程,他们按从 1 到 n 编号。每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天。一门课要持续学习 t 天直到第 d 天时要完成,你将会从第 1 天开始。给出 n 个在线课程用 (t, d) 对表示。你的任务是找出最多可以修几门课。
49+
50+
## 解题思路
51+
52+
- 一般选课,任务的题目会涉及排序 + 贪心。此题同样如此。最多修几门课,采用贪心的思路。先将课程结束时间从小到大排序,优先选择结束时间靠前的课程,这样留给后面课程的时间越多,便可以修更多的课。对排好序的课程从前往后选课,不断累积时间。如果选择修当前课程,但是会超时,这时改调整了。对于已经选择的课程,都加入到最大堆中,遇到需要调整时,比较当前待考虑的课程时长是否比(堆中)已经选择课中时长最长的课时长短,即堆顶的课程时长短,剔除 pop 它,再选择这门时长短的课,并加入最大堆中。并更新累积时间。一层循环扫完所有课程,最终最大堆中包含课程的数目便是最多可以修的课程数。
53+
54+
## 代码
55+
56+
```go
57+
package leetcode
58+
59+
import (
60+
"container/heap"
61+
"sort"
62+
)
63+
64+
func scheduleCourse(courses [][]int) int {
65+
sort.Slice(courses, func(i, j int) bool {
66+
return courses[i][1] < courses[j][1]
67+
})
68+
maxHeap, time := &Schedule{}, 0
69+
heap.Init(maxHeap)
70+
for _, c := range courses {
71+
if time+c[0] <= c[1] {
72+
time += c[0]
73+
heap.Push(maxHeap, c[0])
74+
} else if (*maxHeap).Len() > 0 && (*maxHeap)[0] > c[0] {
75+
time -= heap.Pop(maxHeap).(int) - c[0]
76+
heap.Push(maxHeap, c[0])
77+
}
78+
}
79+
return (*maxHeap).Len()
80+
}
81+
82+
type Schedule []int
83+
84+
func (s Schedule) Len() int { return len(s) }
85+
func (s Schedule) Less(i, j int) bool { return s[i] > s[j] }
86+
func (s Schedule) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
87+
func (s *Schedule) Pop() interface{} {
88+
n := len(*s)
89+
t := (*s)[n-1]
90+
*s = (*s)[:n-1]
91+
return t
92+
}
93+
func (s *Schedule) Push(x interface{}) {
94+
*s = append(*s, x.(int))
95+
}
96+
```

website/content/ChapterFour/0600~0699/0628.Maximum-Product-of-Three-Numbers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ func maximumProduct1(nums []int) int {
106106
----------------------------------------------
107107
<div style="display: flex;justify-content: space-between;align-items: center;">
108108
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0623.Add-One-Row-to-Tree/">⬅️上一页</a></p>
109-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0632.Smallest-Range-Covering-Elements-from-K-Lists/">下一页➡️</a></p>
109+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0630.Course-Schedule-III/">下一页➡️</a></p>
110110
</div>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [630. Course Schedule III](https://leetcode.com/problems/course-schedule-iii/)
2+
3+
## 题目
4+
5+
There are `n` different online courses numbered from `1` to `n`. You are given an array `courses` where `courses[i] = [durationi, lastDayi]` indicate that the `ith` course should be taken **continuously** for `durationi` days and must be finished before or on `lastDayi`.
6+
7+
You will start on the `1st` day and you cannot take two or more courses simultaneously.
8+
9+
Return *the maximum number of courses that you can take*.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
15+
Output: 3
16+
Explanation:
17+
There are totally 4 courses, but you can take 3 courses at most:
18+
First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.
19+
Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day.
20+
Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.
21+
The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.
22+
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: courses = [[1,2]]
29+
Output: 1
30+
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: courses = [[3,2],[4,3]]
37+
Output: 0
38+
39+
```
40+
41+
**Constraints:**
42+
43+
- `1 <= courses.length <= 104`
44+
- `1 <= durationi, lastDayi <= 104`
45+
46+
## 题目大意
47+
48+
这里有 n 门不同的在线课程,他们按从 1 到 n 编号。每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天。一门课要持续学习 t 天直到第 d 天时要完成,你将会从第 1 天开始。给出 n 个在线课程用 (t, d) 对表示。你的任务是找出最多可以修几门课。
49+
50+
## 解题思路
51+
52+
- 一般选课,任务的题目会涉及排序 + 贪心。此题同样如此。最多修几门课,采用贪心的思路。先将课程结束时间从小到大排序,优先选择结束时间靠前的课程,这样留给后面课程的时间越多,便可以修更多的课。对排好序的课程从前往后选课,不断累积时间。如果选择修当前课程,但是会超时,这时改调整了。对于已经选择的课程,都加入到最大堆中,遇到需要调整时,比较当前待考虑的课程时长是否比(堆中)已经选择课中时长最长的课时长短,即堆顶的课程时长短,剔除 pop 它,再选择这门时长短的课,并加入最大堆中。并更新累积时间。一层循环扫完所有课程,最终最大堆中包含课程的数目便是最多可以修的课程数。
53+
54+
## 代码
55+
56+
```go
57+
package leetcode
58+
59+
import (
60+
"container/heap"
61+
"sort"
62+
)
63+
64+
func scheduleCourse(courses [][]int) int {
65+
sort.Slice(courses, func(i, j int) bool {
66+
return courses[i][1] < courses[j][1]
67+
})
68+
maxHeap, time := &Schedule{}, 0
69+
heap.Init(maxHeap)
70+
for _, c := range courses {
71+
if time+c[0] <= c[1] {
72+
time += c[0]
73+
heap.Push(maxHeap, c[0])
74+
} else if (*maxHeap).Len() > 0 && (*maxHeap)[0] > c[0] {
75+
time -= heap.Pop(maxHeap).(int) - c[0]
76+
heap.Push(maxHeap, c[0])
77+
}
78+
}
79+
return (*maxHeap).Len()
80+
}
81+
82+
type Schedule []int
83+
84+
func (s Schedule) Len() int { return len(s) }
85+
func (s Schedule) Less(i, j int) bool { return s[i] > s[j] }
86+
func (s Schedule) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
87+
func (s *Schedule) Pop() interface{} {
88+
n := len(*s)
89+
t := (*s)[n-1]
90+
*s = (*s)[:n-1]
91+
return t
92+
}
93+
func (s *Schedule) Push(x interface{}) {
94+
*s = append(*s, x.(int))
95+
}
96+
```
97+
98+
99+
----------------------------------------------
100+
<div style="display: flex;justify-content: space-between;align-items: center;">
101+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0628.Maximum-Product-of-Three-Numbers/">⬅️上一页</a></p>
102+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0632.Smallest-Range-Covering-Elements-from-K-Lists/">下一页➡️</a></p>
103+
</div>

website/content/ChapterFour/0600~0699/0632.Smallest-Range-Covering-Elements-from-K-Lists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ func (p SortByVal) Less(i, j int) bool {
115115

116116
----------------------------------------------
117117
<div style="display: flex;justify-content: space-between;align-items: center;">
118-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0628.Maximum-Product-of-Three-Numbers/">⬅️上一页</a></p>
118+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0630.Course-Schedule-III/">⬅️上一页</a></p>
119119
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0633.Sum-of-Square-Numbers/">下一页➡️</a></p>
120120
</div>

0 commit comments

Comments
 (0)