classSolution: deffindContentChildren(self, g: List[int], s: List[int]) -> int: # 先满足大胃口 | 此时胃口在移动 g.sort(reverse=True) s.sort(reverse=True) # 从大到小 index = 0 for i inrange(len(g)): if index<len(s) and s[index]>=g[i]: index+=1 return index
classSolution: deffindContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() index = 0 for i inrange(len(s)): if index<len(g) and s[i]>=g[index]: index+=1 return index
376、摆动序列(*)
计算峰值
prediff = nums[i] - nums[i-1]
curdiff = nums[i+1] - nums[i]
如果prediff>0 and curdiff < 0 或者 prediff < 0 and curdiff > 0,就需要统计波动
有三种特殊情况需要考虑:
情况一:上下坡中有平坡
情况二:数组首尾两端
情况三:单调坡中有平坡
情况一:上下坡中有平坡
可以统一规则,删除左边的3个2
情况二:数组首尾两端
如果数组中只包含两个元素,如果这两个元素不相同则摆动序列也为2
例如序列[2, 5],如果靠统计差值计算峰值,需要有三个数才能进行统计,这时候,我们可以假定这个序列为[2,2,5],这样preDiff=0 and curDiff=3,满足我们的条件,res+=1,就可以统一写法,不需要单独把len(nums)==2的情况单独拎出来了。
针对上述情况,我们可以假设res = 1,默认最右边有一个峰值
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defwiggleMaxLength(self, nums: List[int]) -> int: iflen(nums)<=1: returnlen(nums) res = 1 curDiff = 0 preDiff = 0 for i inrange(len(nums)-1): curDiff = nums[i+1] - nums[i] if (preDiff>=0and curDiff < 0) or (preDiff<=0and curDiff > 0): res+=1 preDiff = curDiff return res
classSolution: defmaxSubArray(self, nums: List[int]) -> int: res = float("-inf") count = 0 for num in nums: count += num res = max(res, count) if count < 0: count = 0 return res
classSolution: defcanJump(self, nums: List[int]) -> bool: iflen(nums)==1: returnTrue max_reach = 0 for i inrange(len(nums)): if i > max_reach: returnFalse max_reach = max(nums[i]+i, max_reach) if max_reach >= len(nums)-1: returnTrue returnFalse
在Python中无法动态修改for循环中的循环变量,可以改用while循环
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defcanJump(self, nums: List[int]) -> bool: iflen(nums)==1: returnTrue i = 0 reach = 0 while i <= reach: reach = max(reach, nums[i]+i) if reach>=len(nums)-1: returnTrue i += 1 returnFalse
1 2 3 4 5 6 7 8 9 10 11
classSolution: defcanJump(self, nums: List[int]) -> bool: n = len(nums) res = 0# 记录覆盖范围 for i inrange(n): if i > res: returnFalse res = max(res, i + nums[i]) if res >= n - 1: returnTrue returnFalse
Java代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution{ publicbooleancanJump(int[] nums){ int n = nums.length; int res = 0; for (int i = 0; i <= nums.length; i++) { if (i > res) { returnfalse; } res = Math.max(res, i + nums[i]); if (res >= nums.length - 1) { returntrue; } } returnfalse; } }
classSolution: defjump(self, nums: List[int]) -> int: cur_reach = 0# 当前能够到达的最远距离 next_reach = 0# 下一步能够到达的最远距离 res = 0# 记录走的步数 for i inrange(len(nums)-1): next_reach = max(next_reach, nums[i]+i) if i == cur_reach: res +=1 cur_reach = next_reach if next_reach>=len(nums)-1: return res return res
classSolution: defjump(self, nums: List[int]) -> int: cur_reach = 0 next_reach = 0 res = 0 n = len(nums) for i inrange(n-1): next_reach = max(next_reach, i + nums[i]) if i == cur_reach: res += 1 cur_reach = next_reach if next_reach >= n - 1: return res return res
classSolution: defreconstructQueue(self, people: List[List[int]]) -> List[List[int]]: people.sort(key=lambda x:(x[0],-x[1]),reverse=True) # print(people) res = [] for po in people: pos = po[1] res.insert(pos,po) return res
classSolution: deferaseOverlapIntervals(self, intervals: List[List[int]]) -> int: res = 0 intervals.sort() for i inrange(1,len(intervals)): if intervals[i][0]<intervals[i-1][1]: res+=1 intervals[i][1] = min(intervals[i][1],intervals[i-1][1]) return res
classSolution: defpartitionLabels(self, s: str) -> List[int]: myhash = defaultdict(int) for i inrange(len(s)): myhash[s[i]] = i # print(myhash) res = [] left = 0 right = 0# 右指针 结果是切割点 for i inrange(len(s)): right = max(right,myhash[s[i]]) if right==i: res.append(right-left+1) left = i + 1 return res