【Leetcode 动态规划】 买卖股票 I II III IV 冷却，共5题

48 篇文章 0 订阅
18 篇文章 0 订阅

Best Time to Buy and Sell Stock I

Description: 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.

public class Solution {
public int maxProfit(int[] prices) {
if (prices.length < 2) return 0;

int maxProfit = 0;
int curMin = prices[0];

for (int i = 1; i < prices.length; i++) {
curMin = Math.min(curMin, prices[i]);
maxProfit = Math.max(maxProfit, prices[i] - curMin);
}

return maxProfit;
}
}

Best Time to Buy and Sell Stock II

Description: 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).

public class Solution {
public int maxProfit(int[] prices) {
if (prices.length < 2) return 0;

int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
int diff = prices[i] - prices[i - 1];
if (diff > 0) {
maxProfit += diff;
}
}

return maxProfit;
}
}

Best Time to Buy and Sell Stock III

Description: 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).

public class Solution {
public int maxProfit(int[] prices) {
if (prices.length < 2) return 0;

int n = prices.length;
int[] preProfit = new int[n];
int[] postProfit = new int[n];

int curMin = prices[0];
for (int i = 1; i < n; i++) {
curMin = Math.min(curMin, prices[i]);
preProfit[i] = Math.max(preProfit[i - 1], prices[i] - curMin);//第i天卖出
}

int curMax = prices[n - 1];
for (int i = n - 2; i >= 0; i--) {//从后往前遍历
curMax = Math.max(curMax, prices[i]);
postProfit[i] = Math.max(postProfit[i + 1], curMax - prices[i]);//第i天买入
}

int maxProfit = 0;
for (int i = 0; i < n; i++) {
maxProfit = Math.max(maxProfit, preProfit[i] + postProfit[i]);
}

return  maxProfit;
}
}

Best Time to Buy and Sell Stock IV

Description: 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).

profit[i][j] = max(profit[i – 1][j], profit[i – 1][j – 1] + diff)

local[i][j] = max(global[i - 1][j - 1] + max(diff, 0), local[i - 1][j] + diff)
global[i][j] = max(local[i][j], global[i - 1][j])，

local[i][j]和global[i][j]的区别是：local[i][j]意味着在第i天一定有交易（卖出）发生，当第i天的价格高于第i-1天（即diff > 0）时，那么可以把这次交易（第i-1天买入第i天卖出）跟第i-1天的交易（卖出）合并为一次交易，即local[i][j]=local[i-1][j]+diff；当第i天的价格不高于第i-1天（即diff<=0）时，那么local[i][j]=global[i-1][j-1]+diff，而由于diff<=0，所以可写成local[i][j]=global[i-1][j-1]。global[i][j]就是我们所求的前i天最多进行k次交易的最大收益，可分为两种情况：如果第i天没有交易（卖出），那么global[i][j]=global[i-1][j]；如果第i天有交易（卖出），那么global[i][j]=local[i][j]。

public class Solution {
public int maxProfit(int k, int[] prices) {
if (prices.length < 2) return 0;

int days = prices.length;
if (k >= days) return maxProfit2(prices);

int[][] local = new int[days][k + 1];
int[][] global = new int[days][k + 1];

for (int i = 1; i < days ; i++) {
int diff = prices[i] - prices[i - 1];

for (int j = 1; j <= k; j++) {
local[i][j] = Math.max(global[i - 1][j - 1], local[i - 1][j] + diff);
global[i][j] = Math.max(global[i - 1][j], local[i][j]);
}
}

return global[days - 1][k];
}

public int maxProfit2(int[] prices) {
int maxProfit = 0;

for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
maxProfit += prices[i] - prices[i - 1];
}
}

return maxProfit;
}
}

public class Solution {
public int maxProfit(int k, int[] prices) {
if (prices.length < 2) return 0;
if (k >= prices.length) return maxProfit2(prices);

int[] local = new int[k + 1];
int[] global = new int[k + 1];

for (int i = 1; i < prices.length ; i++) {
int diff = prices[i] - prices[i - 1];

for (int j = k; j > 0; j--) {
local[j] = Math.max(global[j - 1], local[j] + diff);
global[j] = Math.max(global[j], local[j]);
}
}
return global[k];
}

public int maxProfit2(int[] prices) {
int maxProfit = 0;

for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
maxProfit += prices[i] - prices[i - 1];
}
}
return maxProfit;
}
}

309. Best Time to Buy and Sell Stock with Cooldown

sell[i]表示在第i天之前最后一个操作是卖，此时的最大收益。
rest[i]表示在第i天之前最后一个操作是冷冻期，此时的最大收益。

buy[i]  = max(rest[i-1] - price, buy[i-1])
sell[i] = max(buy[i-1] + price, sell[i-1])
rest[i] = max(sell[i-1], buy[i-1], rest[i-1])

buy[i]  = max(sell[i-2] - price, buy[i-1])
sell[i] = max(buy[i-1] + price, sell[i-1])

pre_buy = buy;【因为buy是上一轮的，此句含义：pre_buy = buy[i-1]】
pre_sell = sell;【因为sell是上一轮的，此句含义：sell[i-1] = sell】
sell = max(pre_sell, pre_buy + prices[i]);【此句含义：sell[i]即sell   =   max（sell[i-1]即pre_sell，buy[i-1]【已更新为pre_buy了】 + prices[i]）】

class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n == 0) return 0;

int buy = 0x80000000, sell = 0, pre_buy = 0, pre_sell = 0;
for(int i = 0; i < n; ++i) {//把buy初值设为-prices[0]，for循环从i = 1开始，也对！
pre_sell = sell;
sell = max(pre_sell, pre_buy + prices[i]);
}
return sell;
}
};

• 12
点赞
• 6
收藏
• 0
评论
11-27 5
08-17 946
09-27 243
08-07 4832
05-30 360
11-07 271
11-10 1484
09-02 364
09-07 113

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助