Problem
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.
Code
static int var = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max = INT_MIN;
int sum = 0;
for (int i = 0; i<nums.size(); i++) {
if (sum + nums[i] < nums[i])
sum = nums[i];
else
sum += nums[i];
if (max < sum)
max = sum;
}
return max;
}
};
Result
53. Maximum Subarray.png