1、Best Time to Buy and Sell Stock
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int n = prices.size(); 5 if(n <= 1) 6 return 0; 7 vector<int> buy(n,0); 8 vector<int> sell(n,0); 9 buy[0]= prices[0]; 10 for(int i=1; i<n; i++) 11 { 12 if(prices[i] < buy[i-1]) 13 buy[i] = prices[i]; 14 else buy[i] = buy[i-1]; 15 } 16 sell[n-1]= prices[n-1]; 17 for(int j=n-2; j>=0; j--) 18 { 19 if(prices[j] >sell[j+1]) 20 sell[j] = prices[j]; 21 else sell[j] = sell[j+1]; 22 } 23 int max = 0; 24 for(int k=0; k<n; ++k) 25 { 26 if(max < sell[k]-buy[k]) 27 max = sell[k]-buy[k]; 28 } 29 return max; 30 } 31 };
2 、Best Time to Buy and Sell Stock II
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 if(prices.size() < 1) //prices.size is unsigned int 5 return 0; 6 int pro = 0; 7 for(int i=0; i<prices.size()-1; i++) 8 { 9 if(prices[i+1] >prices[i]) 10 pro += prices[i+1]-prices[i]; 11 } 12 return pro; 13 } 14 };
实际股票是不可能知道今天以后的票价,否则就会根据走向趋势,每次取谷底与山顶就可以了。
3、Best Time to Buy and Sell Stock with Cooldown
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int n = prices.size(); 5 if(n < 2) 6 return 0; 7 vector<int> sells(n, 0); 8 vector<int> buys(n, 0); 9 int delay = 0; 10 sells[0] = 0; 11 buys[0] = -prices[0]; 12 sells[1] = prices[1]-prices[0]; 13 buys[1] = -prices[1]; 14 int res = max(0, prices[1]-prices[0]); 15 for(int i=2; i<n; ++i) 16 { 17 delay = prices[i]-prices[i-1]; 18 buys[i] = max(sells[i-2]-prices[i], buys[i-1]-delay); 19 sells[i] = max(buys[i-1]+prices[i], sells[i-1]+delay); 20 if(res <sells[i]) 21 res = sells[i]; 22 } 23 return res; 24 } 25 };
分析见:http://bookshadow.com/weblog/2015/11/24/leetcode-best-time-to-buy-and-sell-stock-with-cooldown/
4、Best Time to Buy and Sell Stock III
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int n = prices.size(); 5 if(n <= 1) 6 return 0; 7 vector<int> lmax(n,0); 8 vector<int> rmax(n,0); 9 int minb = prices[0]; 10 lmax[0] = 0; 11 for(int i=1; i<n; i++) 12 { 13 minb = min(minb, prices[i]); 14 lmax[i] = max(lmax[i-1], prices[i]-minb); 15 } 16 rmax[n-1] = 0; 17 int maxs = prices[n-1]; 18 for(int j=n-2; j>=0; --j) 19 { 20 maxs = max(maxs, prices[j]); 21 rmax[j] = max(rmax[j+1], maxs-prices[j]); 22 } 23 maxs = 0; 24 for(int k=0; k<n; ++k) 25 { 26 maxs = max(maxs, lmax[k]+rmax[k]); 27 } 28 return maxs; 29 } 30 };
5、 Best Time to Buy and Sell Stock IV
1 class Solution { 2 public: 3 int maxProfit(int k, vector<int>& prices) { 4 int n = prices.size(); 5 if(n<1 || k<1) 6 return 0; 7 if(k > n) 8 return maxPro(prices); 9 vector<int> global(k+1, 0); 10 vector<int> local(k+1, 0); 11 for(int i=0; i<n-1; i++) 12 { 13 int dif = prices[i+1] -prices[i]; 14 for(int j=k; j>=1; --j) 15 { 16 local[j] = max(global[j-1]+max(dif, 0), local[j]+dif); 17 global[j] = max(global[j], local[j]); 18 } 19 } 20 return global[k]; 21 22 } 23 private: 24 int maxPro(vector<int>&prices) 25 { 26 int max = 0; 27 for(int i=1; i<prices.size(); i++) 28 { 29 if(prices[i]-prices[i-1] > 0) 30 max += prices[i] - prices[i-1]; 31 } 32 return max; 33 } 34 35 };