Leetcode 121. best time to Buy and Sell stock when stock sale (dynamic planning, array, simulation)

Source: Internet
Author: User

Title Description

An array is known, and the I element represents the price of the stock of day I, you can only trade once (buy and sell each time), design algorithms to find the maximum benefit

Test examples
Input: [7, 1, 5, 3, 6, 4]Output: 5最大收益 = 6-1 = 5 (不是7-1 = 6,因为先买后卖,7买,1买亏了6)Input: [7, 6, 4, 3, 1]Output: 0最大收益为0
Detailed analysis

At first glance, it's very simple to iterate through an array, select an element at a time, find the maximum value of the array following the element, calculate the difference, and compare the current maximum benefit, like this:
[7, 1,5,3,6,4] current 7, rear Max 6, yield 1
[7,1, 5,3,6,4] current 1, rear max 6, yield 5
[7,1,5, 3,6,4] current 5, rear Max 6, yield 1
So continue to find.
However, this method will time out, a little change, now not only to maintain the maximum benefits, but also to save the lowest price, if the price is lower today, the minimum price (Buy), and if the price of the day minus the minimum price of the largest return, the maximum price (sell), the process is as follows:

[7, 1,5,3,6,4] minPrice=INT_MAX,maxProfit=0 =minPrice=7,maxProfit=0

[7,1, 5,3,6,4] minPrice=7,maxProfit=0 =minPrice=1,maxProfit=0

[7,1,5, 3,6,4] minPrice=1,maxProfit=0 =minPrice=1,maxProfit=4

[7,1,5,3, 6,4] minPrice=1,maxProfit=4 =minPrice=1,maxProfit=4

[7,1,5,3,6, 4] minPrice=1,maxProfit=4 =minPrice=1,maxProfit=5

[7,1,5,3,6,4] minPrice=1,maxProfit=5 =minPrice=1,maxProfit=5

Algorithm implementation
  • Method 1:time Limit exceeded

     Solution {public : int  maxprofit (vector<    int  >& prices) {int  Profit = 0 ; for  (auto  start=prices.begin (); Start!=prices.end (); start++)        {int  buy = *start;        int  sell = *std::max_element (Start,prices.end ()); if  ((sell-buy) >profit)        {Profit = Sell-buy; }} return  profit;}};  
  • Method 2:accepted

    class  Solution{public : int  maxprofit (vector<int     > &prices) {int  Profit = 0 ;    int  minbuy = Std::numeric_limits<int  >::max (); for  (int  i=0 ; i<prices.size (); i++) {//buy it if current price was less than minimum prices yet  if (prices[i]<minbuy) {minbuy = prices[i]; } //sell it if current profit got optimally  if  ((prices[i]-mi        Nbuy) >profit) {profit = Prices[i]-minbuy; }} return  profit;}};  

Leetcode 121. best time to Buy and Sell stock (dynamic planning, array, simulation) when selling the stock of the year (dynamically planned)

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.