[抄题]:
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
.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
merge interval
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- Interval是一种结构,不是数组,不用加[]
- arrays.sort(具体对象+ comparator);
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
sort函数中包括comparator结构体,结构体中包括compare方法
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
comparator三部曲忘了怎么写了:sort函数中包括comparator结构体,结构体中包括compare方法
[关键模板化代码]:
//sort Arrays.sort(intervals, new Comparator<Interval>() { public int compare(Interval a, Interval b) { return a.start - b.start; } });
[其他解法]:
[Follow Up]:
253. Meeting Rooms II PQ这个真的忘了啊
[LC给出的题目变变变]:
merge interval
[代码风格] :
/** * 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 boolean canAttendMeetings(Interval[] intervals) { //cc if (intervals == null) { return false; } //sort Arrays.sort(intervals, new Comparator<Interval>() { public int compare(Interval a, Interval b) { return a.start - b.start; } }); //compare for (int i = 0; i < intervals.length - 1; i++) { if (intervals[i + 1].start < intervals[i].end) return false; } return true; } }