[LeetCode] Maximum Subarray Sum



Dynamic Programming

There is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum. When we visit each num in nums, addnum to sum, then update smax if necessary or reset sum to 0 if it becomes negative.

The code is as follows.

 1 class Solution {
 2 public:
 3     int maxSubArray(vector<int>& nums) {
 4         int sum = 0, smax = INT_MIN;
 5         for (int num : nums) {
 6             sum += num;
 7             if (sum > smax) smax = sum;
 8             if (sum < 0) sum = 0;
 9         }
10         return smax;
11     }
12 };


Divide and Conquer

The DC algorithm breaks nums into two halves and find the maximum subarray sum in them recursively. Well, the most tricky part is to handle the case that the maximum subarray may span the two halves. For this case, we use a linear algorithm: starting from the middle element and move to both ends (left and right ends), record the maximum sum we have seen. In this case, the maximum sum is finally equal to the middle element plus the maximum sum of moving leftwards and the maximum sum of moving rightwards.

Well, the code is just a translation of the above idea.

 1 class Solution {
 2 public:
 3     int maxSubArray(vector<int>& nums) {
 4         int smax = INT_MIN, n = nums.size();
 5         return maxSub(nums, 0, n - 1, smax);
 6     }
 7 private:
 8     int maxSub(vector<int>& nums, int l, int r, int smax) {
 9         if (l > r) return INT_MIN;
10         int m = l + ((r - l) >> 1);
11         int lm = maxSub(nums, l, m - 1, smax); // left half
12         int rm = maxSub(nums, m + 1, r, smax); // right half
13         int i, sum, ml = 0, mr = 0;
14         // Move leftwards
15         for (i = m - 1, sum = 0; i >= l; i--) {
16             sum += nums[i];
17             ml = max(sum, ml);
18         }
19         // Move rightwards
20         for (i = m + 1, sum = 0; i <= r; i++) {
21             sum += nums[i];
22             mr = max(sum, mr);
23         }
24         return max(smax, max(ml + mr + nums[m], max(lm, rm)));
25     }
26 };
时间: 2024-08-28 05:52:47

[LeetCode] Maximum Subarray Sum的相关文章

LeetCode——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. 原题链接: https://oj.leetcode.com/p

LeetCode: Maximum Subarray [052]

[题目] 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. [题意] 给定一个数组,找出和最大的子数组,返回这个

[leetcode]Maximum Subarray @ Python

原题地址:https://oj.leetcode.com/problems/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,

leetcode | Maximum Subarray 最大连续子序列的和

Maximum Subarray: https://leetcode.com/problems/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 [

LeetCode: Maximum Subarray 解题报告

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. SOLUTION 1: 采用滑

leetcode Maximum Subarray 最大子序列

Maximum Subarray Total Accepted: 28381 Total Submissions: 83696 My Submissions 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 contiguo

LEETCODE —— Maximum Subarray [一维DP]

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. More practice:

LeetCode:Continuous Subarray Sum

523. Continuous Subarray Sum Add to List Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where

Leetcode: 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. More practice: If you have