mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
14 lines
342 B
C
14 lines
342 B
C
|
|
int maxcmp(int a, int b) { return a >= b ? a : b; }
|
|
|
|
int maxSubArray(int *nums, int numsSize)
|
|
{
|
|
int maxSoFar = nums[0], maxEndingHere = nums[0];
|
|
for (int i = 1; i < numsSize; i++)
|
|
{
|
|
maxEndingHere = maxcmp(maxEndingHere + nums[i], nums[i]);
|
|
maxSoFar = maxcmp(maxSoFar, maxEndingHere);
|
|
}
|
|
return maxSoFar;
|
|
}
|