Kadane's Algorithm
對於新的元素有兩個選擇:
curSum + num
num
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int curSum = 0;
int maxSum = INT_MIN;
for (int num : nums)
{
curSum = max(num, curSum + num);
maxSum = max(maxSum, curSum);
}
return maxSum;
}
};
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up