-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q9_best_time_to_buy_and_sell_stocks_II.cpp
100 lines (89 loc) · 3.47 KB
/
Q9_best_time_to_buy_and_sell_stocks_II.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
// 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.
// Example 1:
// Input: prices = [7,1,5,3,6,4]
// Output: 7
// Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
// Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
// Total profit is 4 + 3 = 7.
// Example 2:
// Input: prices = [1,2,3,4,5]
// Output: 4
// Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
// Total profit is 4.
// Example 3:
// Input: prices = [7,6,4,3,1]
// Output: 0
// Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
// Approach 1 - Recursion (TLE - Not accepted)
int maxProfitRecursion(vector<int>& prices, int i, int canBuy) {
if(i == prices.size()) {
return 0;
}
if(canBuy) {
int op1 = maxProfitRecursion(prices, i + 1, canBuy);
int op2 = -prices[i] + maxProfitRecursion(prices, i + 1, !canBuy);
return max(op1, op2);
}
else {
int op1 = maxProfitRecursion(prices, i + 1, canBuy);
int op2 = prices[i] + maxProfitRecursion(prices, i + 1, !canBuy);
return max(op1, op2);
}
}
// Approach 2 - Recursion + Memoization (Accepted)
int maxProfitMemoized(vector<int>& prices, int i, int canBuy, vector<vector<int>>& dp) {
if(i == prices.size()) {
return 0;
}
if(dp[i][canBuy] != -1) {
return dp[i][canBuy];
}
if(canBuy) {
int op1 = maxProfitMemoized(prices, i + 1, canBuy, dp);
int op2 = -prices[i] + maxProfitMemoized(prices, i + 1, !canBuy, dp);
return dp[i][canBuy] = max(op1, op2);
}
else {
int op1 = maxProfitMemoized(prices, i + 1, canBuy, dp);
int op2 = prices[i] + maxProfitMemoized(prices, i + 1, !canBuy, dp);
return dp[i][canBuy] = max(op1, op2);
}
}
// Approach 3 - Dynamic Programming (DP)
int maxProfitDP(vector<int>& prices, int n) {
vector<vector<int>> dp(n + 1, vector<int> (2));
for(int i = n - 1; i >= 0; i--) {
dp[i][1] = max(dp[i + 1][1], -prices[i] + dp[i + 1][0]);
dp[i][0] = max(dp[i + 1][0], prices[i] + dp[i + 1][1]);
}
return dp[0][1];
}
// Approach 4 - DP + Space Optimization
int maxProfitDPSpaceOptimized(vector<int>& prices, int n) {
vector<int> curr(2);
vector<int> next(2);
for(int i = n - 1; i >= 0; i--) {
curr[1] = max(next[1], -prices[i] + next[0]);
curr[0] = max(next[0], prices[i] + next[1]);
next = curr;
}
return curr[1];
}
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
// vector<vector<int>> dp(n, vector<int>(2, -1));
// return maxProfitRecursion(prices, 0, 1);
// return maxProfitMemoized(prices, 0, 1, dp);
// return maxProfitDP(prices, n);
return maxProfitDPSpaceOptimized(prices, n);
}
};
// Time Complexity : O(n)
// Space Complexity : O(1)