Skip to content

Commit dddf785

Browse files
author
wuduhren
committed
updates
1 parent 91179a3 commit dddf785

5 files changed

+85
-0
lines changed

problems/python3/insert-interval.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
3+
ans = []
4+
i = 0
5+
6+
#add intervals before newInterval
7+
while i<len(intervals) and intervals[i][1]<newInterval[0]:
8+
ans.append(intervals[i])
9+
i += 1
10+
11+
#add newInterval and merge the overlapped
12+
ans.append(newInterval)
13+
while i<len(intervals) and intervals[i][0]<=ans[-1][1]:
14+
ans[-1][0] = min(ans[-1][0], intervals[i][0])
15+
ans[-1][1] = max(ans[-1][1], intervals[i][1])
16+
i += 1
17+
18+
#add intervals after newInterval
19+
while i<len(intervals):
20+
ans.append(intervals[i])
21+
i += 1
22+
23+
return ans

problems/python3/meeting-rooms-ii.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
3+
intervals.sort()
4+
h = []
5+
ans = 0
6+
7+
for s, e in intervals:
8+
while h and s>=h[0][0]:
9+
heapq.heappop(h)
10+
heapq.heappush(h, (e, s))
11+
ans = max(ans, len(h))
12+
return ans

problems/python3/meeting-rooms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
3+
intervals.sort()
4+
lastEnd = float('-inf')
5+
for s, e in intervals:
6+
if lastEnd>s:
7+
return False
8+
else:
9+
lastEnd = e
10+
return True
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
3+
ans = [-1]*len(queries)
4+
queries = sorted([(query, i) for i, query in enumerate(queries)])
5+
intervals.sort()
6+
h = []
7+
8+
itervalIndex = 0
9+
for query, queryIndex in queries:
10+
#push all intervals that include 'query' to the min heap
11+
while itervalIndex<len(intervals) and intervals[itervalIndex][0]<=query:
12+
left = intervals[itervalIndex][0]
13+
right = intervals[itervalIndex][1]
14+
size = right-left+1
15+
heapq.heappush(h, (size, right))
16+
itervalIndex += 1
17+
18+
#pop all intervals that not includes 'query' out of the min heap
19+
while h and h[0][1]<query:
20+
heapq.heappop(h)
21+
22+
if h: ans[queryIndex] = h[0][0]
23+
return ans
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Time: O(NLogN) for sorting.
3+
"""
4+
class Solution:
5+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
6+
intervals.sort()
7+
8+
ans = 0
9+
prevEnd = intervals[0][1]
10+
11+
for s, e in intervals[1:]:
12+
if s>=prevEnd:
13+
prevEnd = e
14+
else:
15+
ans += 1
16+
prevEnd = min(prevEnd, e)
17+
return ans

0 commit comments

Comments
 (0)