[LeetCode] Course Schedule III 课程清单之三

There are n different online courses numbered from 1 to n. Each course has some duration(course length) tand closed on dth day. A course should be taken continuously for t days and must be finished before or on the dth day. You will start at the 1st day.

Given n online courses represented by pairs (t,d), your task is to find the maximal number of courses that can be taken.

Example:

Input: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]
Output: 3
Explanation:
There're totally 4 courses, but you can take 3 courses at most:
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.
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.
Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.
The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.

Note:

  1. The integer 1 <= d, t, n <= 10,000.
  2. You can't take two courses simultaneously. 

这道题给了我们许多课程,每个课程有两个参数,第一个是课程的持续时间,第二个是课程的最晚结束日期,让我们求最多能上多少门课。博主尝试了递归的暴力破解,TLE了。这道题给的提示是用贪婪算法,那么我们首先给课程排个序,按照结束时间的顺序来排序,我们维护一个当前的时间,初始化为0,再建立一个优先数组,然后我们遍历每个课程,对于每一个遍历到的课程,当前时间加上该课程的持续时间,然后将该持续时间放入优先数组中,然后我们判断如果当前时间大于课程的结束时间,说明这门课程无法被完成,我们并不是直接减去当前课程的持续时间,而是取出优先数组的顶元素,即用时最长的一门课,这也make sense,因为我们的目标是尽可能的多上课,既然非要去掉一门课,那肯定是去掉耗时最长的课,这样省下来的时间说不定能多上几门课呢,最后返回优先队列中元素的个数就是能完成的课程总数啦,参见代码如下:

class Solution {

public:
    int scheduleCourse(vector<vector<int>>& courses) {
        int curTime = 0;
        priority_queue<int> q;
        sort(courses.begin(), courses.end(), [](vector<int>& a, vector<int>& b) {return a[1] < b[1];});
        for (auto course : courses) {
            curTime += course[0];
            q.push(course[0]);
            if (curTime > course[1]) {
                curTime -= q.top(); q.pop();
            }
        }
        return q.size();
    }
};

参考资料:

https://discuss.leetcode.com/topic/93790/short-java-code-using-priorityqueue

https://discuss.leetcode.com/topic/93712/python-straightforward-with-explanation

https://discuss.leetcode.com/topic/93884/c-short-elegant-o-nlogn-time-o-k-space-solution/2

本文转自博客园Grandyang的博客,原文链接:[LeetCode] Course Schedule III 课程清单之三

,如需转载请自行联系原博主。

时间: 2024-09-20 08:42:38

[LeetCode] Course Schedule III 课程清单之三的相关文章

[LeetCode] Single Number III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The order

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

Web APP开发案例:旅程计划应用(Wayfindit: Trip Planner App)

文章描述:Google Web App开发指南第三章:案例研究. 旅程计划应用(Wayfindit: Trip Planner App) 在大多数情况下,Wayfindit的应用必须有很好的易用性.旅行是一件很复杂的事情,不管是商业旅行还是休假旅行,一个顺利的旅程要求从家门到目的都没有意外之忧.Wayfindit的应用要能给旅行者提供所需信息,并且要快而准确.这意味着它需要一个最小的.直观的.响应式界面,能在前端提供有关内容的重要信息--HTML5的地理感知和离线存储特性实现. 一个完美的袖珍指

Google Web App开发指南第三章:案例研究

旅程计划应用(Wayfindit: Trip Planner App) 在大多数情况下,Wayfindit的应用必须有很好的易用性.旅行是一件很复杂的事情,不管是商业旅行还是休假旅行,一个顺利的旅程要求从家门到目的都没有意外之忧.Wayfindit的应用要能给旅行者提供所需信息,并且要快而准确.这意味着它需要一个最小的.直观的.响应式界面,能在前端提供有关内容的重要信息--HTML5的地理感知和离线存储特性实现. 一个完美的袖珍指南 它就装在你的口袋里或者包里,即时提供信息.它拥有本地存储和地理

《CUDA高性能并行计算》----第3章 从循环到网格 3.1 并行化 dist_v1

本 节 书 摘 来 自 华 章 出 版 社 <CUDA高性能并行计算> 一 书 中 的 第3章,第3.1节, 作 者 CUDA for Engineers: An Introduction to High-Performance Parallel Computing[美] 杜安·斯托尔蒂(Duane Storti)梅特·尤尔托卢(Mete Yurtoglu) 著,苏统华 项文成 李松泽 姚宇鹏 孙博文 译 , 更 多 章 节 内 容 可 以 访 问 云 栖 社 区 "华 章 计 算

本·内尔森将挑战美国常春藤盟校

哥伦比亚大学图书馆 和讯科技消息 北京时间4月5日,美国图片分享网站Snapfish和家居网站Redbeacon前CEO本·内尔森(Ben Nelson)本周表示,他已获得2500万美元的种子基金,将组建在线大学Minerva Project,对抗著名的"常青藤"盟校. 应对学费上涨 目前,互联网上已有许多在线大学,一些来自私人,另一些则是公立的,但这些在线大学的质量良莠不齐.内尔森认为,在线大学的发展可以采用一种全新模式.有这样想法的并不止内尔森一人.多名教育行业创业者今年已启动了创

《Cocos2D-x权威指南》——3.10 时间调度

3.10 时间调度 在游戏中,时常需要隔一段时间更新一些数据或者是人物位置,Cocos2D-x中提供了这些时间调度的函数,所有CCNode类的子类都有这样的函数,定义方法如代码清单3-50所示.代码清单3-50 schedule的使用 schedule(schedule_selector(SchedulerAutoremove::autoremove), 0.5f); 这是一个按时调用一个函数的方法.第一个参数使用schedule_selector选择器将autoremove函数名称传进来.第二

研究了数千个在线课程,我整理了一份数据科学入门课清单

一年前,我退出了加拿大最好的计算机科学项目之一,利用在线资源开始创建属于自己的数据科学硕士课程.我意识到我可以通过edX, Coursera,以及Udacity学习我所需要的一切,而且学的更快.效率更高,学费更低. 数据可视化:Alanah Ryding 现在我差不多快要完成了.我上了很多数据科学相关的课程,旁听过更多课程的部分内容.我知道对于一个准备成为数据分析师或数据科学家的初学者来说有哪些选择,以及什么样的技能是必需的.几个月前,我开始创建一个用评价驱动的指南,用来为数据科学中的每个主题推

[LeetCode] Contains Duplicate(II,III)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 解题思路 用一个set保存数组中的值,如