[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.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
bool canAttendMeetings(vector<Interval>& intervals) {
auto cmp = [](Interval a, Interval b) { return a.start < b.start; };
sort(intervals.begin(), intervals.end(), cmp);
for(int i =1; i < intervals.size(); i++)
if(intervals[i].start < intervals[i-1].end) return false;
return true;
}
};
python
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def canAttendMeetings(self, v):
"""
:type intervals: List[Interval]
:rtype: bool
"""
v.sort(key = lambda val: val.start)
return not any(v[i].start < v[i-1].end for i in range(1,len(v)))
相關文章
- 252. Meeting RoomsOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- 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] Meeting Rooms 會議室LeetCodeOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- Meeting Rooms IIOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- 253. Meeting Rooms IIOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- Leetcode 第136場周賽解題報告LeetCode
- LeetCode Weekly Contest 96 解題報告LeetCode
- Leetcode Weekly Contest 95解題報告LeetCode
- Leetcode Weekly Contest94 解題報告LeetCode
- LeetCode解題報告 279. Perfect Squares [medium]LeetCode
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- LeetCode 解題報告 - 2. Add Two NumbersLeetCode
- LeetCode 252. 會議室(排序)LeetCode排序
- LeetCode解題報告 120. Triangle [medium]LeetCode
- LeetCode解題報告 241. Different Ways to Add Parentheses [medium]LeetCode