一旦有重叠区域,则用min更新右边界
class Solution(object):def findMinArrowShots(self, points):""":type points: List[List[int]]:rtype: int"""points.sort(key=lambda x:x[0])if len(points)==0:return 0count = 1for i in range(1, len(points)):if points[i][0] > points[i-1][1]:count += 1else:points[i][1] = min(points[i-1][1],points[i][1])return count
跟上题一模一样
class Solution(object):def eraseOverlapIntervals(self, intervals):""":type intervals: List[List[int]]:rtype: int"""intervals.sort(key=lambda x:x[0])if len(intervals)==0:return 0count = 0for i in range(1, len(intervals)):if intervals[i][0] < intervals[i-1][1]:count += 1intervals[i][1] = min(intervals[i-1][1],intervals[i][1])return count
第三题
构建哈希表,记录每个字母最大位置缩影,重新遍历,不断更新最大位置索引
class Solution(object):def partitionLabels(self, s):""":type s: str:rtype: List[int]"""hashmap = {}result = [] for i in range(len(s)):hashmap[s[i]] = ileft,right = 0, 0for i in range(len(s)):right = max(hashmap[s[i]],right) #不断更新最大位置索引,直到i==最大位置索引if right == i:result.append(right-left+1)#返回长度left = i + 1return result