forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
42 lines (33 loc) · 993 Bytes
/
main2.cpp
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
/// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/
/// Author : liuyubobobo
/// Time : 2017-10-22
#include <iostream>
#include <vector>
using namespace std;
/// Using hold and cash to trace the max money in different state
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() == 0)
return 0;
int hold1 = INT_MIN;
int cash1 = 0;
int hold2 = INT_MIN;
int cash2 = 0;
for(int price : prices) {
cash2 = max(cash2, hold2 + price);
hold2 = max(hold2, cash1 - price);
cash1 = max(cash1, hold1 + price);
hold1 = max(hold1, -price);
}
return cash2;
}
};
int main() {
int prices1[] = {1, 2};
vector<int> pricesVec1(prices1, prices1 + sizeof(prices1)/sizeof(int));
cout << Solution().maxProfit(pricesVec1) << endl;
return 0;
}