435. Non-overlapping Intervals
Given an array of intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Record the number of non-crossing intervals from left to right.
Finally, subtract the number of non-crossing intervals from the total number of intervals, and that's the number of intervals to remove.
Time complexity: O(nlog n) with a Quicksort
Space complexity: O(n)
AC mysolution:
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key = lambda x: (x[0],x[1]))
result = 0
right = intervals[0][1]
for i in range(len(intervals) - 1):
if right <= intervals[i + 1][0]:
right =intervals[i + 1][1]
elif right > intervals[i + 1][0] and right <= intervals[i + 1][1]:
result += 1
elif right > intervals[i + 1][0] and right > intervals[i + 1][1]:
result += 1
right = intervals[i + 1][1]
return result
bro Ka solution:
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
if not intervals:
return 0
intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序
count = 0 # 记录重叠区间数量
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i - 1][1]: # 存在重叠区间
intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界
count += 1
return count
You are given a string s
. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s
.
Return a list of integers representing the size of these parts
Time complexity: O(n)
Space complexity: O(1), using hash arrays is fixed size
class Solution:
def partitionLabels(self, s: str) -> List[int]:
last_occurrence = {}
for i,l in enumerate(s):
last_occurrence[l] = i
result = []
start = 0
end = 0
for i,l in enumerate(s):
end = max(end, last_occurrence[l])
if i == end:
result.append(end - start + 1)
start = i + 1
return result
Given an array of intervals
where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
AC:
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key = lambda x: (x[0], x[1]))
result = []
left = intervals[0][0]
right = intervals[0][1]
for i in range(len(intervals) - 1):
if right >= intervals[i + 1][0] and right < intervals[i + 1][1]:
right = intervals[i + 1][1]
elif right >= intervals[i + 1][0] and right >= intervals[i + 1][1]:
continue
else: # elif right < intervals[i + 1][0]:
result.append([left,right])
left = intervals[i + 1][0]
right = intervals[i + 1][1]
result.append([left,right])
return result
AC simplified version:
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key = lambda x: (x[0], x[1]))
result = []
left = intervals[0][0]
right = intervals[0][1]
for i in range(len(intervals) - 1):
if right >= intervals[i + 1][0]: #and right < intervals[i + 1][1]:
right = max(right, intervals[i + 1][1])
#elif right >= intervals[i + 1][0] and right >= intervals[i + 1][1]:
# continue
else: # elif right < intervals[i + 1][0]:
result.append([left,right])
left = intervals[i + 1][0]
right = intervals[i + 1][1]
result.append([left,right])
return result
bro KA's solution:
class Solution:
def merge(self, intervals):
result = []
if len(intervals) == 0:
return result # 区间集合为空直接返回
intervals.sort(key=lambda x: x[0]) # 按照区间的左边界进行排序
result.append(intervals[0]) # 第一个区间可以直接放入结果集中
for i in range(1, len(intervals)):
if result[-1][1] >= intervals[i][0]: # 发现重叠区间
# 合并区间,只需要更新结果集最后一个区间的右边界,因为根据排序,左边界已经是最小的
result[-1][1] = max(result[-1][1], intervals[i][1])
else:
result.append(intervals[i]) # 区间不重叠
return result