253. Meeting Rooms II 需要多少间会议室
[抄题]:
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
首尾时间衔接上了,就业只需要一间
[思维问题]:
[一句话思路]:
结束时间每次都取最小的,所以用heap来维持
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- heap中添加元素用的是吉利的offer方法,接口和具体实现都用的是PQ
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
首尾时间衔接上了,就业只需要一间
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
heap模板:长度+三层:
括号是空的表示构造函数
/ Use a min heap to track the minimum end time of merged intervals
PriorityQueue<Interval> heap = new PriorityQueue<Interval>(intervals.length, new Comparator<Interval>() {
public int compare(Interval a, Interval b) { return a.end - b.end; }
});
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {
public int minMeetingRooms(Interval[] intervals) {
//cc : null
if (intervals == null || intervals.length == 0) return 0; //ini : sort start, min heap for end, offer
Arrays.sort(intervals, new Comparator<Interval>(){public int compare(Interval a, Interval b)
{return a.start - b.start;}}); PriorityQueue<Interval> heap = new PriorityQueue<Interval>(intervals.length, new Comparator<Interval>()
{public int compare(Interval a, Interval b) {return a.end - b.end;}}); //for loop
heap.offer(intervals[0]);
for (int i = 1; i < intervals.length; i++) {
//poll
Interval curr = heap.poll(); //compare end
if (intervals[i].start >= curr.end) {
curr.end = intervals[i].end;
}else {
heap.add(intervals[i]);
} //put back
heap.offer(curr);
} return heap.size();
}
}
253. Meeting Rooms II 需要多少间会议室的更多相关文章
- [LeetCode] 253. Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 253. Meeting Rooms II 会议室 II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- 【LeetCode】253. Meeting Rooms II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+堆 日期 题目地址:https://leetco ...
- [leetcode]253. Meeting Rooms II 会议室II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- 253. Meeting Rooms II
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
- [LeetCode#253] Meeting Rooms II
Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...
- [LC] 253. Meeting Rooms II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- LeetCode Meeting Rooms II
原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...
随机推荐
- win7下安装ubuntu14.04lts 双系统
首先,在win7下的硬盘管理 压缩出一块空闲的分区,即压缩卷之后,不做任何操作. 并且确保该空闲卷是“基本”类型 不是的话,参考http://www.jianshu.com/p/2f07312 ...
- linux CentOS 安装rz和sz命令 lrzsz
lrzsz在linux里可代替ftp上传和下载. lrzsz 官网入口:http://freecode.com/projects/lrzsz/ lrzsz是一个unix通信套件提供的X,Y,和ZMod ...
- vs2012加载T4MVC模板
(1)在工程项目上右键点击“管理NuGet程序包”,在线搜索T4MVC模板,选择并安装,安装成功后,项目中会添加T4MVC.tt文件及子文件. (2)如果添加了新的控制器,则右击T4MVC.tt文件点 ...
- Apache DBUtils使用总结 【转】
Apache DBUtils使用总结 DBUtils是个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果集封装成JavaBean,这就为我们做了最枯燥乏味. ...
- python is 和 == 的区别
一.is 和 == 的区别 == 比较 比较的俩边的值 is 比较 比较的是内存地址 id() 二.小数据池 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址就不一样 字符串 ...
- Hibernate学习7—Hibernate 映射继承
需求:学生有很多照片,分为生活照和工作照: 第一节:每个具体类对应一个表 Student.java: package com.cy.model; import java.util.Set; publi ...
- ping正常但是ssh到linux服务器很卡的解决方法
ssh登录很慢的问题 1.关闭ssh DNS反向解析 vi /etc/ssh/sshd_config 修改UseDNS no 2.关闭 GSSAPI 的用户认证 vi /etc/ssh/sshd ...
- 009:JSON
一. MySQL JSON类型 1. JSON介绍 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级 ...
- open the flashback
1.打开flashback: 关闭数据库 启动到mount方式 SQL>startup mount; 如果归档没有打开,打开归档[因为flashback依赖Media recovery,所以在打 ...
- linux 进程通信 :流套接字
消息队列是可以实现没有共同关系的进程之间的通信.Socket则可以实现不同计算机的不同进程之间的通信. //地址的结构体 struct sockaddr_in{ short int sin_famil ...