630. Course Schedule III
Question
There are
n
different online courses numbered from1
ton
. You are given an arraycourses
wherecourses[i] = [duration<sub>i</sub>, lastDay<sub>i</sub>]
indicate that thei<sup>th</sup>
course should be taken continuously forduration<sub>i</sub>
days and must be finished before or onlastDay<sub>i</sub>
.You will start on the
1<sup>st</sup>
day and you cannot take two or more courses simultaneously.Return the maximum number of courses that you can take.
Solution
贪心算法,优先级队列实现。
优先选择截止日期更短的课程。
优先级队列pq记录当前已选课程,按持续时间从长到短排列。
同时维护当前的日期curDay。
当当前选择的课程截止日期晚于curDay加上当前的持续时间duration时,查看大根堆里最长的课程longestDuration。如果最长的时间大于当前时间,则将其挤出,并将当前课程加入。
如果当前课程截止日期晚于curDay,则直接将其加入队列中,并更新curDay。
Code
1 | class Solution { |
630. Course Schedule III
https://xuanhe95.github.io/2022/06/24/630-Course-Schedule-III/