309.Best time to Buy and Sell Stock with Cooldown

Source: Internet
Author: User

Topic:

Say you has an array for which the i-th element is the price of a given-stock on day I.

Design an algorithm to find the maximum profit. Many transactions as (ie, buy one and sell one share of the stock multiple times) with the FO Llowing Restrictions:

    • Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).
    • After you sell your the stock, you cannot buy the stock on next day. (ie, cooldown 1 day)

Example:

Prices = [1, 2, 3, 0, 2]maxprofit = 3transactions = [Buy, Sell, cooldown, buy, sell]

Links: http://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

Exercises

The stock question comes again, this should be the last question of the current stock series. After selling there is cooldown, then multi transaction the largest profit. The first impression is DP, but every time the topic of DP, transfer equation how also write bad, must be well strengthened. Out of this problem dietpepsi in discuss also wrote his ideas and solutions, we all go to see. But I didn't read it myself .... DP skill is too poor, but it is behind a buddy's state machine solution compared to make sense. The above solution is based on a day with only one operation, or buy or sell or hold. There are also some understandings that can be bought and sold on the day, listed in the discuss. It seems that the topic really must be specified very carefully, otherwise it is difficult to read.

Time Complexity-o (n), Space complexity-o (n)

 Public classSolution { Public intMaxprofit (int[] prices) {        if(Prices = =NULL|| Prices.length = = 0) {            return0; }        intLen =prices.length; int[] buy =New int[Len + 1];//before I, for any sequence last action at I was going to be buy        int[] sell =New int[Len + 1];//before I, for any sequence last action at I am going to be sell        int[] cooldown =New int[Len + 1];//before I, for any sequence last action at I am going to be cooldownBuy[0] =Integer.min_value;  for(inti = 1; I < Len + 1; i++) {Buy[i]= Math.max (Buy[i-1], cooldown[i-1]-prices[i-1]);//must sell to get profitSell[i] = Math.max (Buy[i-1] + prices[i-1], sell[i-1]); Cooldown[i]= Math.max (Sell[i-1], Math.max (buy[i-1], cooldown[i-1])); }                returnMath.max (Buy[len], Math.max (Sell[len], Cooldown[len])); }}

Using the

 Public classSolution { Public intMaxprofit (int[] prices) {        if(Prices = =NULL|| Prices.length < 2) {            return0; }        intLen =prices.length; int[] S0 =New int[Len];//to buy        int[] S1 =New int[Len];//To sell        int[] s2 =New int[Len];//To restS0[0] = 0; s1[0] =-prices[0]; s2[0] = 0;  for(inti = 1; i < Len; i++) {S0[i]= Math.max (S0[i-1], s2[i-1]); S1[i]= Math.max (S1[i-1], s0[i-1]-Prices[i]); S2[i]= S1[i-1] +Prices[i]; }                returnMath.max (S0[len-1], s2[len-1]);//Hold and Res    }}

There are opportunities to simplify space complexity, to see the yavinci of the resolution.

Off Topic:

Today just found Leetcode submitted the answer page with the title number, convenient a lot, before just the total directory has the title number. This question is very similar to mine. Now the company's policy is to buy stocks must hold for 30 days, and can not buy real estate stocks ... I think I have no contact with any data, can not do short-term, really very loss.

Reference:

Https://leetcode.com/discuss/72030/share-my-dp-solution-by-state-machine-thinking

http://fujiaozhu.me/?p=725

http://bookshadow.com/weblog/2015/11/24/leetcode-best-time-to-buy-and-sell-stock-with-cooldown/

Https://leetcode.com/discuss/71391/easiest-java-solution-with-explanations

Http://www.cnblogs.com/grandyang/p/4997417.html

Https://leetcode.com/discuss/71246/line-constant-space-complexity-solution-added-explanation

Https://leetcode.com/discuss/73617/7-line-java-only-consider-sell-and-cooldown

Https://leetcode.com/discuss/71354/share-my-thinking-process

309.Best time to Buy and Sell Stock with Cooldown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.