121. Best time to Buy and Sell Stock (one) Leetcode problem-solving notes

Source: Internet
Author: User

121. Best time to Buy and Sell Stock

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.

It's like I'm afraid we can't understand it. Two examples are given in fact this topic means to give you an array of the elements of the element I mean the price of the first day of the commodity how to choose the best time to buy and sell goods to maximize profits (only one transaction allowed)

It is no doubt to try to buy the highest when the lowest time to sell the simultaneous buy times must be before the sell time feel a traverse enough to maintain a minimum value

The idea is to iterate from the first element while maintaining the minimum and maximum profit values of a traversed element to facilitate the return benefit

There's a way to get the code out of here. Try to write code in a text editor or directly in the Leetcode code input box without overly relying on tools unless you can find a problem and then use the tool to debug it.

public class Solution {public    int maxprofit (int[] prices) {        int min=integer.max_value;        int profit=0;        for (int i=0;i<prices.length-1;i++) {        min=prices[i]<min?prices[i]:min;        profit= (prices[i+1]-min) >profit?prices[i+1]-min:profit;        }        return profit;}    }

is still a one-off.

Convention See details

can also 2ms O (1) space complexity O (n) time complexity personally think that there is no optimal solution time is shorter than I may be the test case is different here is not to see other people's discuss no need to go to the next question

121. Best time to Buy and Sell Stock (one) Leetcode problem-solving notes

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.