[Leetcode] 121. best time to Buy and Sell Stock 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.

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: [7, 1, 5, 3, 6, 4]output:5max. difference = 6-1 = 5 (Not 7-1 = 6, as selling-price needs-be-larger than buying price)

Example 2:

 Input: [7, 6, 4, 3, 1]output:0in this case, no transaction was done, i.e. Max Profit = 0. 

test instructions and analysis: give a price sequence for a stock, Buy one sell at a time and ask for the maximum profit. is to ask for an array, the following number minus the previous number to get the maximum value. The easiest thing to think about is to select a number at a time, traverse the number that follows, find the direct difference and then compare it to the current maximum, so that the time complexity is O (n*n),
(1) If the current number is greater than the previous number, you can easily reach the maximum value that can be reached at that point Res[i] is the maximum value of the previous number + (Prices[i]-prices[i-1]), and then compares with the current maximum value, which is greater than the current maximum, resets the maximum value of
(2) If the current number is less than the previous number, and is divided into two cases
A. If the maximum <=0 to the previous number, So the maximum difference that can reach the current point Res[i] is 0
B. If the maximum difference of the previous number is greater than 0, then the maximum difference of the current point can be reached Res[i] Math.max (res[i-1]+ (prices[i]-prices[i-1)), 0);
You can get an answer to a number by processing the remaining number in the last loop alone.

Code:
public class Solution {public    int maxprofit (int[] prices) {        int length=prices.length;        Int[] Res=new int[length];        int max=0;        if (length<=1)        return 0;        res[0]=0;        res[1]=0;        for (int i=1;i<length;i++) {        if (Prices[i]>prices[i-1]) {        res[i]=res[i-1]+ (prices[i]-prices[i-1]);        if (Max<res[i])        max=res[i];        } else{        if (res[i-1]<=0)        res[i]=0;        else{        Res[i]=math.max (res[i-1]+ (prices[i]-prices[i-1]), 0);        }} if (max>0) return max;        else return 0;}    }

  



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

Related Article

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.