目录
学习目标
- 理论基础
- 455.分发饼干
- 376. 摆动序列
- 53. 最大子序和
学习内容
理论基础
455.分发饼干
455. 分发饼干 - 力扣(LeetCode)
https://leetcode.cn/problems/assign-cookies/
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
res = 0
g.sort()
s.sort()
index = 0
n = len(s)
for child in g:
while index<n and s[index]<child:
index+=1
if index==n:
break
else:
res+=1
index+=1
return res
376. 摆动序列
376. 摆动序列 - 力扣(LeetCode)
https://leetcode.cn/problems/wiggle-subsequence/
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
preC,curC,res = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度
for i in range(len(nums) - 1):
curC = nums[i + 1] - nums[i]
if curC * preC <= 0 and curC !=0: #差值为0时,不算摆动
res += 1
preC = curC #如果当前差值和上一个差值为一正一负时,才需要用当前差值替代上一个差值
return res
53. 最大子序和
53. 最大子数组和 - 力扣(LeetCode)
https://leetcode.cn/problems/maximum-subarray/
import math
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
res = -math.inf
amount = 0
for num in nums:
if amount<0:
amount = num
else:
amount+=num
res = max(res,amount)
return res