-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path43_BuySellStockVI.cpp
113 lines (103 loc) · 3.2 KB
/
43_BuySellStockVI.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
// You are given an array prices where prices[i] is the price of a given stock on
// the ith day, and an integer fee representing a transaction fee.
// Find the maximum profit you can achieve. You may complete as many
// transactions as you like, but you need to pay the transaction fee
// for each transaction.
// Note: You may not engage in multiple transactions simultaneously (i.e.,
// you must sell the stock before you buy again).
#include <bits/stdc++.h>
using namespace std;
class Solution4 {
// Discussion solution
public:
int maxProfit(vector<int>& prices, int fee) {
int s0 = 0, s1 = INT_MIN;
for(int p:prices) {
int tmp = s0;
s0 = max(s0, s1+p);
s1 = max(s1, tmp-p-fee);
}
return s0;
}
};
class Solution3
{
// Tabulation: Space Optimised
public:
int maxProfit(vector<int> &prices, int fee)
{
int n = prices.size();
vector<int> temp(2, 0), curr(2, 0);
for (int i = n-1; i >= 0; --i)
{
for (int stock = 0; stock < 2; ++stock)
{
int action = curr[!stock], noAction = curr[stock];
stock ? action += (prices[i] - fee): action -= prices[i];
temp[stock] = max(action, noAction);
}
curr = temp;
}
return curr[0];
}
};
class Solution2
{
// Tabulation
public:
int maxProfit(vector<int> &prices, int fee)
{
int n = prices.size();
vector<vector<int>> dp(n+1, vector<int> (2, 0));
for (int i = n-1; i >= 0; --i)
{
for (int stock = 0; stock < 2; ++stock)
{
int action = dp[i+1][!stock], noAction = dp[i+1][stock];
stock ? action += (prices[i] - fee): action -= prices[i];
dp[i][stock] = max(action, noAction);
}
}
return dp[0][0];
}
};
class Solution1
{
// Recursion: Memoization
public:
int maxProfit(vector<int> &prices, int fee)
{
int n = prices.size();
vector<vector<int>> dp(n, vector<int> (2, -1));
return trader(prices, dp, fee);
}
int trader(vector<int> &prices, vector<vector<int>> &dp, int fee, int i = 0, int stock = 0) {
if (i == prices.size()) return 0;
if (dp[i][stock] != -1) return dp[i][stock];
int action = trader(prices, dp, fee, i+1, !stock), noAction = trader(prices, dp, fee, i+1, stock);
stock ? action += (prices[i] - fee): action -= prices[i];
return dp[i][stock] = max(action, noAction);
}
};
class Solution
{
// BruteForce: Recursion
public:
int maxProfit(vector<int> &prices, int fee)
{
return trader(prices, fee);
}
int trader(vector<int> &prices, int fee, int i = 0, int stock = 0) {
if (i == prices.size()) return 0;
int action = trader(prices, fee, i+1, !stock), noAction = trader(prices, fee, i+1, stock);
stock ? action += (prices[i] - fee): action -= prices[i];
return max(action, noAction);
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}