leetcode : Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell 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 complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
Input: [7, 1, 5, 3, 6, 4] Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1] Output: 0 In this case, no transaction is done, i.e. max profit = 0.
题意:一次买卖
思路:前进过程中,实时记录到目前为止最小值,并实时计算收益;
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int len = prices.size(); 5 int curmin = pow(10,6); 6 int ans=0; 7 for(int i=0;i<len;i++) 8 { 9 if(prices[i]<curmin) 10 curmin = prices[i]; 11 else 12 { 13 int tmp = prices[i] - curmin; 14 if(tmp>ans) 15 ans = tmp; 16 } 17 } 18 return ans; 19 } 20 };
122. Best Time to Buy and Sell 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 many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
题意:多次买卖,买前卖
思路:能卖则卖;保留前一刻的值,和当前值比较;
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int len = prices.size(); 5 int ans = 0; 6 if(len == 0) 7 return 0; 8 int cur = prices[0]; 9 for(int i=0;i<len;i++) 10 { 11 if(prices[i]>cur) 12 { 13 ans += prices[i]-cur; 14 cur = prices[i]; 15 } 16 else 17 { 18 cur = prices[i]; 19 } 20 } 21 return ans; 22 } 23 };
123. Best Time to Buy and Sell 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.
题意:最多两次买卖
思路:
每一次操作有几种类型:第一次买、第一次卖、第二次买、第二次卖;
分别定义数组,a[i]表示到i位置为止,每种类型的最大收益;
由于只使用最近一次记录,故数组简化成变量;
(最初想法是:将数组进行划分,然后分别进行只有一次的买卖操作,结果超时)
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int len = prices.size(); 5 if(len==0) 6 return 0; 7 int ans=0; 8 int b1 = -pow(10,6); 9 int b2 = -pow(10,6); 10 int s1 = -pow(10,6); 11 12 for(int i=0;i<len;i++) 13 { 14 int pb1 = b1; 15 b1 = max(b1,-prices[i]); 16 int ps1 = s1; 17 s1 = max(s1,pb1+prices[i]); 18 int pb2 = b2; 19 b2 = max(b2,ps1-prices[i]); 20 ans = max(ans,pb2+prices[i]); 21 } 22 return max(ans,s1); 23 } 24 };
188. Best Time to Buy and Sell Stock IV
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 k transactions.
题意:最多k次买卖
思路:
延续Best Time to Buy and Sell Stock III的解法,只是需要用数组来保存变量;
b[j]表示到i时,进行了j次买的最大收益;
s[j]表示到i时,进行了j次卖的最大收益;
注意:
当k大于元素的个数时,实际相当于Best Time to Buy and Sell Stock II;
因为k次交易次数肯定用不完,即没有限制;
1 class Solution { 2 public: 3 int maxProfit(int k, vector<int>& prices) { 4 int len = prices.size(); 5 if(len==0) 6 return 0; 7 if(k>=len) 8 return maxProfit2(prices); 9 int tmp = -pow(10,6); 10 vector<int> b(k+1,tmp); 11 vector<int> s(k+1,tmp); 12 int ans=0; 13 int pbj; 14 int psj; 15 int psj1=0; 16 17 for(int i=0;i<len;i++) 18 { 19 psj1=0; 20 for(int j=1;j<=k;j++) 21 { 22 pbj = b[j]; 23 psj = s[j]; 24 b[j] = max(pbj,psj1-prices[i]); 25 s[j] = max(psj,pbj+prices[i]); 26 ans = max(ans,s[j]); 27 psj1 = psj; 28 } 29 } 30 31 return ans; 32 } 33 int maxProfit2(vector<int>& prices) { 34 int len = prices.size(); 35 int ans = 0; 36 if(len == 0) 37 return 0; 38 int cur = prices[0]; 39 for(int i=0;i<len;i++) 40 { 41 if(prices[i]>cur) 42 { 43 ans += prices[i]-cur; 44 cur = prices[i]; 45 } 46 else 47 { 48 cur = prices[i]; 49 } 50 } 51 return ans; 52 } 53 54 };
309. Best Time to Buy and Sell Stock with Cooldown
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 many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
- You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
- After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
prices = [1, 2, 3, 0, 2] maxProfit = 3 transactions = [buy, sell, cooldown, buy, sell]
题意:多次买卖,买前卖,卖完间隔一天才能买
思路:
状态表达式稍微修改即可,
buy[i]=max{buy[i-1], sell[i-2]-prices[i]};//因为要间隔一天
sell[i]=max{sell[i-1], buy[i-1]+prices[i]};
其次可用变量替换数组;
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int presell=0; 5 int sell=0; 6 int prebuy=0; 7 int buy= -pow(10,10); 8 int len = prices.size(); 9 for(int i=0;i<len;i++) 10 { 11 prebuy = buy; 12 buy = max(presell-prices[i],buy); 13 14 presell = sell; 15 sell = max(prebuy+prices[i],sell); 16 } 17 return sell; 18 } 19 20 };
注意:
上述部分题需要注意初值情况,如buy[1]的处理;
另附:
当需要一个无穷小数时,视情况而定,可用一个非常小的数来代替;如这里的-pow(10,6);