-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path123. Best Time to Buy and Sell Stock III.c
41 lines (29 loc) · 1.19 KB
/
123. Best Time to Buy and Sell Stock III.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
/*
123. Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
*/
int maxProfit(int* prices, int pricesSize) {
int buy1, buy2; // max profit if I buy
int sell1, sell2; // max profit if I sell
int i;
buy1 = buy2 = 0x80000000; // min of integer
sell1 = sell2 = 0;
for (i = 0; i < pricesSize; i ++) {
// possible profit
buy1 = buy1 > 0 - prices[i] ? buy1 : 0 - prices[i];
sell1 = sell1 > buy1 + prices[i] ? sell1 : buy1 + prices[i];
buy2 = buy2 > sell1 - prices[i] ? buy2 : sell1 - prices[i];
sell2 = sell2 > buy2 + prices[i] ? sell2 : buy2 + prices[i];
}
return sell2;
}
/*
Difficulty:Hard
Total Accepted:88.8K
Total Submissions:303.4K
Related Topics Array Dynamic Programming
Similar Questions Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock II Best Time to Buy and Sell Stock IV
*/