-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlintcode1000.cpp
More file actions
31 lines (28 loc) · 982 Bytes
/
lintcode1000.cpp
File metadata and controls
31 lines (28 loc) · 982 Bytes
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
class Solution {
public:
/**
* @param prices: a list of integers
* @param fee: a integer
* @return: return a integer
*/
int maxProfit(vector<int> &prices, int fee) {
int n = prices.size();
if(n <= 1 || fee <= 0)
return 0;
/**
* 狀態轉移方程
* pay fee on selling
* dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
* dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
* 一樣是跟鄰居作業而已 不需要整個dp陣列
*/
int not_hold = 0, hold = -prices[0];
for(int i = 0; i < n; ++i) {
int tmp = not_hold;
not_hold = max(not_hold, hold + prices[i] - fee);
// 注意前面not_hold已經被動過了,所以我先用一個變量把前一天的not_hold存起來
hold = max(hold, tmp - prices[i]);
}
return not_hold;
}
};