贪心-Course Schedule III
2020-02-01 21:37:39
问题描述:

问题求解:
对于课程来说截止时间在前面的肯定需要优先安排,所以首先需要将courses按照deadline进行排序。
然后只需要不断的加入当前的课程即可,如果时间超过了deadline,那么就将之前最耗时的课程剔除即可。
为什么剔除了耗时的就一定可以不超时呢?
1)最耗时的是当前的课程,那么直接删除,状态还原到上一层,不超时;
2)最耗时的不是当前的课程,那么删除耗时的,再加上新增的必不会超过prev_time,也就必不会超时;
public int scheduleCourse(int[][] courses) {
Arrays.sort(courses, new Comparator<int[]>(){
public int compare(int[] o1, int[] o2) {
return o1[1] - o2[1];
}
});
PriorityQueue<Integer> pq = new PriorityQueue<>();
int time = 0;
for (int[] c : courses) {
time += c[0];
pq.add(-c[0]);
if (time > c[1]) {
time += pq.poll();
}
}
return pq.size();
}
贪心-Course Schedule III的更多相关文章
- [LeetCode] Course Schedule III 课程清单之三
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- 630. Course Schedule III
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- [leetcode-630-Course Schedule III]
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- [Swift]LeetCode630. 课程表 III | Course Schedule III
There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- 【LeetCode】贪心 greedy(共38题)
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
- [LeetCode] Course Schedule II 课程清单之二
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [LeetCode] Course Schedule 课程清单
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- LeetCode Course Schedule II
原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...
随机推荐
- 【U创营学员招募】8节免费云计算课程,让你轻松掌握生产技能
课程不错,免费听课.免费听课.免费听课,强烈推荐-! 公众号对话框回复"课程助手" 即可报名! ---END---
- 使用 javascript 配置 nginx
在上个月的 nginx.conf 2015 大会上, 官方宣布已经支持通过 javascript 代码来配置 nginx,并把这个实现称命名为--nginscript.使用 nginscript,可以 ...
- 有关终端的一些tips
reg.exe是用于操作注册表的命令,可以通过reg /?来查看所有参数,在pentest中有两个很实用的参数 reg query 读取注册表信息, reg add 添加或修改注册表内容. 设想如下场 ...
- webpack的基本配置(初识)
webpack能根据模块的依赖关系递归地构建一个依赖关系图,当中包含了应用程序所需要的所有模块,最后打包成一个或多个bundle.它有四个核心概念entry.output .loader.plugin ...
- 作为前端,你需要懂得javascript实现继承的方法
在ES6之前,javascript不跟其他语言一样,有直接继承的方法,它需要借助于构造函数+原型对象模拟实现继承.现在我们可以利用ES6的extends方法实现继承,如果想了解更多有关ES6实现的继承 ...
- 10分钟进阶SpringBoot - 05. 数据访问之JDBC(附加源码分析+代码下载)
10分钟进阶SpringBoot - 05. 数据访问之JDBC 代码下载:https://github.com/Jackson0714/study-spring-boot.git 一.JDBC是什么 ...
- SpringBoot2整合Redis缓存
遵循SpringBoot三板斧 第一步加依赖 <!-- Redis --> <dependency> <groupId>org.springframework.bo ...
- 在idea下遇到的问题汇总(间接性更新)
在idea下遇到的问题汇总(间接性更新) tomcat下的jsp代码问题: 在idea的环境下,遇到jsp代码.符号失效,首先需要考虑到jar包没有引入,情况如图: 这种情况是因为jar包没有导入进去 ...
- Angular 从入坑到挖坑 - 表单控件概览
一.Overview angular 入坑记录的笔记第三篇,介绍 angular 中表单控件的相关概念,了解如何在 angular 中创建一个表单,以及如何针对表单控件进行数据校验. 对应官方文档地址 ...
- 解决layui表单ajax提交回调函数不起作用问题的两种方式
最近想用layui开发一个论坛模板用的是fly-ui,才接触layui对其还不太熟悉.一个简单的登录就困扰了我很久.登录的form通过ajax提交回调函数老是不起作用.经过浪费了N多时间的调试,发现l ...