best time to Buy and Sell Stock--leetcode

Source: Internet
Author: User

Original title address: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

The content is in the code and comments, it is not wordy.

Class Solution {Public:int Maxprofit (vector<int>& prices) {/* This type of topic has been encountered several times, is to find in a one-dimensional array of two values combined to meet a        Nature, it seems only to use the poor lifting method each time to remove two values to check whether to meet the required nature, in fact, to use the topic given the situation, to extract some restrictions or (hidden) properties, narrow the search scope. For this problem, the maximum value of the difference between the two elements in the array is actually required, and the two elements satisfy the following properties: 1. Unless the daily stock price remains constant, the larger value must be after a smaller value (obviously, you can't buy stocks today, and then sell them yesterday, time can't go back, The sale proceeds for the same day is 0); 2. The income is independent of the number of days between trading shares, so for a prices[i] you only need to find the maximum value of its right element or the minimum value of the left element, then Prices[i] is the largest of its difference. The use of this property does not need to be exhaustive all prices[i] and then to the left or right of the value of the search, otherwise the complexity is still O (n^2). Find out that the maximum (small) element can use the linear complexity of the iterative method, traversing the prices is also linear complexity, which is a combination of (this is a very broad, Generalized words) up, still linear complexity, only a single traversal. Because the nature of 1 indicates that you do not have to find the current maximum (small) element, search for the element before it. The following code, while traversing prices, finds the smallest element in the traversed element, which is equivalent to determining the day the stock is sold, then buying the day of the lowest stock price in the previous days (which is logically, of course, impossible) to get the most benefit. These two things can be done "at the same time" to "determine the day of the stock sale" and "find the day when the stock price is the lowest".        When you are sure that the day before the stock price is the lowest in the day before you sell the stock, you do not have to consider the other days before the day of selling the stock, which greatly reduces the number of elements to be searched and reduces the complexity.        */if (prices.size () <= 0) {return 0;                } int max = 0, min = prices[0]; for (int i = 1; i < prices.size (); ++i) {if(Prices[i] < min)            {min = prices[i];            } else if (Prices[i]-min > Max) {max = prices[i]-min;    }} return max; }};


best time to Buy and Sell Stock--leetcode

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.