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.
一开始思路:
对于第i天,如果选择在这一天卖出的话,考虑在什么时候买入合适? 思考发现 一定是在[0,i]范围内的价格最低的那一天买入最合适。
那我们
- 初始化最大利润maxProfit = 0;
- 遍历prices数组,过程中记录价格最小值min.当遍历到第i天的时候 min一定是在[0,i]范围内的,比较prices[i]-min和max的大小 max取两者中的最大值
ps: 期间还想过其实没必要考虑每一天卖出的情况,在一个单调序列[i,j]中 计算第i天卖出时min下标<=i;而在计算第j天卖出时 min下标同样<=i.所以第j天卖出肯定比第i天卖出利润大,遍历的时候可以直接考虑单调递增序列的尾部天数卖出。但是感觉复杂度没啥改进,还是要遍历整个数组,而且写起来更加复杂,所以想想还是之前的比较好。
后来想用动态规划,定义c[i]表示[0,i]范围内的maxProfit,min为[0,i-1]范围内的最小值
则有
- 初始c[0] = 0;
- 当prices[i]<min时: 即当前值比前面的最小值还要小,则c[i] = c[i-1];并且把min赋值为prices[i];
- 当prices[i]>=min时: c[i] = max{c[i-1],prices[i]-min};
- 我们要求的maxProfit = c[len-1];
后来一想似乎没必要用数组c去保存因为c[i]肯定是大于等于c[i-1]。所以用一个变量max记录就可以。然后代码写出来,发现跟最开始的思路是一样的-。-
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if(prices == null || prices.length == 0) return 0;
int len = prices.length;
int max = 0;
int min = prices[0];
for(int i = 1;i<=len-1;i++){
if(prices[i]<min) {min = prices[i];}
else max = prices[i]-min>max? prices[i]-min:max;
}
return max;
}
Best Time to Buy and Sell Stock II
与上题的区别在于可以交易n次,算最大利润
直观的想法就是在价格数组prices[]的所有单调序列的头(最低点)买入,尾(最高点)卖出;
然后就想有没有可能不按照上述规则算出的利润更大呢?思考了下,发现不可能
- 对于单调序列内部的买入卖出不难思考 ,肯定是头买入,尾卖出最高。
- 那考虑跨越单调序列的买入卖出:
- 假设[i, k] [k+1, j] 是两个相邻的单调区间,我们在[i , k]内的某一点 x 买入,在[k+1, j ]内的某一点y卖出,首先当x = i和y = j时,是这种买入卖出方式的最大值(因为x不等于i的话,x-1买入肯定比x买入收益更大,y的取值类似)。 那么比较在i点买入,j点卖出和在i点买入,k点卖出,k+1点买入,j点卖出的利润大小:因为分为了两个单调区间,所以price[k+1]<price[k] 根据这个条件可以证明: prices[j] - prices[i] <prices[j] - prices[k+1] + prices[k] - prices[i] 所以第二种方式i点买入,k点卖出,k+1点买入,j点卖出的利润更大。
代码:
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if(prices == null || prices.length == 0) return 0;
int i = 0;
int min,max;
int maxProfit = 0;
int len = prices.length;
while(i<=len-1){
min = prices[i];
max = prices[i];
i++;
while(i<=len-1&&prices[i]>=prices[i-1]) i++;
max = prices[i-1];
maxProfit = maxProfit + max - min;
}
return maxProfit;
}
Best Time to Buy and Sell Stock III
这个的限制就是交易最多2次
刚开始就是想的先确定[0,i]范围内的maxProfit,然后算[i,len-1]范围内的maxProfit 求和,取和中的最大值。
然后就在纠结扫一遍 扫到[0,i]的时候,[0,i]范围内的maxProfit好确定,但是[i,len-1]范围内的觉得不好算。
然后发现自己脑残了...为啥一定要纠结同步确定呢,我扫两遍,第一遍从头到尾把[0,i]范围内的最大确定,像
Best Time to Buy and Sell Stock中那样记录为数组c, 第二遍从尾到头扫,在算出[i,len-1]范围内的最大值之后,直接去数组c中取出[0,i-1]范围内的就可以了...
代码:
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if(prices == null || prices.length == 0) return 0;
int len = prices.length;
int c[] = new int[len];//c[i] means max in interval[0,i]
c[0] = 0;
int min = prices[0];
for(int i = 1;i<=len-1;i++){
if(prices[i]<min) {min = prices[i];c[i] =c[i-1];}
else c[i] = prices[i]-min>c[i-1]? prices[i]-min:c[i-1];
}
int maxProfit = c[len-1];
int max = 0;
int diff = 0;
int tempMax = 0;
for(int j = len-1;j>=0;j--){
if(prices[j]>max) {max = prices[j];}
else if( max-prices[j]>diff) diff = max - prices[j];
if(j>=1) tempMax = diff+c[j-1];
if(tempMax>maxProfit) maxProfit = tempMax;
}
return maxProfit;
}