best time to Buy and Sell Stock II

Source: Internet
Author: User

https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

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

Problem Solving Ideas:

You can buy and sell multiple times compared to the best time to buy and Sell stock issues.

A more tricky solution because there is no limit to the number of trades. As long as the next day is the price increase, you can immediately sell, and then buy, no need to consider whether the third day is also to increase prices. Because it is the same as 1-3 of the money that is earned.

Compare the solution of the rules. Compare the value of two days before and after, as long as the price increases, take in hand, do not sell. A price reduction proves that the day before is sold. At the same time assume that the price of the day to buy, so reciprocating.

 Public classSolution { Public intMaxprofit (int[] prices) {        intMaxprofit = 0;  for(inti = 0; I <prices.length;) {intj = i + 1;  while(J < prices.length && Prices[j] >= prices[j-1]) {J++; } Maxprofit+ = Prices[j-1]-Prices[i]; I=J; }        returnMaxprofit; }}

This is an implementation of the above ideas, the same as O (n^2), but because I jump directly to J, actually close to O (n).

 Public classSolution { Public intMaxprofit (int[] prices) {        intMaxprofit = 0; intBuy_price = 0; if(Prices.length = = 0){            return0; }Else{Buy_price= Prices[0]; }                 for(inti = 1; I <prices.length;) {if(Prices[i] < prices[i-1]) {Maxprofit+ = Prices[i-1]-Buy_price; Buy_price=Prices[i]; }            if(i = = prices.length-1 && prices[i] >= prices[i-1]) {Maxprofit+ = Prices[i]-Buy_price; } I++; }        returnMaxprofit; }}

This is an O (n) implementation. Deposit Prices[i] As a buy value, it is updated when the price falls, and the maximum profit amount is calculated. Consider a special case, the last interval is always the price, so this situation only judge I has been to the end, and the price to come to a close, otherwise it will miss the profit value of this interval.

Again, this method is less clear than the above.

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.