-
Notifications
You must be signed in to change notification settings - Fork 43
/
meeting-rooms.py
150 lines (128 loc) · 4.04 KB
/
meeting-rooms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
252. Meeting Rooms
Easy
Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: true
Constraints:
0 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti < endi <= 106
"""
# Time: O(nlogn)
# Space: O(n)
#
# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
# V0
class Solution:
def canAttendMeetings(self, intervals):
"""
NOTE this
"""
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
"""
NOTE this :
-> we compare ntervals[i][0] and ntervals[i-1][1]
"""
if intervals[i][0] < intervals[i-1][1]:
return False
return True
# V0'
# IDEA : SORT
class Solution(object):
def canAttendMeetings(self, intervals):
# edge case
if not intervals or len(intervals) == 1:
return True
intervals.sort(key = lambda x : [x[0], -x[1]])
if len(intervals) == 2:
return intervals[0][0] < intervals[1][0] and intervals[0][1] < intervals[1][1]
last = intervals[0]
for i in range(1, len(intervals)):
# CASE 1 : last and intervals[i] overlap (3 situations)
if (last[1] > intervals[i][0] and intervals[i][0] < last[1]) or\
(last[0] == intervals[i][0] and last[1] == intervals[i][1]) or\
(last[0] < intervals[i][1] and last[1] > intervals[i][1]):
return False
# CASE 2 : last and intervals[i] NOT overlap
else:
last = intervals[i]
return True
# V1
class Solution:
# @param {Interval[]} intervals
# @return {boolean}
def canAttendMeetings(self, intervals):
intervals.sort(key=lambda x: x[0])
for i in range(len(intervals)-1):
if intervals[i][1] > intervals[i+1][0]:
return False
return True
# V1'
# IDEA : BRUTE FORCE
# https://leetcode.com/problems/meeting-rooms/solution/
# JAVA
# public static boolean overlap(int[] interval1, int[] interval2) {
# return (Math.min(interval1[1], interval2[1]) >
# Math.max(interval1[0], interval2[0]));
# }
# V1''
# IDEA : SORTING
# https://leetcode.com/problems/meeting-rooms/solution/
class Solution:
def canAttendMeetings(self, intervals):
intervals.sort()
for i in range(len(intervals) - 1):
if intervals[i][1] > intervals[i + 1][0]:
return False
return True
# V1''''
# https://blog.csdn.net/qq508618087/article/details/50750465
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)))
# V1''''
# https://www.jiuzhang.com/solution/meeting-rooms/
# JAVA
# 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){
# return i1.start - i2.start;
# }
# });
# int end = intervals[0].end;
# for(int i = 1; i < intervals.length; i++){
# if(intervals[i].start < end) {
# return false;
# }
# end = Math.max(end, intervals[i].end);
# }
# return true;
# }
# }
# V2
class Solution:
# @param {Interval[]} intervals
# @return {boolean}
def canAttendMeetings(self, intervals):
intervals.sort(key=lambda x: x.start)
for i in range(1, len(intervals)):
if intervals[i].start < intervals[i-1].end:
return False
return True