classSolution: defcanJump(self, nums): max_can_reach = 0 for i inrange(len(nums)): #注意 if i <= max_can_reach: max_can_reach = max(nums[i] + i, max_can_reach) if max_can_reach >= len(nums) - 1: returnTrue returnFalse
classSolution: defwiggleMaxLength(self, nums: List[int]) -> int: iflen(nums)==1: return1 direction = None res = 0 for i inrange(1, len(nums)): if nums[i] == nums[i-1]: continue elif nums[i] > nums[i-1]: if direction == 0: continue direction = 0 res += 1 elif nums[i] < nums[i-1]: if direction == 1: continue direction = 1 res += 1 return res + 1
分发饼干[455]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: deffindContentChildren(self, g: List[int], s: List[int]) -> int: # 将胃口和饼干排序 g.sort() s.sort() # 孩子的数量 n = len(g) # 饼干的数量 m = len(s) # 记录结果 res = 0 for i inrange(m): # 从胃口小的开始喂 if res < n and g[res] <= s[i]: res += 1 return res
最大子序和[53]
1 2 3 4 5 6 7 8 9 10 11
classSolution: defmaxSubArray(self, nums): result = float('-inf') # 初始化结果为负无穷大 count = 0 for i inrange(len(nums)): count += nums[i] if count > result: # 取区间累计的最大值(相当于不断确定最大子序终止位置) result = count if count <= 0: # 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和 count = 0 return result
# 正常解法 classSolution: defcandy(self, ratings: List[int]) -> int: left = [1] * len(ratings) right = [1] * len(ratings) for i inrange(1, len(ratings)): if ratings[i] > ratings[i - 1]: left[i] = left[i - 1] + 1 for i inrange(len(ratings) - 2, -1, -1): if ratings[i] > ratings[i + 1]: #注意 right[i] = right[i+1] + 1 s = 0 for i inrange(len(left)): s = s + max(left[i], right[i]) return s
classSolution: deflemonadeChange(self, bills: List[int]) -> bool: five = 0 ten = 0 twenty = 0 for i in bills: if i==5: five += 1 elif i==10: if five>0: five -=1 ten += 1 else: returnFalse elif i==20: if ten>0and five>0: ten -= 1 five -=1 twenty += 1 elif five>=3: five -= 3 twenty += 1 else: returnFalse returnTrue
根据身高重建队列[406]
1 2 3 4 5 6 7
classSolution: defreconstructQueue(self, people: List[List[int]]) -> List[List[int]]: people.sort(key=lambda x:(-x[0],x[1])) res = [] for i in people: res.insert(i[1], i) return res
classSolution: deffindMinArrowShots(self, points: List[List[int]]) -> int: result = 1 points.sort(key = lambda x: x[0]) for i inrange(1, len(points)): if points[i][0] > points[i-1][1]: result = result + 1 else: points[i][1] = min(points[i][1], points[i-1][1]) return result
无重叠区间[435]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: deferaseOverlapIntervals(self, intervals: List[List[int]]) -> int: ifnot intervals: return0 intervals.sort(key=lambda x: x[0]) # 按照左边界升序排序 result = 1# 不重叠区间数量,初始化为1,因为至少有一个不重叠的区间 for i inrange(1, len(intervals)): if intervals[i][0] >= intervals[i - 1][1]: # 没有重叠 result += 1 else: # 重叠情况 intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]) # 更新重叠区间的右边界 returnlen(intervals) - result
划分字母区间[763]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defpartitionLabels(self, s: str) -> List[int]: from collections import defaultdict d = defaultdict(int) for index, value inenumerate(s): d[value] = index end = 0 results = [] start = 0 for index, value inenumerate(s): end = max(end, d[value]) if index == end: results.append(index-start+1) start = index+1 return results
合并区间[56]
1 2 3 4 5 6 7 8 9 10 11
classSolution: defmerge(self, intervals: List[List[int]]) -> List[List[int]]: intervals.sort() stack = [] for i in intervals: if stack and i[0] <= stack[-1][1]: v = stack.pop() stack.append([min(v[0],i[0]), max(v[1],i[1])]) else: stack.append(i) return stack
单调递增的数字[738]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defmonotoneIncreasingDigits(self, n: int) -> int: s = [int(i) for i instr(n)] max_idx = 0 for i inrange(1, len(s)): if s[i] > s[i-1]: max_idx = i elif s[i] == s[i-1]: continue elif s[i] < s[i-1]: s[max_idx] = s[max_idx] - 1 for j inrange(max_idx+1, len(s)): s[j] = 9 break returnint("".join([str(k) for k in s]))
classSolution: defisPossible(self, nums: List[int]) -> bool: res = [] for n in nums: for v in res: if n == v[-1] + 1: v.append(n) break else: res.insert(0,[n]) returnall([len(v)>=3for v in res])