cpp-interview/Problems/LeetcodeProblems/maximum-subarray.h

13 lines
320 B
C
Raw Normal View History

2018-02-09 21:47:58 +08:00
#define MIN_INT -2147483648
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max = MIN_INT, temp = 0;
for(auto i = 0; i < nums.size(); ++i) {
temp += nums[i];
if(temp > max) max = temp;
if(temp < 0) temp = 0;
}
return max;
}
};