求股票的最大利润,简言之就是求一个数组里面元素差的最大值,要求时间复杂度O(n)
#include <stdio.h>
#include <stdlib.h>
int maxDiff(int *number, int length)
{
if (NULL == number || length < 2)
{
return 0;
}
int min = number[0];
int max = number[0];
for (int i = 1; i < length; ++i)
{
if (min > number[i])
{
min = number[i];
}
if (max < number[i])
{
max = number[i];
}
}
return max - min;
}
int main()
{
int a[] = {9, 11, 8, 5, 7, 12, 16, 14};
int value = maxDiff(a, sizeof(a) / sizeof(int));
printf("value id %d\n", value);
return 0;
}
value id 11
联系客服