455. 分发饼干:
代码思路
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g = sorted(g)
s = sorted(s)
res = 0
g_index = 0
s_index = 0
while g_index < len(g) and s_index < len(s):
if s[s_index] >= g[g_index]:
g_index += 1
s_index += 1
res += 1
else:
s_index += 1
return res
376. 摆动序列:
代码思路
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
preC,curC,res = 0,0,1
for i in range(len(nums) - 1):
curC = nums[i + 1] - nums[i]
if curC * preC <= 0 and curC !=0:
res += 1
preC = curC
return res
return len(wiggle_list)
53. 最大子数组和:
代码思路
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return nums[0]
else:
max_left = self.maxSubArray(nums[0:len(nums) // 2])
max_right = self.maxSubArray(nums[len(nums) // 2:len(nums)])
max_l = nums[len(nums) // 2 - 1]
tmp = 0
for i in range(len(nums) // 2 - 1, -1, -1):
tmp += nums[i]
max_l = max(tmp, max_l)
max_r = nums[len(nums) // 2]
tmp = 0
for i in range(len(nums) // 2, len(nums)):
tmp += nums[i]
max_r = max(tmp, max_r)
return max(max_right,max_left,max_l+max_r)