-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39_BuySellStocksII.cpp
140 lines (124 loc) · 3.39 KB
/
39_BuySellStocksII.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
// You are given an integer array prices where prices[i] is the price of a
// given stock on the ith day.
// On each day, you may decide to buy and/or sell the stock. You can only
// hold at most one share of the stock at any time. However, you can buy it
// then immediately sell it on the same day.
// Find and return the maximum profit you can achieve.
#include <bits/stdc++.h>
using namespace std;
class Solution4
{
// Tabulation: Space Optimised
public:
int maxProfit(vector<int> &prices)
{
int n = prices.size();
vector<int> prev(2), curr(2);
for (int i = n-1; i >= 0; --i)
{
for (int j = 0; j < 2; ++j)
{
int noAction = prev[j];
int action = prev[!j];
j ? action += prices[i] : action -= prices[i];
curr[j] = max(action, noAction) ;
}
prev = curr;
}
return curr[0];
}
};
class Solution3
{
// Tabulation
public:
int maxProfit(vector<int> &prices)
{
int n = prices.size();
vector<vector<int>> dp(n+1, vector<int> (2));
for (int i = n-1; i >= 0; --i)
{
for (int j = 0; j < 2; ++j)
{
int noAction = dp[i+1][j];
int action = dp[i+1][!j];
j ? action += prices[i] : action -= prices[i];
dp[i][j] = max(action, noAction) ;
}
}
return dp[0][0];
}
};
class Solution2
{
// Recursion: Memoization
public:
int maxProfit(vector<int> &prices)
{
vector<vector<int>> dp(prices.size(), vector<int> (2, -1));
return solve(prices, dp);
}
int solve (vector<int> &prices, vector<vector<int>> &dp, int stock = 0, int i = 0) {
// base case
if (i == prices.size()) return 0 ;
if (dp[i][stock] != -1) return dp[i][stock];
int noAction = solve (prices, dp, stock, i+1);
int action = solve(prices, dp, !stock, i+1);
stock ? action += prices[i] : action -= prices[i];
return dp[i][stock] = max(action, noAction);
}
};
class Solution1
{
// BruteForce: Recursion
public:
int maxProfit(vector<int> &prices)
{
return solve(prices);
}
int solve (vector<int> &prices, bool stock = false, int i = 0) {
// base case
if (i == prices.size()) return 0 ;
int noAction = solve (prices, stock, i+1);
int action = solve(prices, !stock, i+1);
stock ? action += prices[i] : action -= prices[i];
return max(action, noAction);
}
};
class Solution
{
// Intution
public:
int maxProfit(vector<int> &prices)
{
int profit = 0, stock = 0;
for (int i = 0; i < prices.size() - 1; ++i)
{
if (!stock && prices[i + 1] > prices[i])
{
profit -= prices[i];
stock = 1;
}
else if (stock) {
if (prices[i+1] > prices[i]){
continue;
}
else {
profit += prices[i];
stock = 0 ;
}
}
}
if (stock) {
profit += prices.back();
}
return profit ;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}