455 分发饼干
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
m,n=len(g),len(s)
i,j=0,0
ret=0
while i<m and j<n:
if g[i]<=s[j]:
ret+=1
i+=1
j+=1
else:
j+=1
return ret
376 摆动序列
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
l=len(nums)
if l<2:
return l
up=[1]+[0]*(l-1)
down=[1]+[0]*(l-1)
for i in range(1,l):
if nums[i]>nums[i-1]:
up[i]=max(down[i-1]+1,up[i-1])
down[i]=down[i-1]
elif(nums[i]<nums[i-1]):
down[i]=max(up[i-1]+1,down[i-1])
up[i]=up[i-1]
else:
up[i]=up[i-1]
down[i]=down[i-1]
return max(up[l-1],down[l-1])
53 最大子数组和
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
l=len(nums)
temp=[0]*l
for i in range(l):
if i==0:
temp[i]=nums[i]
else:
temp[i]=max(temp[i-1]+nums[i],nums[i])
return max(temp)