mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
18 lines
465 B
C
18 lines
465 B
C
int maxcmp(int a, int b) { return (a >= b) ? a : b; }
|
|
|
|
/* max subarray problem by using Kadane's Algorithm
|
|
*/
|
|
int maxProfit(int *prices, int pricesSize)
|
|
{
|
|
/* maxCur: current maximum
|
|
* maxSoFar: found maximum for subarray so far
|
|
*/
|
|
int maxCur = 0, maxSoFar = 0;
|
|
for (int i = 1; i < pricesSize; i++)
|
|
{
|
|
maxCur = maxcmp(0, maxCur + prices[i] - prices[i - 1]);
|
|
maxSoFar = maxcmp(maxSoFar, maxCur);
|
|
}
|
|
return maxSoFar;
|
|
}
|