309. Best Time to Buy and Sell Stock with Cooldown

    /*
     * 309. Best Time to Buy and Sell Stock with Cooldown
     * 2016-7-4 by Mingyang
     * http://buttercola.blogspot.com/2016/01/leetcode-best-time-to-buy-and-sell.html
     *
     *1. Define States
     *
     *To represent the decision at index i:
     *buy[i]: Max profit till index i. The series of transaction is ending with a buy.
     *sell[i]: Max profit till index i. The series of transaction is ending with a sell.
     *To clarify:
     *Till index i, the buy / sell action must happen and must be the last action.
     * It may not happen at index i. It may happen at i - 1, i - 2, ... 0.
     *In the end n - 1, return sell[n - 1]. Apparently we cannot finally end up with a buy.
     *In that case, we would rather take a rest at n - 1.
     *For special case no transaction at all, classify it as sell[i], so that in the end, we can still return sell[n - 1].
     *
     *2. Define Recursion
     *
     *buy[i]: To make a decision whether to buy at i, we either take a rest, by just using the old decision at i - 1,
     *or sell at/before i - 2, then buy at i, We cannot sell at i - 1, then buy at i, because of cooldown.
     *sell[i]: To make a decision whether to sell at i, we either take a rest, by just using the old decision at i - 1,
     *or buy at/before i - 1, then sell at i.
     *So we get the following formula:
     *buy[i] = Math.max(buy[i - 1], sell[i - 2] - prices[i]);
     *sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);
     */
     public int maxProfit5(int[] prices) {
            if (prices == null || prices.length <= 1) {
                return 0;
            }
            int[] sell=new int[prices.length];
            int[] buy=new int[prices.length];
            buy[0] = -prices[0];
            sell[0] = 0;
            buy[1]=Math.max(buy[0],-prices[1]);
            sell[1]=Math.max(sell[0],buy[0]+prices[1]);
            for (int i = 2; i <prices.length; i++) {
                 buy[i]=Math.max(buy[i-1],sell[i-2]-prices[i]);
                 sell[i]=Math.max(sell[i-1],buy[i-1]+prices[i]);
            }
            return sell[prices.length-1];
        }
时间: 2024-10-20 10:00:34

309. Best Time to Buy and Sell Stock with Cooldown的相关文章

LeetCode Best Time to Buy and Sell Stock with Cooldown

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: 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

Best Time to Buy and Sell Stock with Cooldown

题目: 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)

Best Time to Buy and Sell Stock with Cooldown -- LeetCode

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) wit

解题报告Best Time to Buy and Sell Stock with Cooldown

题目 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)

Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)

股票问题: 309. 最佳买卖股票时机含冷冻期 714. 买卖股票的最佳时机含手续费 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 .? 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天). 示例: 输入: [1,2,3,0,2] 输出: 3 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入,

[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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,

Best Time to Buy and Sell Stock系列

1.Best Time to Buy and Sell Stock II 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 if(prices.size() < 1) //prices.size is unsigned int 5 return 0; 6 int pro = 0; 7 for(int i=0; i<prices.size()-1; i++) 8 { 9 if(price

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution 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

Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock I 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 algorith