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的更多相关文章

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

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  2. 630. Course Schedule III

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  3. [leetcode-630-Course Schedule III]

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  4. [Swift]LeetCode630. 课程表 III | Course Schedule III

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  5. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  6. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  7. [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 ...

  8. [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 ...

  9. LeetCode Course Schedule II

    原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...

随机推荐

  1. mingster.com

    Good to Great: Why Some Companies Make the Leap... and Others Don'tby Jim Collinshttp://rcm.amazon.c ...

  2. Magic Methods 5

    描述符 : 将某种特殊类型的类的实例指派给另一个类的属性. 特殊类型为以下方法的1-3个 : __get__(self, instance, owner):用于访问属性,它返回属性的值 __set__ ...

  3. uboot--tftp

    一.      概述 U-boot中的TFTP用于发送较小的文件.下层使用UDP协议,发送使用UDP 69端口,每次发送的最大分组为512 Bytes.发送双方采用超时重传机制.数据传输模式为octe ...

  4. JAVA WEB基础巩固之Json、Ajax

    一.Json(Json介绍)json校验工具 Json是JavaScript object notation的缩写,优点是易读易编写易于解析: 例如:Object: { "name" ...

  5. Java并发编程(01):线程的创建方式,状态周期管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.并发编程简介 1.基础概念 程序 与计算机系统操作有关的计算机程序.规程.规则,以及可能有的文件.文档及数据. 进程 进程是计算机中的程序 ...

  6. 快速上手百度大脑EasyDL专业版·物体检测模型(附代码)

    作者:才能我浪费991.    简介:1.1.    什么是EasyDL专业版EasyDL专业版是EasyDL在2019年10月下旬全新推出的针对AI初学者或者AI专业工程师的企业用户及开发者推出的A ...

  7. java8新特性——stream笔记

    stream对象 Stream IntStream LongStream DoubleStream 创建 常用的三种方式: 使用list对象: list.stream() − 为集合创建串行流. li ...

  8. C# 关于位运算的学习笔记

    一.理解什么是位运算 程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),简单来说位运算就是直接对在内存中的二进制数的每位进行运算操作. 二.学习前先了解一下有哪些运算,运算符都怎么 ...

  9. ubuntu下安装typescript

    安装ts之前需要安装好node, 安装ts: 1. npm install -g typescript /opt/node/bin/tsc -> /opt/node/lib/node_modul ...

  10. PC端如何下载B站里面的视频?

    此随笔只是记录一下:   PC端下载B站的视频,在blibli前面加上一个i 然后在视频上鼠标右键,视频另存为+路径即可 PS:网上其他的方法,比如在blibli前面加上kan,后面加上jj等,这些方 ...