LeetCode 455. 分发饼干
贪心就是干
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort(reverse = True)
s.sort(reverse = True)
i = j = 0
res = 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
res += 1
j += 1
i+= 1
return res
LeetCode 53. 最大子数组和
贪心就是干
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
res = float('-inf')
sum_ = 0
for num in nums:
sum_ += num
res = max(sum_, res)
if sum_ < 0:
sum_ = 0
return res
贪心就是干
LeetCode 376. 摆动序列
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
if len(nums) < 2:
return len(nums)
preDiff = 0
# curDiff = 0
res = 1
for i in range(len(nums) - 1):
curDiff = nums[i + 1] - nums[i]
if curDiff != 0 and curDiff * preDiff <= 0:
res += 1
preDiff = curDiff
return res
标签:nums,int,res,sum,随想录,455,len,LeetCode
From: https://www.cnblogs.com/li508q/p/18459241