class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g.sort()
s.sort()
ans = 0
if len(g) == 0 or len(s) == 0:
return 0
times = len(g)
while times > 0:
if len(s) == 0:
break
if g[-1] <= s[-1]:
ans += 1
s.pop()
g.pop()
times -= 1
return ans
题解如下:
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
ans = 0
index = len(s)-1
for i in range(len(g)-1, -1, -1):
if index >= 0 and g[i] <= s[index]:
ans += 1
index -= 1
return ans
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
preDiff = 0
curDiff = 0
res = 1 #默认最后端有一峰值
for i in range(len(nums)-1):
curDiff = nums[i+1] - nums[i]
if preDiff * curDiff <= 0 and curDiff !=0:
res += 1
preDiff = curDiff
return res