Leetcode: best time to buy and stock Stock III

Source: Internet
Author: User
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Difficulty: 99

Refer to the Code ganker solution.

This is an extension of best time to buy and stock. Now we can make up to two transactions. We still use dynamic planning to complete the process. In fact, it can solve a very general situation, that is, the situation where a maximum of K transactions can be performed.
Here we first explain the algorithm that can be used for a maximum of K transactions, and then we only need to set K to 2 for a maximum of two transactions. We still use "local optimization and global optimization ". We maintain two types of transactions. One is the maximum number of J transactions that can be made on the day I (Global [I] [J]). the other is the current day I, where a maximum of J transactions can be made, and the best profit of the last transaction sold on the current day (local [I] [J]). Let's look at the recursive formula. The global structure is relatively simple,

Global [I] [J] = max (local [I] [J], global [I-1] [J]),

That is, the best part of the current transaction, and the largest one in the past (because if the last transaction contains the best part of the current day, otherwise, it must be in the global optimization of the past ). For maintenance of local variables, the recursive formula is

Local [I] [J] = max (Global [I-1] [J-1] + max (diff, 0), local [I-1] [J] + diff ),

That is to see two quantities, the first is the global to I-1 days for J-1 transactions, and then add today's transactions, if today is making money (that is, as long as the previous J-1 transactions, the last transaction takes the current day), the second is to take the local I-1 day J transaction, then add the difference today (here because of the local [I-1] [J] such as the transaction that contains the day of the I-1 sold, so now it is the day of the I sold, does not increase the number of transactions, in addition, whether diff is greater than 0 or not, it must be added because otherwise, the local [I] [J] must be sold on the last day ).
In the above algorithm, a scan is required for the number of days, and a recursive solution is required for the number of transactions each time. Therefore, the time complexity is O (n * k). If a maximum of two transactions are performed, so the complexity is O (n ). In space, you only need to maintain the data of the current day, so it is O (k). When k = 2, it is O (1 ).

 

 1 public class Solution { 2     public int maxProfit(int[] prices) { 3         return helper(prices, 2); 4     } 5      6     public int helper(int[] prices, int k) { 7         int len = prices.length; 8         if (len == 0) { 9             return 0;10         }11         int[][] local = new int[len][k+1];12         int[][] global = new int[len][k+1];13         for (int i=1; i<len; i++) {14             int diff = prices[i] - prices[i-1];15             for (int j=1; j<=k; j++) {16                 local[i][j] = Math.max(global[i-1][j-1]+Math.max(diff, 0), local[i-1][j]+diff);17                 global[i][j] = Math.max(local[i][j], global[i-1][j]);18             }19         }20         return global[len-1][k];21     }22 }

 

 

Leetcode: best time to buy and stock 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.