TheAlgorithms-C/leetcode/src/53.c

14 lines
342 B
C
Raw Normal View History

2019-09-26 23:29:31 +08:00
int maxcmp(int a, int b) { return a >= b ? a : b; }
2019-09-26 23:29:31 +08:00
int maxSubArray(int *nums, int numsSize)
{
2019-09-26 23:29:31 +08:00
int maxSoFar = nums[0], maxEndingHere = nums[0];
for (int i = 1; i < numsSize; i++)
{
2019-09-26 23:29:31 +08:00
maxEndingHere = maxcmp(maxEndingHere + nums[i], nums[i]);
maxSoFar = maxcmp(maxSoFar, maxEndingHere);
}
return maxSoFar;
}