Skip to content

Commit d37333d

Browse files
committed
update
1 parent 295946e commit d37333d

21 files changed

+687
-48
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Large diffs are not rendered by default.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class Solution:
2+
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
3+
slots1.sort()
4+
slots2.sort()
5+
i, j = 0, 0
6+
res = []
7+
while i < len(slots1) and j < len(slots2):
8+
s1, e1 = slots1[i]
9+
s2, e2 = slots2[j]
10+
if e1 < s2:
11+
i += 1
12+
elif e2 < s1:
13+
j += 1
14+
else:
15+
interval = [max(s1, s2), min(e1, e2)]
16+
if interval[1] - interval[0] >= duration:
17+
res.append(interval[0])
18+
res.append(interval[0] + duration)
19+
break
20+
elif e1 < e2:
21+
i += 1
22+
else:
23+
j += 1
24+
25+
return res
26+
27+
# time O(mlogm + nlogn)
28+
# space O(1), not counting built in sort cost
29+
# using array and line sweep and compare two intervals each round and two pointers and greedy and sort
30+
31+
class Solution:
32+
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
33+
34+
intervals = []
35+
for s, e in slots1:
36+
if e - s >= duration:
37+
intervals.append([s, e])
38+
for s, e in slots2:
39+
if e - s >= duration:
40+
intervals.append([s, e])
41+
intervals.sort()
42+
43+
res = []
44+
if intervals:
45+
prev_s, prev_e = intervals[0]
46+
for i in range(1, len(intervals)):
47+
cur_s, cur_e = intervals[i]
48+
if prev_e < cur_s:
49+
prev_s, prev_e = cur_s, cur_e
50+
else:
51+
interval = [max(prev_s, cur_s), min(prev_e, cur_e)]
52+
if interval[1] - interval[0] >= duration:
53+
res.append(interval[0])
54+
res.append(interval[0] + duration)
55+
break
56+
if cur_e > prev_e:
57+
prev_s, prev_e = cur_s, cur_e
58+
return res
59+
60+
# time O((m+n)log(m+n))
61+
# space O(m+n)
62+
# using array and line sweep and compare two intervals each round and greedy and sort and prune
63+
64+
from heapq import *
65+
class Solution:
66+
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
67+
68+
events = []
69+
for s, e in slots1:
70+
if e - s >= duration:
71+
events.append((s, 1))
72+
events.append((e, - 1))
73+
for s, e in slots2:
74+
if e - s >= duration:
75+
events.append((s, 1))
76+
events.append((e, - 1))
77+
78+
heapify(events)
79+
status = 0
80+
prev_timestamp = 0
81+
res = []
82+
while events:
83+
timestamp, weight = heappop(events)
84+
if status == 2:
85+
interval = [prev_timestamp, timestamp]
86+
if interval[1] - interval[0] >= duration:
87+
res.append(interval[0])
88+
res.append(interval[0] + duration)
89+
break
90+
prev_timestamp = timestamp
91+
status += weight
92+
return res
93+
94+
# time O((m+n)log(m+n))
95+
# space O(m+n)
96+
# using array and line sweep and and greedy and heap and prune
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]:
3+
4+
res = []
5+
prev_s, prev_e = toBeRemoved
6+
for i, (cur_s, cur_e) in enumerate(intervals):
7+
if cur_s >= prev_e:
8+
res.extend(intervals[i:])
9+
return res
10+
elif cur_e <= prev_s:
11+
res.append([cur_s, cur_e])
12+
else:
13+
if cur_s < prev_s:
14+
res.append([cur_s, prev_s])
15+
if cur_e > prev_e:
16+
res.append([prev_e, cur_e])
17+
18+
return res
19+
20+
# time O(n)
21+
# space O(n), due to output list
22+
# using array and line sweep and compare two intervals each round
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
3+
intervals.sort(key = lambda x: (x[0], - x[1]))
4+
5+
res = 0
6+
prev_s, prev_e = intervals[0]
7+
for i in range(1, len(intervals)):
8+
cur_s, cur_e = intervals[i]
9+
if prev_e >= cur_e:
10+
res += 1
11+
else:
12+
prev_s, prev_e = cur_s, cur_e
13+
14+
return len(intervals) - res
15+
16+
# time O(nlogn)
17+
# space O(1) or consider the built in sort's cost
18+
# using array and line sweep and compare two intervals each round and sort and greedy
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from heapq import *
2+
class Solution:
3+
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
4+
5+
intervals.sort()
6+
queries = [(q, i) for i, q in enumerate(queries)]
7+
queries.sort()
8+
res = [- 1 for _ in range(len(queries))]
9+
10+
heap = []
11+
idx = 0
12+
for q, i in queries:
13+
while idx < len(intervals):
14+
s, e = intervals[idx]
15+
if q < s:
16+
break
17+
elif e < q:
18+
idx += 1
19+
else:
20+
heappush(heap, (e - s + 1, s, e))
21+
idx += 1
22+
23+
while heap:
24+
length, s, e = heap[0]
25+
if e < q:
26+
heappop(heap)
27+
else:
28+
res[i] = length
29+
break
30+
return res
31+
32+
# time O(nlogn + qlogq)
33+
# space O(n + q)
34+
# using array and line sweep and heap to store previous intervals’ states and greedy and sort
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
3+
if not intervals:
4+
return True
5+
6+
intervals.sort()
7+
8+
prev_s, prev_e = intervals[0]
9+
for i in range(1, len(intervals)):
10+
cur_s, cur_e = intervals[i]
11+
if prev_e <= cur_s:
12+
prev_s, prev_e = cur_s, cur_e
13+
continue
14+
return False
15+
16+
return True
17+
18+
# time O(nlogn)
19+
# space O(1), or consider sort's cost
20+
# using array and line sweep and compare two intervals each round
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from heapq import *
2+
class Solution:
3+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
4+
5+
intervals.sort()
6+
7+
heap = []
8+
for s, e in intervals:
9+
if heap and heap[0] <= s:
10+
heappop(heap)
11+
heappush(heap, e)
12+
return len(heap)
13+
14+
# time O(nlogn)
15+
# space O(n)
16+
# using array and line sweep and heap to store previous intervals’ states
17+
18+
from heapq import *
19+
class Solution:
20+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
21+
22+
intervals.sort()
23+
24+
heap = []
25+
res = 0
26+
for s, e in intervals:
27+
while heap and heap[0] <= s:
28+
heappop(heap)
29+
heappush(heap, e)
30+
res = max(res, len(heap))
31+
return res
32+
33+
# time O(nlogn)
34+
# space O(n)
35+
# using array and line sweep and heap to store previous intervals’ states
36+
37+
class Solution:
38+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
39+
40+
events = []
41+
42+
for s, e in intervals:
43+
events.append((s, 1))
44+
events.append((e, - 1))
45+
events.sort()
46+
47+
status = 0
48+
res = 0
49+
for timestamp, weight in events:
50+
status += weight
51+
res = max(res, status)
52+
return res
53+
54+
# time O(nlogn)
55+
# space O(n)
56+
# using array and line sweep and sort
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
3+
4+
intervals.sort()
5+
6+
res = 0
7+
prev_s, prev_e = intervals[0]
8+
for i in range(1, len(intervals)):
9+
cur_s, cur_e = intervals[i]
10+
if prev_e <= cur_s:
11+
prev_s, prev_e = cur_s, cur_e
12+
else:
13+
if cur_e < prev_e:
14+
prev_s, prev_e = cur_s, cur_e
15+
res += 1
16+
17+
return res
18+
19+
# time O(nlogn)
20+
# space O(1), or due to built in sort's cost
21+
# using array and line sweep and compare two intervals each round and sort and greedy
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution:
2+
def findMinArrowShots(self, points: List[List[int]]) -> int:
3+
4+
points.sort()
5+
6+
res = 0
7+
prev_e = points[0][1]
8+
for i in range(1, len(points)):
9+
cur_s, cur_e = points[i]
10+
if prev_e < cur_s:
11+
res += 1
12+
prev_e = cur_e
13+
else:
14+
prev_e = min(prev_e, cur_e)
15+
16+
res += 1
17+
18+
return res
19+
20+
# time O(nlogn)
21+
# space O(1), not counting built in sort cost
22+
# using array and line sweep and compare two intervals each round and sort and greedy
23+
'''
24+
1. only end ptr matters
25+
'''
26+
27+
class Solution:
28+
def findMinArrowShots(self, points: List[List[int]]) -> int:
29+
30+
points.sort(key = lambda x: x[1])
31+
32+
res = 0
33+
prev_e = points[0][1]
34+
for i in range(1, len(points)):
35+
cur_s, cur_e = points[i]
36+
if prev_e < cur_s:
37+
res += 1
38+
prev_e = cur_e
39+
40+
res += 1
41+
42+
return res
43+
44+
# time O(nlogn)
45+
# space O(1), not counting built in sort cost
46+
# using array and line sweep and compare two intervals each round and sort and greedy
47+
'''
48+
1. only end ptr matters
49+
'''
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution:
2+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
3+
4+
intervals.sort()
5+
6+
res = []
7+
prev_s, prev_e = intervals[0]
8+
for i in range(1, len(intervals)):
9+
cur_s, cur_e = intervals[i]
10+
if prev_e < cur_s:
11+
res.append([prev_s, prev_e])
12+
prev_s, prev_e = cur_s, cur_e
13+
else:
14+
prev_e = max(prev_e, cur_e)
15+
16+
res.append([prev_s, prev_e])
17+
18+
return res
19+
20+
# time O(nlogn)
21+
# space O(n), due to output and sort
22+
# using array and line sweep and compare two intervals each round and sort
23+
24+
class Solution:
25+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
26+
27+
events = []
28+
for s, e in intervals:
29+
events.append((s, 1))
30+
events.append((e, - 1))
31+
events.sort(key = lambda x: (x[0], - x[1]))
32+
33+
res = []
34+
status = 0
35+
prev_timestamp = 0
36+
37+
for timestamp, weight in events:
38+
if status == 0 and weight == 1:
39+
prev_timestamp = timestamp
40+
if status == 1 and weight == - 1:
41+
res.append([prev_timestamp, timestamp])
42+
status += weight
43+
44+
return res
45+
46+
# time O(nlogn)
47+
# space O(n), due to output and sort
48+
# using array and line sweep and sort
49+
'''
50+
1. if certain timestamp have both start and end event, let start event become first
51+
'''

0 commit comments

Comments
 (0)