Leetcode "Dynamic planning": best time to Buy and Sell Stock I && II && III && IV

Source: Internet
Author: User

1. Best time to Buy and Sell Stock I

Topic links

Title Requirements:

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.

The essence of this problem is to find out the maximum difference between any two numbers in an array (the number of large numbers minus the small numbers). We find that the maximum difference information for a segment of an array can be saved and as the initial value for the next paragraph. The specific recursive style is as follows:

1], Prices[i]-minprice);

The procedure is also relatively simple:

1 classSolution {2  Public:3     intMaxprofit (vector<int>&prices) {4         intSZ =prices.size ();5         if(SZ = =0)6             return 0;7             8vector<int> dp (SZ,0);9         intMinprice = prices[0];Ten          for(inti =1; I < sz; i++) One         { ADp[i] = max (Dp[i-1], Prices[i]-minprice); -             if(Minprice >Prices[i]) -Minprice =Prices[i]; the         } -          -         returnDp[sz-1]; -     } +};
View Code

Another very representative solution is as follows (refer to from one post):

A new array of price spreads, i.e. prices[1]-prices[0], prices[2]-prices[1], prices[3]-prices[2], ..., prices[n-1]-prices[n-2], So our problems are transformed into the largest contiguous sub-segments and. How to efficiently solve maximal contiguous sub-segments and refer to a blog post that you have written before.

2. Best time to Buy and Sell Stock II

Leetcode did not provide the title, originating from other posts.

Title Requirements:

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

The solution of this problem is simpler, we need to build a stock price margin group, and then add all the numbers greater than 0 in the array to get the result. Specific procedures are quoted in the same blog post:

1 classSolution {2  Public:3     intMaxprofit (vector<int> &prices) {4         //Important:please Reset any member data declared, as5         //The same solution instance would be a reused for each test case.6         intLen =prices.size ();7         if(Len <=1)return 0;8         intres =0;9          for(inti =0; I < len-1; i++)Ten             if(prices[i+1]-prices[i] >0) OneRes + = prices[i+1] -Prices[i]; A         returnRes; -     } -};
View Code 3. Best time to Buy and Sell Stock III

Topic links

Title Requirements:

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

Since only a maximum of two transactions can be completed, and the second transaction can only be done after the first transaction is completed, we can first approach the first method, in order to calculate the maximum and the number of arrays, and then the largest and the inverse of the array, and then the two arrays to obtain the maximum benefit. The specific procedures are as follows:

1 classSolution {2  Public:    3     intSimplemaxprofit (vector<int>&prices) {4         intSZ =prices.size ();5         if(SZ = =0)6             return 0;7         8vector<int> dp (SZ,0);9         intMinprice = prices[0];Ten          for(inti =1; I < sz; i++) One         { ADp[i] = max (Dp[i-1], Prices[i]-minprice); -             if(Minprice >Prices[i]) -Minprice =Prices[i]; the         } -          -vector<int> Dp_rev (SZ,0); -         intMaxprice = Prices[sz-1]; +          for(inti = sz-2; I >-1; i--) -         { +Dp_rev[i] = min (dp_rev[i +1], Prices[i]-maxprice); A             if(Maxprice <Prices[i]) atMaxprice =Prices[i]; -         } -          -vector<int> Maxgain (SZ,0); -maxgain[0] =0; -          for(inti =1; I < sz; i++) in         { -Maxgain[i] = max (Maxgain[i-1], Dp[i]-dp_rev[i]); to         } +          -         returnMaxgain[sz-1]; the     } *  $     intMaxprofit (vector<int>&prices) {Panax Notoginseng         returnSimplemaxprofit (prices); -     } the};
View Code

Leetcode "Dynamic planning": best time to Buy and Sell Stock I && II && III && IV

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.