-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0121_Best-Time-to-Buy-and-Sell-Stock.cpp
More file actions
44 lines (42 loc) · 1.19 KB
/
0121_Best-Time-to-Buy-and-Sell-Stock.cpp
File metadata and controls
44 lines (42 loc) · 1.19 KB
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
// Simple DP:
class Solution {
public:
int maxProfit(vector<int> &prices) {
// dp[i] -> maximum value in range [i, end]
// formulation:
vector<int> dp(prices.size(), 0);
dp[prices.size() - 1] = prices.back();
for (int i = prices.size() - 2; i >= 0; --i) {
dp[i] = max(dp[i + 1], prices[i]);
}
int ret = 0;
for (int i = 0; i < prices.size(); ++i) {
// if we buy today, the possible maximum value is dp[i]
ret = max(ret, dp[i] - prices[i]);
}
return ret;
}
};
// Old DP:
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() <= 1)
return 0;
vector<int> delta(prices.size() - 1, 0);
for (int i = 0; i < prices.size() - 1; ++i) {
delta[i] = prices[i + 1] - prices[i];
}
vector<int> dp(delta.size(), 0);
dp[0] = delta[0];
int answer = dp[0];
for (int i = 1; i < dp.size(); ++i) {
dp[i] = max(dp[i - 1], 0) + delta[i];
answer = max(answer, dp[i]);
}
if (answer < 0)
return 0;
else
return answer;
}
};