Leetcode 123 best time to Buy and Sell Stock III

Source: Internet
Author: User

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 are in most of the transactions.

Note:

Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).

Thought: My brain is more stupid, the idea is more unpopular, and high efficiency.

The maximum time complexity is O (n^2), the minimum is O (n), is not fixed, and the space complexity is O (n). Why the complexity of time is not fixed, the following again. Note If the purchase is divided into two times, the time between the first sell and the second buy. The stock is bound to fall, otherwise it will not need to be purchased two times. If he buys from day a for the first time, sells it in Day B and buys it from day C.

From day B to day C, at least the stock price fell one time, or even a continuous decline, so we just need to find sub[i]<0 place to disconnect. Try it out in two purchases. So when the stock price rises 2i days. The first 2i+1 days dropped, or the first 2i days fell. When the first 2i+1 day rises. Time Complexity of O (n^2)

1. Use an array of sub[n-1]. (n is the number of days of the stock price) to store the difference between the day and the previous stock, that is, sub[i]=prices[i+1]-prices[i], which translates the problem into one or two maximum sub-sequences.

2. When the stock has fallen from the 2nd day to the M-day. On the m+1 day, the stock price rises, then the sub[0...m-1] is set to 0. When the stock rises in K days. From k+1 to Nth, the SUB[K+1....N] is set to 0 (this step is not required for optimization)

3. Calculate the maximum benefit of a purchase only once, compared with the total of when sub[i]<0 (this I may have more than one), two times the income of the purchase. You can take the maximum value.

Version number 1. Use an array, faster

public class Solution {public int maxsum (int[] num,int start,int end) {int sum = 0;int max = integer.min_value;for (int i = start; I <=end; i++) {sum + = num[i];if (Max < sum) {max = sum;} if (sum <= 0) {sum = 0;}} return max>>0?max:0;} public int Maxprofit (int[] prices) {if (Prices.length = = 0 | | prices.length = 1) return 0;int[] Sub = new Int[prices.lengt H-1];boolean flag=false;for (int i = 0; i < sub.length; i++) {if (prices[i+1]-prices[i]>=0| | Flag) {flag=true;    Sub[i] = prices[i + 1]-prices[i];} else{sub[i]=0;}} for (int i = sub.length-1; 0 <= i; i--) {if (prices[i+1]-prices[i]<0) {    sub[i] = 0;} Else{break;}} int result=maxsum (sub,0,sub.length-1); for (int i=0;i<sub.length;i++) {if (sub[i]<0) {int temp=maxsum (sub,0,i-1) +maxsum (sub,i+1,sub.length-1); result=result>temp?result:temp;}} return result;}}

Version number 2, using ArrayList, when stocks continue to fall from the beginning, or from a certain time to the end has been falling. These sub values are removed.

Import Java.util.arraylist;public class Solution {public int maxsum (arraylist<integer> num,int start,int end) {i NT sum = 0;int max = integer.min_value;for (int i = start; I <=end; i++) {sum + = Num.get (i); if (Max < sum) max = sum ; if (sum <= 0) sum = 0;} return max>0?max:0;} public int Maxprofit (int[] prices) {if (prices.length==0| | Prices.length==1) return 0; arraylist<integer> sub = new Arraylist<integer> (); boolean flag=false;for (int i = 0; i < prices.length-1; i++) {if (prices[i+1]-prices[i]>=0| |    Flag) {flag=true; Sub.add (prices[i + 1]-prices[i]);}} for (int i = prices.length-2; 0 <= i; i--) {if (!sub.isempty () &&prices[i+1]-prices[i]<0) Sub.remove ( Sub.size ()-1); else break;} if (Sub.size () ==0) return 0;int result=maxsum (Sub,0,sub.size ()-1); for (int i=0;i<sub.size (); i++) {if (Sub.get (i) <0) {int temp=maxsum (sub,0,i-1) +maxsum (Sub,i+1,sub.size ()-1); result=result>temp?result:temp;}} return result;}}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Leetcode 123 best time to Buy and Sell Stock III

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.