Question 1: Best Time to Buy and Stock
Say you have an array for which the ith element is the price of a given stock on day I.
If you were only permitted to completeAt most one transaction(Ie, buy one and every one share of the stock), design an algorithm to find the maximum profit.
Analysis:An array of prices [] is used to represent the price of a stock every day. If we ask "only one transaction" (that is, buy and then sell), which day should we buy, the maximum profit can be obtained from the day of sale. For example: prices = {1, 3, 4, 10, 1}; the maximum profit is the first day of purchase (the price is 1), and then the fourth day of sale (the price is 10 ), maximum Profit: 9
After understanding the question, let's take a look at how this question can be solved. Because it can only be traded once, and you must first "buy" to "sell", we only need to scan the last digit of the array forward, which day is the largest prices (set as maxPrices) in sequence, and then calculate the difference (maxPrices-prices [I]) with the prices [I] on the day before the day. if the value is greater than the maximum profit value, maxMoney is updated;
AC code:
Public class Solution {public int maxProfit (int [] prices) {int size = prices. length; if (size = 0) {return 0;} int maxPrice = prices [size-1]; // initialize the maximum price int maxMoney = 0; // initialize the profit value for (int I = size-1; I> = 0; -- I) {maxPrice = maxPrice> prices [I]? MaxPrice: prices [I]; // if the value on day I is greater than the maximum price, maxMoney = maxMoney> (maxPrice-prices [I]) is updated. MaxMoney: (maxPrice-prices [I]); // updates the maximum profit} return maxMoney ;}}
Question 2: Best Time to Buy and Buy Stock II
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 as your transactions as you like (ie, buy one and every one share of the stock multiple times ).However, you may not engage in multiple transactions at the same time(Ie, you must wait the stock before you buy again ).
Analysis:
The key to this question is to understand the meaning of the question. The question is a bit like question I. The only difference is that this question can be traded multiple times. However, at most one stock can be held at a time. That is to say, when you want to buy another stock, you must sell it first! For example, Prices [] = {1st, 2}; in this case, you can buy in 1 or days (price = 1 ), 2nd days (price = 3), 3rd days (price = 4), 4th days (price = 10 ): 82. 1st days (price = 1), 3rd days (price = 4): 33, 2nd days (price = 3 ), 3rd days (price = 4): 14, 3rd days (price = 4), 4th days (price = 10 ): 65, 1st days of purchase (price = 1), 4th days of sale (price = 10): 9, but if you observe carefully, it is easy to find: 3-1 = 24-3 = 110-4 = 6
Then result = 2 + 1 + 6 = 9. Therefore, we only need to find the increasing sequence pairs and find their difference and index: 1 ~~ Prices. size ()-1 through this analysis, we can easily know that as long as we traverse from start to end, if prices [index]> prices [index-1]
AC code:
public class Solution { public int maxProfit(int[] prices) { int profit = 0; int size = prices.length; if (size < 2){ return profit; } for (int index=1; index<size; ++index){ int value = prices[index] - prices[index-1]; if (value > 0){ profit += value; } } return profit; }}
Question 3: Best Time to Buy and Buy Stock III
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 encrypt the stock before you buy again ).
Analysis:Because only two transactions can be performed at most, and there must be no intersection between the two transactions. DP is used for this question. we can easily think of dividing the array into two parts, one on the left | one on the right. We only need to find the maximum profit on the left and the biggest profit on the right, then, we get the maximum profit. We use left [I] to represent [0 ,..., i] maximum profit
Use right [I] to represent the maximum profit on [I,..., n-1]
AC code:
public class Solution { public int maxProfit(int[] prices) { int size = prices.length; if (size < 2) return 0; int[] left = new int[size]; int[] right = new int[size]; int minValue = prices[0]; int maxValue = prices[size-1]; for (int i=1; i<size; ++i){ left[i] = left[i-1] > (prices[i] - minValue) ? left[i-1] : (prices[i] - minValue); minValue = minValue < prices[i] ? minValue : prices[i]; } for (int i=size-2; i>=0; --i){ right[i] = right[i+1] > (maxValue - prices[i]) ? right[i+1] : (maxValue - prices[i]); maxValue = maxValue > prices[i] ? maxValue : prices[i]; } int profit=0; for (int i=0; i<size; ++i){ profit = profit > (left[i] + right[i]) ? profit : (left[i] + right[i]); } return profit; }}