Best Time to Buy and Sell Stock III – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
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. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
Constraints:
1 <= prices.length <= 105
0 <= prices[i] <= 105
C++ Best Time to Buy and Sell Stock III LeetCode Solution
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
vector<int> noS1(n,INT_MIN);
vector<int> noS2(n,INT_MIN);
vector<int> inH0(n);
vector<int> inH1(n,INT_MIN);
inH0[0] = -prices[0];
for(int i=1;i<n;i++){
inH0[i] = max(-prices[i],inH0[i-1]);
noS1[i] = max(noS1[i-1],prices[i]+inH0[i-1]);
if(i>=2) inH1[i] = max(noS1[i-1]-prices[i],inH1[i-1]);
if(i>=3) noS2[i] = max(noS2[i-1],inH1[i-1]+prices[i]);
}
return max(0,max(noS1[n-1],noS2[n-1]));
}
};
Java Best Time to Buy and Sell Stock III LeetCode Solution
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1) {
return 0;
}
/*
1. days: 0: market has not started yet -> total length: prices.length + 1;
2. transactions: 0: I just don't want to get involved in the market, 1: Do one transaction, 2: Do two transactions
3. 0: not holding, 1: holding;
*/
int[][][] m = new int[prices.length + 1][3][2];
for (int i = 0; i <= 2; i ++) {
m[0][i][0] = 0;
// we are not holding anything in 0th day
m[0][i][1] = Integer.MIN_VALUE;
// we are not allowed to hold any stocks in 0th day
}
int result = 0;
for (int i = 1; i < prices.length + 1; i ++) {
// traversing through days
for (int j = 1; j <= 2; j ++) {
// try all the transactions
m[i][j][0] = Math.max(m[i - 1][j][0], m[i - 1][j][1] + prices[i - 1]);
// Here: Math.max(yesterday not holding, yesterday holding, so try to sell it)
m[i][j][1] = Math.max(m[i - 1][j][1], m[i - 1][j - 1][0] - prices[i - 1]);
// Here: Math.max(yesterday holding, yesterday not holding, try to buy one)
// Note that here I mark buying as the begin of the transaction
result = Math.max(result, m[i][j][0]);
// Only update result, when we are not holding any stocks
}
}
return result;
}
}
Python 3 Best Time to Buy and Sell Stock III LeetCode Solution
class Solution:
def maxProfit(self, prices: List[int]) -> int:
h1 = nh1 = h2 = nh2 = -float('inf')
for price in prices:
h1, nh1, h2, nh2 = max(-price, h1), max(nh1, h1 + price), max(h2, nh1 - price), max(nh2, h2 + price)
return max(0, nh1, nh2)
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1
Leave a comment below