Best Time to Buy and Sell Stock
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.
难度:easy
Solution: Maximum Subarray
这个问题有个通用的解法:Kadane's Algorithm
public class Solution {
public int maxProfit(int[] prices) {
int maxRevenue = 0;
if(prices.length == 0) {
return maxRevenue;
}
int buyPrice = prices[0];
for(int i = 1; i < prices.length; ++i) {
maxRevenue = Math.max(maxRevenue, prices[i] - buyPrice);
buyPrice = Math.min(buyPrice, prices[i]);
}
return maxRevenue;
}
}
Best Time to Buy and Sell Stock II
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).
难度:easy
public class Solution {
public int maxProfit(int[] prices) {
int maxRevenue = 0;
for(int i = 1; i < prices.length; ++i) {
maxRevenue = Math.max(maxRevenue, maxRevenue + prices[i] - prices[i - 1]);
}
return maxRevenue;
}
}
Best Time to Buy and Sell Stock III
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).
难度:hard
Solution: Dynamic Programming
public class Solution {
public int maxProfit(int[] prices) {
int k = 2, maxRevenue = 0;
if(prices.length == 0) {
return maxRevenue;
}
int[][] dp = new int[k + 1][prices.length];
for(int i = 1; i <= k; ++i) {
int preMax = dp[i - 1][0] - prices[0];
for(int j = 1; j < prices.length; ++j) {
dp[i][j] = Math.max(dp[i][j - 1], preMax + prices[j]);
preMax = Math.max(preMax, dp[i - 1][j] - prices[j]);
maxRevenue = Math.max(maxRevenue, dp[i][j]);
}
}
return maxRevenue;
}
}
Best Time to Buy and Sell Stock IV
Hard
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).
Solution: Dynamic Programming
class Solution {
public int maxProfit(int k, int[] prices) {
int m = prices.length;
int maxRevenue = 0;
if(m == 0) {
return maxRevenue;
}
if(k >= m / 2) {
for(int i = 1; i < m; ++i) {
if(prices[i] > prices[i - 1]) {
maxRevenue += (prices[i] - prices[i - 1]);
}
}
} else {
int[][] dp = new int[k + 1][prices.length];
for(int i = 1; i <= k; ++i) {
int preMax = dp[i - 1][0] - prices[0];
for(int j = 1; j < prices.length; ++j) {
dp[i][j] = Math.max(dp[i][j - 1], preMax + prices[j]);
preMax = Math.max(preMax, dp[i - 1][j] - prices[j]);
maxRevenue = Math.max(maxRevenue, dp[i][j]);
}
}
}
return maxRevenue;
}
}
Best Time to Buy and Sell Stock with Cooldown
Medium
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)
Solution: Dynamic Programming + State Machine
State Machine is a good way of solving DP problem with complex choices.
class Solution {
public int maxProfit(int[] prices) {
if(prices.length <= 1) {
return 0;
}
int m = prices.length;
int afterSell = prices[1] - prices[0], beforeBuy = 0, afterBuy = Math.max(-prices[1], -prices[0]);
for(int i = 2; i < m; ++i) {
int tempAS = afterSell;
int tempBB = beforeBuy;
int tempAB = afterBuy;
afterSell = prices[i] + tempAB;
beforeBuy = Math.max(tempAS, tempBB);
afterBuy = Math.max(tempAB, tempBB - prices[i]);
}
return Math.max(beforeBuy, afterSell);
}
}