LeetCode 252. 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
.
start已經排好序,所以只檢查這個條件就足夠了)
/**
* 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) {
Arrays.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval i1, Interval i2) {
return i1.start - i2.start;
}
});
for(int i=0; i<intervals.length-1; i++) {
if (intervals[i].start <= intervals[i+1].start && intervals[i+1].start < intervals[i].end) return false;
}
return true;
}
}
方法二:利用start<end的特性,分別對start和end進行排序。
/**
* 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) {
int[] starts = new int[intervals.length];
int[] ends = new int[intervals.length];
for(int i=0; i<intervals.length; i++) {
starts[i] = intervals[i].start;
ends[i] = intervals[i].end;
}
Arrays.sort(starts);
Arrays.sort(ends);
for(int i=1; i<intervals.length; i++) {
if (ends[i-1] > starts[i]) return false;
}
return true;
}
}
相關文章
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- 252. Meeting RoomsOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- LeetCode 252. 會議室(排序)LeetCode排序
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- Leetcode: Meeting RoomsLeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- LeetCode-Meeting Rooms IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- Meeting Rooms IIOOM
- [LeetCode] 252、會議室LeetCode
- leetcode 掃描線專題 06-leetcode.252 meeting room 力扣.252 會議室LeetCodeOOM力扣
- 253. Meeting Rooms IIOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- 【leetcode】252 會議室(陣列)LeetCode陣列
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- LeetCode-Python-252. 會議室LeetCodePython
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- leetcode253——會議室 II——java實現LeetCodeJava
- 每天站立會議(Daily Stand-up Meeting)AI
- Leetcode 253: meeting roomLeetCodeOOM
- 會議室多屏同步顯示
- 高階網路會議室如何實現會議集中控制
- LeetCode-Best Meeting PointLeetCode