[Leetcode] 22. best time to Buy and Sell Stock II Java

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

test instructions and analysis: Suppose there is an array, the first element of which is the price of a given stock in the first day. Design an algorithm to find the maximum profit. You can do as many trades as possible (buy and sell stocks multiple times). However, you cannot participate in multiple transactions at the same time (you must sell the stock before you buy again). For one day, if the price of the next day is higher than the current price then you can buy, and then sell the next day, the yield is the price difference, and so on, until the next day, can prove that this can get the maximum profit. So you can scan prices directly from start to finish, if price[i] – price[i-1] greater than zero is counted into the final revenue, this is the greedy method. 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 2 trading. But the right thing to do is: Buy the 3rd day on the 1th day. Although the two methods of numerical results are true, the logic is different:

Code:

public class Solution {public    int maxprofit (int[] prices) {        if (prices.length==0| | Prices.length==1) return 0;        int res=0;        for (int i=0;i<prices.length-1;i++) {        if (prices[i+1]>prices[i])        res+=prices[i+1]-prices[i];        }        System.out.println (res);        return res;    }}

But this is a violation of the title can not be included in the same day buy and sell, so it is possible to find the reduction of the local lowest point, and then find the increment of the local highest points, calculate once plus, and then find. This will ensure that the sale is not on the same day.

Code:

public class Solution {public    static int Maxprofit (int[] prices) {        int len = prices.length;        if (Len <= 1)            return 0;        int total=0;//total revenue        int i=0;//current position                while (i<len-1) {        int buy,sell;//record buy sell time while        (i<len-1 &&prices[i+1]<prices[i]) {//Find local minimum point        i++;        }        buy=i;//buy Point        i++;//sell point at least at the next point of the buy point while        (I<len&&prices[i]>=prices[i-1]) {//Find local maximum point        i ++;        }        sell=--i;//sell point total        +=prices[sell]-prices[buy];        }        return total;    }}

  

  

  

  

[Leetcode] 22. best time to Buy and Sell Stock II Java

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.