标签:lin inline sub glob not tput divide str self
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the \(O(n)\) solution, try coding another solution using the divide and conquer approach, which is more subtle.
找出一个数组连续子序列的最大和
子序列要求连续,子序列向右扩展分为两种情况
子序列的局部最大和为这两种情况中和最大的那个
在所有的局部最大和中找出全局最大和
写出一个算法复杂度为\(O(n)\)的版本
def maxSubArray(self, nums: List[int]) -> int:
maximum = nums[0]
global_maximum = nums[0]
for i in nums[1:]:
maximum = max(maximum + i, i)
global_maximum = max(maximum, global_maximum)
return global_maximum
[LeetCode] 53. Maximum Subarray
标签:lin inline sub glob not tput divide str self
原文地址:https://www.cnblogs.com/arcsinw/p/12913102.html