-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeeting Rooms II.py
84 lines (67 loc) · 1.89 KB
/
Meeting Rooms II.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
'''
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
Example 1:
Input: [[0, 30],[5, 10],[15, 20]]
Output: 2
Example 2:
Input: [[7,10],[2,4]]
Output: 1
'''
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def minMeetingRooms(self, intervals):
"""
:type intervals: List[Interval]
:rtype: int
"""
start = [x.start for x in intervals]
end = [x.end for x in intervals]
start.sort()
end.sort()
res = 0
idx = 0
for i in xrange(len(start)):
if start[i] < end[idx]:
res += 1
else:
idx += 1
return res
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def minMeetingRooms(self, intervals):
"""
:type intervals: List[Interval]
:rtype: int
"""
start = sorted([x.start for x in intervals])
end = sorted([x.end for x in intervals])
res = 0
tmp = 0
i = 0
j = 0
while i < len(start):
if j < len(end):
if start[i] < end[j]:
if tmp > 0:
tmp -= 1
else:
res += 1
i += 1
else:
tmp += 1
j += 1
else:
if tmp > 0:
tmp -= 1
else:
res += 1
i += 1
return res