121.
Say you has an array for which the i-th element is the price of a given-stock on day I.
If you were-permitted-to-complete at most one transaction (ie, buy one and sell one share of the stock), design an AL Gorithm to find the maximum profit.
classSolution { Public: intMaxprofit (vector<int>&prices) { intn =prices.size (); if(N <1) return 0; intMini = prices[0], ans =0, I; for(i =1; I < n; i++) { if(Prices[i]-mini >ans) ans= prices[i]-Mini; if(Prices[i] <Mini) Mini=Prices[i]; } returnans; }};
122.
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 could complete as many transactions as (ie, buy one and sell one share of the stock multiple times). However, engage in multiple transactions for the same time (ie, you must sell the stock before you buy again).
classSolution { Public: intMaxprofit (vector<int>&prices) { intn =prices.size (); if(N <1) return 0; intMini = prices[0], Maxi = prices[0], ans =0, I; for(i =1; I < n; i++) { if(Prices[i] >Maxi) Maxi=Prices[i]; Else if(Prices[i] <Maxi) {ans+ = maxi-Mini; Maxi= Mini =Prices[i]; }} ans+ = maxi-Mini; returnans; }};
123.
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.
classSolution { Public: intMaxprofit (vector<int>&prices) { intn =prices.size (); if(N <1) return 0; Vector<int> Forward (n,0), Backward (n,0); intMini, Maxi, ans, I; forward[0] =0; Mini= prices[0]; for(i =1; I < n; i++) {Forward[i]= Max (forward[i-1], Prices[i]-mini); if(Prices[i] <Mini) Mini=Prices[i]; } backward[n-1] =0; Maxi= prices[n-1]; for(i = n2; I >=0; i--) {Backward[i]= Max (backward[i+1], Maxi-Prices[i]); if(Prices[i] >Maxi) Maxi=Prices[i]; } ans=0; for(i =0; I < n; i++) {ans= Max (ans, forward[i] +Backward[i]); } returnans; }};
188.
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. Transactions at the most K.
Note:
Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).
classSolution { Public: intMaxprofit (intK, vector<int>&prices) { intn =prices.size (), I, J; if(N <1) return 0; if(k >= (n>>1)) { intAns =0; for(i =0; I < n1; i++) { if(prices[i+1]-prices[i] >0) ans+ = prices[i+1]-Prices[i]; } returnans; } Vector<int> Buy (k +1, Int_min), sell (k +1,0); for(i =0; I < n; i++) { for(j =1; J <= K; J + +) {Buy[j]= Max (Buy[j], sell[j-1]-prices[i]); SELL[J] = max (Sell[j], buy[j] + prices[i]); } } returnSell[k]; }};
Buy[i] says buy I a maximum amount of money left. Sell[i] Indicates how much money I have to sell.
Buy[j] = max (Buy[j], sell[j-1]-prices[i]); See buy Prices[i] is the original cost-effective
classSolution { Public: intMaxprofit (intK, vector<int>&prices) { intn =prices.size (), I, J; if(N <1) return 0; if(k >= (n>>1)) { intAns =0; for(i =0; I < n1; i++) { if(prices[i+1]-prices[i] >0) ans+ = prices[i+1]-Prices[i]; } returnans; } Vector<vector<int>> DP (N, vector<int> (k +1,0));//Dp[i][j] said that to the first day sell J to make up how much money for(i =1; I <= K; i++) { intbuy =-prices[0]; for(j =0; J < N; J + +) {Dp[j][i]= Max (J >0? dp[j-1][i]:0, Buy +Prices[j]); Buy= Max (Buy, dp[j][i-1] -Prices[j]); } } returndp[n-1][k]; }};
The same idea as the previous algorithm.
121.122. 123.188. best time to buy and Sell stock *hard*--buy and sell shares