贪心-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 ...
随机推荐
- 360若真入股HTC 到底是谁来拯救谁
到底是谁来拯救谁" title="360若真入股HTC 到底是谁来拯救谁"> 我总是持有一种观点,那就是拯救是相互的.就像老师拯救"堕落"学生, ...
- IPSec 传输模式下ESP报文的装包与拆包过程 - 择日而终的博客
一.IPsec简介 IPSec ( IP Security )是IETF(Internet Engineering Task Force,Internet工程任务组)的IPSec小组建立的一组IP安全 ...
- android activity 启动过程分析(source code 4.4)
说实话,android source code从2.3到4.4变化是蛮多的,尤其是media部分,虽然总的框架是没有多大变化,但是找起代码来看还是挺麻烦的.在android里面最受伤的是使用了java ...
- Wordpress 外网访问时不显示图片解决办法
我的云服务器是 :windows2012R 打开命令行: 进入到mysql中 show databases; //查看你Wordpress使用的数据库是否存在 use blog; //例如你使用的数据 ...
- CSS——NO.1(初识CSS)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- Python知识点汇总
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- SpringBoot入门系列(一)如何快速创建SpringBoot项目
这段时间也没什么事情,所以就重新学习整理了Spring Boot的相关内容.今天开始整理更新Spring Boot学习笔记,感兴趣的朋友可以关注我的博客:https://www.cnblogs.com ...
- 探究Java中的引用
探究Java中的四种引用 从JDK1.2版本开始,Java把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期.这四种级别由高到低依次为:强引用.软引用.弱引用和虚引用.本篇就来详细探究 ...
- 如何使用@import导入实现了ImportBeanDefinitionRegistrar接口的类?
如何使用@import导入实现了ImportBeanDefinitionRegistrar接口的类? 在程序开发的时候,我们经常会遇见一个名词“接口”这也是我们做开发人员工作中必不可少的一个技术, ...
- 正式学习MVC 04
1.ActionResult ActionResult是一个父类, 子类包括了我们熟知的 ViewResult 返回相应的视图 ContentResult 返回字符串 RedirectResult( ...