forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path122.c
36 lines (32 loc) · 737 Bytes
/
122.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
#include <stdio.h>
int maxProfit(int prices[], int n){
int i;
int ret = 0;
for (i = 1; i < n; i++) {
int t = prices[i] - prices[i - 1];
if (t > 0)
ret += t;
}
return ret;
}
/*
int maxProfit(int prices[], int n){
int i;
int ret = 0;
int min = prices[0];
for (i = 1; i < n; i++) {
if (prices[i] < prices[i - 1]) {
ret += prices[i - 1] - min;
min = prices[i];
}
}
if (prices[n - 1] > min)
ret += prices[n - 1] - min;
return ret;
}
*/
int main() {
int prices[] = {1, 2, 3, 2, 4};
printf("%d\n", maxProfit(prices, sizeof(prices)/sizeof(prices[0])));
return 0;
}