-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode149.cpp
64 lines (57 loc) · 1.8 KB
/
lintcode149.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
int n = prices.size();
if(n <= 1)
return 0;
/** 這題dp不需要開三維
* 因為k只能有一次
* dp[i][0]: i-th day, 沒持有股票的最大利益
* dp[i][1]: i-th day, 持有股票的最大利益
* 狀態轉移:
* dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
* dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
* base case:
* dp[0][0] = 0
* 注意這裡不是INT_MIN
* 最多只有一次交易,所以一但持有股票,那一定是-prices[i]
* dp[0][1] = -prices[i];
*/
vector<vector<int>> dp(n, vector<int>(2));
for(int i = 0; i < n; ++i) {
if(i == 0) {
dp[0][0] = 0;
dp[0][1] = -prices[i];
continue;
}
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = max(dp[i - 1][1], - prices[i]);
}
return dp[n - 1][0];
}
};
// 把空間複查度降到O(1)
// 整個dp陣列我們每次其實只用到隔壁那位而已
// 因此用一個變量代替就好
class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
int n = prices.size();
if(n <= 1)
return 0;
int no_stock = 0, has_stock = -prices[0];
for(int i = 0; i < n; ++i) {
no_stock = max(no_stock, has_stock + prices[i]);
has_stock = max(has_stock, -prices[i]);
}
return no_stock;
}
};