Leetcode 121. best time to Buy and Sell Stock

Source: Internet
Author: User

121. Best time to Buy and Sell Stock
    • Total accepted:115636
    • Total submissions:313848
    • Difficulty:easy

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.

Example 1:

Input: [71536456- 1 5 7-16 as selling price needs to being larger than buying price)

Example 2:

Input: [764310this case  is0.

Idea: DP.

Method One: For the first day, find [0,i-1] Day buy the lowest price, thus get the first day of sale of the highest price. Then traverse I to get the highest selling price.

Method Two: Maximal sub-sequences and problems.

Code:

Method One:

1 classSolution {2  Public:3     intMaxprofit (vector<int>&prices) {4         intN=prices.size (), i,maxprofit=0, minprice=Int_max;5          for(i=0; i<n;i++){6             if(maxprofit<prices[i]-minprice) {7maxprofit=prices[i]-Minprice;8             }9             if(minprice>Prices[i]) {TenMinprice=Prices[i]; One             } A         } -         returnMaxprofit; -     } the};

Method Two:

Reference:

https://discuss.leetcode.com/topic/19853/ Kadane-s-algorithm-since-no-one-has-mentioned-about-this-so-far-in-case-if-interviewer-twists-the-input

Https://en.wikipedia.org/wiki/Maximum_subarray_problem

Original:

The logic to solve ThisProblem isSame as "Max Subarray Problem" usingKadane's algorithm. Since no body have mentioned this so far, I thought it's a good thing foreverybody to know. All the straight forward solution shouldifThe interviewer twists the question slightly by giving the difference array of prices, Ex: for{1,7,4, One},ifHe gives {0,6, -3,7}, you might end up being confused. Here, the logic isTo calculate the difference (maxcur + = prices[i]-prices[i-1]) of the original array, and find a contiguous subarray giving maximum profit. If the difference falls below0, reset it to zero.  Public intMaxprofit (int[] prices) {        intMaxcur =0, Maxsofar =0;  for(inti =1; i < prices.length; i++) {Maxcur= Math.max (0, maxcur + = prices[i]-prices[i-1]); Maxsofar=Math.max (Maxcur, Maxsofar); }        returnMaxsofar; }*maxcur =Current Maximum value*maxsofar = maximum value found so far

1 classSolution {2  Public:3     intMaxprofit (vector<int>&prices) {4         intI,n=prices.size (), curprofit=0, maxprofit=0;5          for(i=1; i<n;i++){6Curprofit=max (0, curprofit+prices[i]-prices[i-1]);7maxprofit=Max (maxprofit,curprofit);8         }9         returnMaxprofit;Ten     } One};

Leetcode 121. best time to Buy and Sell Stock

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.