Problem_Best Time to Buy and Sell Stock I, II, III, IV, Stock with Cooldown - xwu36/LeetCode GitHub Wiki
121. Best Time to Buy and Sell Stock
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.
Algoritm:
release = Math.max(hold + price, release)
hold = Math.max(hold, -price)
Code:
class Solution {
public int maxProfit(int[] prices) {
//hold means the max many we have when buy a product
int n = prices.length;
if(n == 0)
return 0;
int hold = -prices[0];
int release = 0;
for(int i = 1; i < n; i++){
int price = prices[i];
release = Math.max(release, price + hold);
hold = Math.max(-price, hold);
}
return release;
}
}
122. Best Time to Buy and Sell Stock II
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times).
However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times).
However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Algoritm:
release = Math.max(hold + price, release)
hold = Math.max(release - price, hold)
Code:
class Solution {
public int maxProfit(int[] prices) {
// release = Math.max(release, hold + price);
// hold = Math.max(release - price, hold)
int n = prices.length;
if(n == 0)
return 0;
int release = 0;
int hold = -prices[0];
for(int i = 1; i < n; i++){
int price = prices[i];
release = Math.max(release, hold + price);
hold = Math.max(release - price, hold);
}
return release;
}
}
123. Best Time to Buy and Sell Stock III
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Algoritm:
release[i][k] = Math.max(release[i - 1][k], hold + price)
hold = Math.max(hold, price - release[i][k - 1])
Code:
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if(n == 0) return 0;
int k = 2;
int[][] release = new int[k + 1][n];
int hold = 0;
for(int i = 1; i <= k; i++){
hold = release[i-1][0] - prices[0];
for(int j = 1; j < n; j++){
release[i][j] = Math.max(release[i][j - 1], hold + prices[j]);
hold = Math.max(hold, release[i - 1][j] - prices[j]);
}
}
return release[k][n - 1];
}
}
188. Best Time to Buy and Sell Stock IV
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
Algoritm:
release[i][k] = Math.max(release[i - 1][k], hold + price)
hold = Math.max(release[i][k - 1] - price, hold)
Code:
class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if(n == 0) return 0;
//dp[i][j] indicates the maximum profit we can get on i day at most j transactions.
if(k >= prices.length / 2){
int hold = -prices[0];
int release = 0;
for(int i = 1; i < n; i++){
release = Math.max(release, hold + prices[i]);
hold = Math.max(release - prices[i], hold);
}
return release;
}
int[][] release = new int[k + 1][n];
int hold = 0;
for(int i = 1; i <= k; i++){
hold = release[i-1][0] - prices[0];
for(int j = 1; j < n; j++){
release[i][j] = Math.max(release[i][j - 1], hold + prices[j]);
hold = Math.max(hold, release[i - 1][j] - prices[j]);
}
}
return release[k][n - 1];
}
}
309. Best Time to Buy and Sell Stock with Cooldown
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
Algoritm:
release[i] = Math.max(release[i - 1], hold + price)
hold = Math.max(hold, release[i - 2] - price)
Code:
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if(n <= 1) return 0;
int[] release = new int[n];
release[1] = Math.max(0, prices[1] - prices[0]);
int hold = Math.max(-prices[0], -prices[1]);
for(int i = 2; i < n; i++){
release[i] = Math.max(release[i - 1], hold + prices[i]);
hold = Math.max(hold, release[i - 2] - prices[i]);
}
return release[n - 1];
}
}