1 . The best time to buy and sell stocks
Suppose there is an array, the first element of which is the price of a given stock in the first day. If you are allowed to complete only one transaction at most (for example, buying and selling a stock), design an algorithm to find out the maximum profit.
1 Public classSolution {2 /**3 * @paramprices:given an integer array4 * @return: Maximum Profit5 */6 Public intMaxprofit (int[] prices) {7 //Write your code here if (PRICES.LENGTH<2) return 0;8 if(PRICES.LENGTH<2)return0;9 intMaxprofit = 0;Ten intMinprice = Prices[0]; One for(inti=1;i<prices.length;i++){ AMinprice =math.min (Minprice, prices[i]); -Maxprofit = Math.max (Maxprofit, prices[i]-minprice); - } the returnMaxprofit; - } -}
2, the best time to buy and sell stocks II
Suppose there is an array, the first element of which is the price of a given stock in the first day. Design an algorithm to find the maximum profit. You can do as many trades as possible (buy and sell stocks multiple times). However, you cannot participate in multiple transactions at the same time (you must sell the stock before you buy again).
This can also be counted as a relatively simple greedy algorithm, if the current price is higher than yesterday's price, we will trade, until the completion of the traversal.
1 classSolution {2 /**3 * @paramprices:given an integer array4 * @return: Maximum Profit5 */6 Public intMaxprofit (int[] prices) {7 //Write your code here8 if(PRICES.LENGTH<2)return0;9 intMaxprofit = 0;Ten for(inti=1;i<prices.length;i++){ One intDiff=prices[i]-prices[i-1]; A if(diff>0){ -Maxprofit = maxprofit+diff; - } the } - returnMaxprofit; - } -}
3, the best time to buy and sell stocks III
Suppose you have an array, the first element of which is the price of a given stock in the first day. Design an algorithm to find the maximum profit. You can complete up to two trades.
Sample Example
Give a sample array [4,4,6,1,1,4,2,5], return 6
Note
You may not participate in multiple transactions at the same time (you must sell the stock before you buy again)
Analysis of the idea: this question and the second question of the feeling is not the same, the use of the idea of dynamic planning, can be divided into two, since the most complete double transaction, assuming that the first transaction occurs on day I, you can think of this issue before I seek maximum profit and I after the maximum profit, the last two plus. Through the traversal of I, we can find the best I for the whole array, and get the maximum profit.
classSolution {/** * @paramprices:given an integer array *@return: Maximum Profit*/ Public intMaxprofit (int[] prices) { //Write your code here if(Prices.length < 2)return0; intn=prices.length; int[] profit1=New int[n]; int[] profit2=New int[n]; for(inti=0;i<n;i++){ intMinprice=prices[0]; intMaxprofit=0; for(intj=0;j<i;j++) {Minprice=math.min (Minprice,prices[j]); Maxprofit= Math.max (maxprofit,prices[j]-minprice); } Profit1[i]=Maxprofit; Minprice=Prices[i]; Maxprofit=0; for(intj=i;j<n;j++) {Minprice=math.min (Minprice,prices[j]); Maxprofit= Math.max (maxprofit,prices[j]-minprice); } Profit2[i]=Maxprofit; } intMaxprofit = 0 ; intCurprofit = 0; for(inti=0;i<n;i++) {Curprofit=profit1[i]+Profit2[i]; if(Curprofit>maxprofit) maxprofit=Curprofit; } returnMaxprofit; }};the best time to buy and sell stocks IV I'm not going to write this question, wait till it's written and then updated .
Best-time-to-buy-and-sell-stock I &&ii && III && ivbest-time-to-buy-and-sell-stock-ii