-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path123.cpp
More file actions
51 lines (40 loc) · 1.4 KB
/
Copy path123.cpp
File metadata and controls
51 lines (40 loc) · 1.4 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
45
46
47
48
49
50
51
// Best Time to Buy and Sell Stock III
// HARD
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(2, vector<int>(3, 0)));
for (int i = n - 1; i >= 0; i --) {
for (int k = 1; k <= 2; k ++) {
dp[i][1][k] = max(-prices[i] + dp[i + 1][0][k], dp[i + 1][1][k]);
dp[i][0][k] = max(prices[i] + dp[i][1][k - 1], dp[i + 1][0][k]);
}
}
return dp[0][1][2];
}
};
// Memoisation
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
memo.resize(n, vector<vector<int>>(2, vector<int>(3, -1)));
return profitHelper(prices, 0, 1, 2);
}
private:
vector<vector<vector<int>>> memo;
int profitHelper(vector<int>& prices, int pos, int buy, int cnt) {
if (cnt < 0 || pos == prices.size()) return 0;
if (memo[pos][buy][cnt] != -1) return memo[pos][buy][cnt];
int profit = 0;
if (buy) {
profit = max(-prices[pos] + profitHelper(prices, pos + 1, 0, cnt - 1), profitHelper(prices, pos + 1, 1, cnt));
} else {
profit = max(prices[pos] + profitHelper(prices, pos, 1, cnt), profitHelper(prices, pos + 1, 0, cnt));
}
return memo[pos][buy][cnt] = profit;
}
};