Leetcode121/122/123 best time to Buy and Sell stock< stock > I/II/III----dp+greedy**

Source: Internet
Author: User

One: Leetcode 121 best time to Buy and Sell Stock

Topic:

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

If you were-permitted-to-complete at most one transaction (ie, buy one and sell one share of the stock), design an AL Gorithm to find the maximum profit.

Links: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

Analysis: The problem is to choose to buy and sell the maximum profit, for the first day of the sale is the first day of the stock market price minus [0,i-1] days of the minimum stock price, when the first day of the stock market price is lower than the lowest price of the paint, then the lowest stock price update. Then take the biggest equity gains for DP issues.   With Profit[i] represents the earnings of day I, then Minbuyprice = min (Minbuyprice, prices[i]), and profit[i] = Prices[i]-minbuyprice. Then take the maximum value in the profit.

Class Solution {public:    int maxprofit (vector<int> &prices) {        int n = prices.size ();        if (n = = 0) return 0;        int maxPro = 0;        int minbuyprice = prices[0];        for (int i = 1; i < n; i++) {            minbuyprice = min (Minbuyprice, prices[i]);   Minimum value for recording current day buy minbuyprice[i+1] = min (Minbuyprice[i], nums[i])            if (MaxPro < (prices[i]-minbuyprice)) {     // Whether the global maximum benefit is less than the interest of the day                 MaxPro = prices[i]-minbuyprice;            }        }        return maxPro;}            ;

II: Leetcode 122 best time to Buy and Sell Stock II

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. You could complete as many transactions as (ie, buy one and sell one share of the stock multiple times). However, engage in multiple transactions for the same time (ie, you must sell the stock before you buy again).

Links: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

Analysis: This problem is the deformation of the problem, buy and sell the number of times there is no limit, but the second buy must be in the first time after the sale of the node, there is a local optimal, that is, 2 4 5 3 6 8, at this time 8-2 must be less than 5-2+8-3, so there is an array of each increment of the income is the local Then all local optimality is combined with the global optimal

Class Solution {Public:int Maxprofit (vector<int> &prices) {int n = prices.size ();        if (n = = 0) return 0;        int minbuyprice = prices[0];        int sumresult = 0;  /*for (int i = 0; i < n; i++) {if (I+1 < n && prices[i] >= prices[i+1]) {//local optimum is when Prices[i]    >= prices[i+1] Sumresult + = Prices[i]-minbuyprice;            Prices[i]-minbuyprices can be used to find the local optimal--all add up is the global optimal solution Minbuyprice = prices[i+1];            }else{if (i+1 = = N) sumresult + = Prices[i]-minbuyprice; }}*/for (int i = 1; i < n; i++) {if (Prices[i] < prices[i-1]) {//have a local optimality at I, all offices                The optimal Sumresult + = prices[i-1]-Minbuyprice is the global optimal.            Minbuyprice = Prices[i];        }} Sumresult + = prices[n-1]-minbuyprice;    return sumresult; }};
Later in the discuss saw a more awesome code: just 10 lines:

Class Solution {public:    int maxprofit (vector<int> &a) {        int profit = 0;        for (int i = 1; i < a.size (); ++i) {            if (A[i] > A[i-1]) {                profit + = A[i]-a[i-1];}        }        return profit;}    ;
Three: Leetcode 123 best time to Buy and Sell Stock III

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. You are in most of the transactions.

Note:
Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).

Links: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

Analysis: The problem is deformation, that is, to find the maximum number of transactions is two times. Of course there are two methods, the first kind of violence, for each I, we seek [0,i] and [i,n-1] two proceeds, and then sum, traverse I, can take the maximum value, need O (n^2) time. The second method is the dynamic programming, using two arrays, the first array f1[i] to represent the maximum proceeds of buy and sell in [0,i], with F2[i] representing the maximum proceeds of buy and sell within [i,n-1]. Then the maximum benefit is Max (F1[i]+f2[i]), how to find F1[i] and F2[i], the first question method has been given.

Class Solution {public:    int maxprofit (vector<int> &prices) {        int n = prices.size ();        if (n <= 1) return 0;        Vector<int> F1 (n);  Represents the maximum profit        vector<int> f2 (n) that can be obtained by buying and selling within [0,i];  Indicates that the maximum profit result for a buy and sell in [I, N-1] is  max (f1[i]+f2[i])                int minprice = prices[0];                for (int i = 1; i < n; i++) {            minprice = min (Minprice, prices[i]);            F1[i] = max (f1[i-1], prices[i]-minprice);           }                int maxprice = prices[n-1];        for (int i = n-2; I >=0; i--) {     //here to traverse from backward            Maxprice = max (Maxprice, prices[i]);            F2[i] = max (f2[i+1], maxprice-prices[i]);        }        int maxresult = 0;        for (int i = 0; i < n; i++)            Maxresult = max (Maxresult, F1[i]+f2[i]);        return maxresult;    }        ;





Leetcode121/122/123 best time to Buy and Sell stock< stock > I/II/III----dp+greedy**

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.