Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
click to show more practice.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
求数组的子数组之和的最大值。这题曾经在编程之美上看到过,直接给出书上的思路和解法。
解法一:
直接法,记Sum[i, ..., j]为数组A中第i个元素到第j个元素的和,遍历所有可能的Sum[i, ..., j],其中Sum[i, ..., j] = Sum[i, ..., j - 1] + A[j]. 其时间复杂段为O(n^2)
代码:
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) maxsum = -10000000 i = 0 while i < n: csum = 0 j = i while j < n: csum += nums[j] if csum > maxsum: maxsum = csum j += 1 i += 1 return maxsum
解法二:
分治法,将数组(A[0], A[1], ..., A[n - 1])分为长度相等的两段数组(A[0], ..., A[n/2 - 1])和(A[n/2], ..., A[n - 1]),分别求出这两段数组各自的最大子段和,则原数组(A[0], A[1], ..., A[n - 1]的最大子段和为以下三种情况下的最大值:
1. (A[0], A[1], ..., A[n - 1])的最大子段和与(A[0], A[1], ..., A[n/2 - 1])的最大子段和相同;
2. (A[0], A[1], ..., A[n - 1])的最大子段和与(A[0], A[1], ..., A[n - 1])的最大子段和相同;
3. (A[0], A[1], ..., A[n - 1])的最大子段和跨过A[n/2 - 1]和A[n/2]
第1和2两种情况可以根据递归可得,对于第3种情况,我们要找到以A[n/2 - 1]结尾的和最大的一段数组之和s1 = (A[0], A[1], ..., A[n/2 - 1]) (0<=i<n/2 - 1)和以A[n/2]开始的最大的一段数组之和s2 = (A[n/2], ..., A[j]) (n/2 <= j < n).那么第三种情况的最大值为s1 + s2 = A[i] + ... + A[n/2 - 1] + A[n/2] + ... + A[j],只需要对原数组进行一次遍历即可。
其时间复杂段为O(n*logn).
代码:
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ return self.findMaxSubArray(nums, 0, len(nums) - 1) def findMaxCrossSubarray(self, nums, low, mid, high): # 跨越 INIFINITE = -1000000 left_sum = INIFINITE csum = 0 i = mid ''' 计算以A[n/2 - 1]为结尾的最大的一段子数组之和 ''' while i >= low: csum += nums[i] if csum > left_sum: left_sum = csum i -= 1 right_sum = INIFINITE csum = 0 i = mid + 1 ''' 计算以A[n/2]开始的最大的一段子数组之和 ''' while i <= high: csum += nums[i] if csum > right_sum: right_sum = csum i += 1 return left_sum + right_sum def findMaxSubArray(self, nums, low, high): if low == high: return nums[low] else: mid = (low + high) / 2 left = self.findMaxSubArray(nums, low, mid) right = self.findMaxSubArray(nums, mid + 1, high) cross = self.findMaxCrossSubarray(nums, low, mid, high) return max(left, cross, right)
解法3:
动态规划法。考虑数组的第一个元素A[0],以及最大的一段数组(A[i], ..., A[j])跟A[0]之间的关系,有以下几种情况:
1. 当0 = i = j时,元素A[0]本身构成和最大的一段;
2. 当0 = i < j时,和最大的一段以A[0]开始;
3. 当0 < i时,元素A[0]跟和最大的一段没有关系。
从上面三种情况可以看出,可以将一个大问题(n个元素的数组)转化为一个较小的问题(n - 1个元素的数组)。
设(A[1], ..., A[n - 1])中的最大一段数组之和为All[1],设(A[1], ..., A[n - 1])中包含A[1]的最大一段数组为Start[1]。
则 (A[0], A[1], ..., A[n - 1])的最大一段数组之和All[0]为max{A[0], A[0] + Start[1], All[1]}.
时间复杂度为O(n).
代码:
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) start = [0] * n start[n - 1] = nums[n - 1] All = [0] * n All[n - 1] = nums[n - 1] i = n - 2 while i >= 0: start[i] = max(nums[i], nums[i] + start[i + 1]) All[i] = max(start[i], All[i + 1]) i -= 1 return All[0]
代码:
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) nstart = nums[n - 1] nAll = nums[n - 1] i = n - 2 while i >= 0: nstart = max(nums[i], nums[i] + nstart) nAll = max(nstart, nAll) i -= 1 return nAll