forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve2.c
44 lines (44 loc) · 761 Bytes
/
solve2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
static inline int MIN(int a, int b)
{
return a < b ? a : b;
}
static inline int MAX(int a, int b)
{
return a > b ? a : b;
}
int maxProfit(int *a, int n)
{
int max = a[n - 1];
int profit = 0;
for (int i = n - 2; i >= 0; --i) {
max = MAX(max, a[i]);
profit = MAX(profit, max - a[i]);
}
return profit;
}
/*
int maxProfit(int *a, int n)
{
int min = a[0];
int max = 0;
for (int i = 1; i < n; ++i) {
min = MIN(min, a[i]);
max = MAX(max, a[i] - min);
}
return max;
}
*/
int main(int argc, char **argv)
{
int a[20], n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i)
scanf("%d", a + i);
printf("maxProfit = %d\n", maxProfit(a, n));
}
return 0;
}