[LeetCode] Best Time to Buy and Buy Stock I II III IV problem solving report, leetcode=
Best Time to Buy and Buy Stock I
Question:An array is used to represent the daily price of the stock, and the number of I in the array indicates the price of the stock on the day I. If only one transaction is allowed, that is, only one stock can be bought and sold for the maximum benefit.
Analysis:Dynamic Programming Method. Traverse the array from the past to the back, record the current lowest price, as the purchase price, and calculate the proceeds sold at the current price, as the biggest possible benefit, throughout the traversal process, the biggest benefit you have ever seen is what you want.
Code:Time O (n), Space O (1 ).
Best Time to Buy and Stock II
Question:An array is used to represent the daily price of the stock, and the number of I in the array indicates the price of the stock on the day I. The number of transactions is unlimited, but only one stock can be traded at a time. That is to say, only one stock can be held at most at a time to maximize returns.
Analysis:Greedy method. Traverse the array from the front to the back, as long as the price of the day is higher than the price of the previous day, even if the inbound income.
Code:Time O (n), Space O (1 ).
Best Time to Buy and Buy Stock III
Question:An array is used to represent the daily price of the stock, and the number of I in the array indicates the price of the stock on the day I. You can trade up to two times, and only one stock can be held in your hand for maximum benefit.
Analysis:Dynamic Programming Method. Based on the dividing line of day I, calculate the maximum profit preProfit [I] for a transaction before day I, and the maximum profit for a transaction after day I. postProfit [I], after the last traversal, max {preProfit [I] + postProfit [I]} (0 ≤ I ≤ N-1) is the biggest benefit. The Best Time to Buy and Buy Stock I method is the same for the maximum benefit obtained before and after day I.
Code:Time O (n), Space O (n ).
Best Time to Buy and Buy Stock IV
Question:An array is used to represent the daily price of the stock, and the number of I in the array indicates the price of the stock on the day I. A maximum of k transactions can be made, and only one stock can be held at most for maximum benefit.
Analysis:Special dynamic programming method. In the traditional dynamic planning, we will think like this, the maximum benefit of performing a j transaction on the day I, either equal to the maximum benefit of the j transaction at the I-1 Day (the price on the I day is lower than the price on the I-1 Day), or equal to the I-1 at the J-1 day, A transaction is then made on the day I (the price on the day I is higher than the price on the day of the i-1 ). The dynamic equations are obtained as follows (diff = prices [I]-prices [I-1]):
Profit [I] [j] = max (profit [I-1] [j], profit [I-1] [j-1] + diff)
It seems quite reasonable, but it is not. Why not? Because diff is the difference between the day I and the day I-1, if the day of the I-1 itself has a transaction, then the two transactions can be combined into a transaction, in this way, profit [I-1] [j-1] + diff actually only deals with the J-1, rather than the most j times, so that the maximum benefit is small.
So how can we calculate the maximum benefit of the transaction on the day I to avoid the calculation of less than one transaction? We use a local optimal solution and a global optimal solution to represent the benefits of performing j operations on the day I, which is a special feature of this dynamic planning.
Use local [I] [j] to indicate the local optimal solution for a maximum of j transactions when the day is reached; Use global [I] [j] to indicate the day when the day is reached, the global optimal solution can be performed up to j times. The relationship between them is as follows (diff = prices [I]-prices [I-1]):
Local [I] [j] = max (global [I-1] [j-1] + max (diff, 0 ), local [I-1] [j] + diff)
Global [I] [j] = max (global [I-1] [j], local [I] [j])
The local [I-1] [j] + diff is designed to avoid merging the I-day transaction and the I-1-day transaction into one transaction, with less than one transaction gain. Reference: http://www.cnblogs.com/grandyang/p/4295761.html
Code:Time O (n), Space O (k ).
Source Address: [LeetCode] Best Time to Buy and Fixed Stock I II III IV