53. Maximum Subarray

Given an integer array `nums`, find the subarray with the largest sum, and return its sum.

Example 1:

``````Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
``````

Example 2:

``````Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.
``````

Example 3:

``````Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
``````

Constraints:

• `1 <= nums.length <= 10^5`
• `-10^4 <= nums[i] <= 10^4`

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.

C++ 解法一:

``````class Solution {
public:
int maxSubArray(vector<int>& nums) {
int res = INT_MIN, curSum = 0;
for (int num : nums) {
curSum = max(curSum + num, num);
res = max(res, curSum);
}
return res;
}
};
``````

Java 解法一:

``````public class Solution {
public int maxSubArray(int[] nums) {
int res = Integer.MIN_VALUE, curSum = 0;
for (int num : nums) {
curSum = Math.max(curSum + num, num);
res = Math.max(res, curSum);
}
return res;
}
}
``````

C++ 解法二:

``````class Solution {
public:
int maxSubArray(vector<int>& nums) {
if (nums.empty()) return 0;
return dfs(nums, 0, (int)nums.size() - 1);
}
int dfs(vector<int>& nums, int left, int right) {
if (left >= right) return nums[left];
int mid = left + (right - left) / 2, leftSum = 0, rightSum = 0;
for (int i = mid - 1, curSum = 0; i >= left; --i) {
curSum += nums[i];
leftSum = max(leftSum, curSum);
}
for (int i = mid + 1, curSum = 0; i <= right; ++i) {
curSum += nums[i];
rightSum = max(rightSum, curSum);
}
return max({dfs(nums, left, mid - 1), dfs(nums, mid + 1, right), leftSum + nums[mid] + rightSum});
}
};
``````

Java 解法二:

``````public class Solution {
public int maxSubArray(int[] nums) {
if (nums.length == 0) return 0;
return dfs(nums, 0, nums.length - 1);
}
public int dfs(int[] nums, int left, int right) {
if (left >= right) return nums[left];
int mid = left + (right - left) / 2, leftSum = 0, rightSum = 0;
for (int i = mid - 1, curSum = 0; i >= left; --i) {
curSum += nums[i];
leftSum = Math.max(leftSum, curSum);
}
for (int i = mid + 1, curSum = 0; i <= right; ++i) {
curSum += nums[i];
rightSum = Math.max(rightSum, curSum);
}
return Math.max(Math.max(dfs(nums, left, mid - 1), dfs(nums, mid + 1, right)), leftSum + nums[mid] + rightSum);
}
}
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/53

Best Time to Buy and Sell Stock

Maximum Product Subarray

Degree of an Array

Longest Turbulent Subarray

Maximum Absolute Sum of Any Subarray

Maximum Subarray Sum After One Operation

Maximum Score Of Spliced Array

Substring With Largest Variance

Count Subarrays With Score Less Than K

Maximum Value of a String in an Array

Find the Substring With Maximum Cost

K Items With the Maximum Sum

https://leetcode.com/problems/maximum-subarray/

https://leetcode.com/problems/maximum-subarray/discuss/20211/Accepted-O(n)-solution-in-java

https://leetcode.com/problems/maximum-subarray/discuss/20193/DP-solution-and-some-thoughts

https://leetcode.com/problems/maximum-subarray/discuss/20200/Share-my-solutions-both-greedy-and-divide-and-conquer

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation