# #41 Maximum Subarray

Given an array of integers, find a contiguous subarray which has the largest sum.

##### Notice

The subarray should contain at least one number.

Example

Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.

Challenge

Can you do it in time complexity O(n)?

Mycode（AC = 28ms）：

class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
if (nums.size() == 0) return 0;

int local = nums[0], global = nums[0];
for (int i = 1; i < nums.size(); i++) {
local = max(local + nums[i], nums[i]);
global = max(global, local);
}

return global;
}
};


#### [LeetCode] Maximum Product Subarray的4种解法

2014-09-26 12:11:40

#### LeetCode Maximum Product Subarray 解题报告

2014-10-06 12:09:35

#### Maximum Subarray -- LeetCode

2014-03-20 09:24:41

#### Maximum Subarray (Java)

2015-01-18 11:16:03

#### Lintcode 617：Maximum Average Subarray

2017-02-21 20:27:34

#### LeetCode-53-Maximum Subarray(DP/分治)-Medium

2016-01-16 21:07:20

#### 动态规划之Maximum Subarray

2015-09-20 20:26:54

#### Lintcode - Maximum Subarray III

2015-03-24 05:21:00

#### 54.Maximum Subarray（动态规划）

2016-01-09 23:02:38