[Problem] [array] [DP] [codility] best time to buy and buy stock

Source: Internet
Author: User

Best time to buy and buy stock

Say you have an array for which the ith element is the price of a given stock on day I.

IfYou were only permitted to complete at most one transaction(Ie, buy one and every one share of the stock), design an algorithm to find the maximum profit.

Ideas

Best time to buy and stock seems to be maxprofit in codility. codility puts this question in the chapter of maximum slice problem, but this question has a more intuitive solution.

Code

Int solution (const vector <int> & A) {if (. size () <2) return 0; int Mina = A [0]; // The minimum int maxp = 0 before I; // The overall maximum benefit for (int I:) {int P = I-mina; // returns the I sold if (P> maxp) maxp = P; if (I <Mina) Mina = I; // update the minimum value} return maxp ;}

Best time to buy and stock Stock II

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 your transactions as you like(Ie, buy one and every one share of the stock multiple times ). however, you may not engage in multiple transactions at the same time (ie, you must wait the stock before you buy again ).

Ideas

II this question is more like a maximum slice problem, there is an important observation that Prices [I]-Prices [I-2] = (Prices [I]-Prices [I-1]) + (Prices [I-1]-Prices [I-2]), so we can abstract the largest field and problem of the adjacent element difference sequence.

Code

 
Int maxprofit (vector <int> & prices) {int n = prices. Size (); If (n <= 1) return 0;
Int profit = 0; For (INT I = 1; I <n; I ++) {int Gap = Prices [I]-Prices [I-1]; If (GAP> 0) profit + = gap;} return profit ;}

Best time to buy and buy stock 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, you must wait the stock before you buy again ).

Ideas

This is a classic question. It is similar to the second idea of maxdoubleslicesum. It refers to the enumeration center point, which is scanned by both sides.

Code

Int maxprofit (vector <int> & prices) {int n = prices. Size (); If (n <= 1) return 0;
// Calculate the maximum transaction value of the subsequence [0, I]: int min = Prices [0]; vector <int> maxprofita (n, 0); For (INT I = 1; I <n; I ++) {int price = Prices [I]; int diff = Price-min; If (maxprofita [I-1] <diff) maxprofita [I] = diff; else maxprofita [I] = maxprofita [I-1]; If (price <min) min = price;} // evaluate [I, n-1] The maximum transaction value of this subsequence int max = Prices [n-1]; vector <int> maxprofitb (n, 0); For (INT I = n-2; I> = 0; I --) {int price = Prices [I]; int diff = max-Prices [I]; If (maxprofitb [I + 1] <diff) maxprofitb [I] = diff; else maxprofitb [I] = maxprofitb [I + 1]; If (price> MAX) max = price ;}
// Traverse the Split points of the two transactions once. Int maxprofit = maxprofita [n-1]; // a transaction is performed in [0, n-1] For (INT I = 0; I <n-1; I ++) {maxprofita [I] + = maxprofitb [I + 1]; // note the meanings of the lower mark if (maxprofit <maxprofita [I]) maxprofit = maxprofita [I];} return maxprofit ;}

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.