LeetCode Meeting Rooms
原题链接在这里:https://leetcode.com/problems/meeting-rooms/
题目:
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return false.
题解:
对array进行排序,从i = 1开始判断start是否在前一个end之前, 若是就return false. 完成loop返回true.
Time Complexity: O(nlogn). Space: O(1).
AC Java:
/**
* 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; }
* }
*/
public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if(intervals == null || intervals.length == 0){
return true;
}
Arrays.sort(intervals, new Comparator<Interval>(){
public int compare(Interval i1, Interval i2){
if(i1.start == i2.start){
return i1.end - i2.end;
}
return i1.start - i2.start;
}
}); for(int i = 1; i<intervals.length; i++){
if(intervals[i].start < intervals[i-1].end){
return false;
}
}
return true;
}
}
LeetCode Meeting Rooms的更多相关文章
- [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 会议室
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 ...
- [LeetCode] Meeting Rooms I & II
Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s ...
- [LeetCode] 253. Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 252. Meeting Rooms 会议室
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 ...
- [Algorithm] How many meeting rooms needed?
Give you set of meetings start time and end time, count how many meeting rooms needed. For example: ...
- 252. Meeting Rooms 区间会议室
[抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...
随机推荐
- (转载)c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值
第一种方法: 用委托,Form2和Form3是同一组 Form2 C#代码 using System; using System.Collections.Generic; using System.C ...
- 使用 Git 来管理 Xcode 中的代码片段
使用 Git 来管理 Xcode 中的代码片段 代码片段介绍 xcode4 引入了一个新 feature: code snippets,在整个界面的右下角,可以通过快捷键:cmd + ctrl + o ...
- 【C语言】16-预处理指令2-条件编译
条件编译的概念 在很多情况下,我们希望程序的其中一部分代码只有在满足一定条件时才进行编译,否则不参与编译(只有参与编译的代码最终才能被执行),这就是条件编译. 一.基本用法 1 #if 条件1 2 . ...
- nodejs安装和环境搭建
环境下载 根据系统类型,系统位数选择需要的版本下载.下载地址:https://nodejs.org/en/download/ msi:开发环境,包含运行环境,开发文档等,所以我们下载.msi. exe ...
- oracle sql日期比较
oracle sql日期比较:在今天之前: select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm ...
- Gitolite配置管理和GIT基本操作
简述公司版gitolite的项目配置与管理 1. 基于秘钥对的管理 1.1 客户端(需要访问代码库的机器)生成秘钥对,采用RSA加密ssh-keygen -t rsa -f path_to_store ...
- HTML / JavaScript / PHP 实现页面跳转的几种方式
① HTML 的 meta refresh 标签 <!doctype html> <html lang="en"> <head> <met ...
- 【翻译】Kinect v2程序设计(C++) BodyIndex篇
通过Kinect SDK v2预览版,取得BodyIndex(人体区域)的方法和示例代码. 上一节,介绍了从Kinect v2预览版用Kinect SDK v2预览版获取Depth数据的方法. 这 ...
- C# Xml文件操作,解释见注释
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Mysql 定时备份操作
1.创建保存备份文件的路径/mysqldata #mkdir /bak/mysqlbak 2.创建/usr/sbin/bakmysql文件 #vi /usr/sbin/bakmysql.sh 3.写入 ...