Last active
November 4, 2018 12:29
Star
You must be signed in to star a gist
[leetcode] 53. Maximum Subarray : https://leetcode.com/problems/maximum-subarray/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxSubArray(vector<int>& nums) { | |
int ans = nums[0]; | |
int subSum = 0; | |
for(int i=0;i<nums.size();i++){ | |
subSum += nums[i]; | |
ans = max(subSum, ans); | |
if(subSum < 0) | |
subSum = 0; | |
} | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment