Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

252. 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.

Example 1:

1
2
Input: [[0,30],[5,10],[15,20]]
Output: false

Example 2:

1
2
Input: [[7,10],[2,4]]
Output: true

依區間結尾排序,看區間有無重疊,上個時間結尾如果超出下個開始就給false

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
public class MeetingRooms {
public static boolean canAttend(int[][] intervals){
if(intervals == null || intervals.length == 0) return true;
for(int i = 1; i < intervals.length; i++){
if(intervals[i][0] < intervals[i - 1][1])
return false;
}
return true;
}
public static void main(String[] args) {
int[][] slot = {{0, 3}, {5, 10}, {15, 20}};
System.out.println(canAttend(slot));
}
}
>
True

Python

1
2
3
4
5
6
7
8
T:O(n), S:O(1)
class Solution(object):
def canAttendMeetings(self, intervals):
# intervals.sort(key=lambda itv: (itv[0], itv[1))
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i - 1][1]:
return False
return True

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels