*best time to Buy and Sell Stock II

Source: Internet
Author: User

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

Exercises

The simple way is that once the next day's price is higher than the previous day, it is sold the next days before buying. The code is as follows:

1  Public intMaxprofit (int[] prices) {2     intTotal = 0;3      for(intI=1; i< prices.length; i++) {4         if(prices[i]>prices[i-1]){ 5             intProfit = Prices[i]-prices[i-1];6Total + =profit;7         }8     }9     returnTotal ;Ten}

But this will violate the "can't buy the same day rules", for example, 3 days of the price is three-to-one, then according to the above algorithm will be on the same day in 2 trading ...

The right thing to do is to buy the 3rd day on the 1th day.

Although the two methods of numerical results are true, the logic is different:

However, despite this, the ability of the problem is still to let you find the biggest profit, and did not let you know which day to buy the day of sale.

So to the interviewer you can still say that this method just helps me to find the maximum profit, I did not say that the same day to buy and sell, just calculate: All the next days than the previous one big difference, I was to find the maximum profit (draw a trend map to explain the line). )。

But if it's not that slightly opportunistic, why do you have to add a little bit of growth every time, bringing unnecessary explanations?

Why not find the reduced local lows first, then find the incremental local highs, count the plus, and then find them again? This will ensure that the sale is not on the same day.

The code is as follows:

1  Public Static intMaxprofit (int[] prices) {2         intLen =prices.length;3         if(Len <= 1)4             return0;5         6         inti = 0;7         intTotal = 0;8          while(I < len-1){9             intBuy,sell;Ten             //look for the last value of the descending interval (local minimum point) One              while(I+1 < Len && Prices[i+1] <Prices[i]) Ai++; -             //Local minimum point as buy point -buy =i; the              -             //find the next point (the sell point is at least the next point) -i++; -             //not satisfied. Continue to find the last value of the increment interval (local highest point) +              while(I<len && prices[i] >= prices[i-1]) -i++; +             //set up a sell point ASell = I-1; at             //Calculate sum -Total + = Prices[sell]-Prices[buy]; -         } -         returnTotal ; -}

Reference

Http://www.cnblogs.com/springfor/p/3877065.html

Http://www.cnblogs.com/TenosDoIt/p/3436457.html

*best time to Buy and Sell Stock II

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.