[leetcode]Best Time to Buy and Sell Stock

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]范围内的价格最低的那一天买入最合适。

那我们

  1. 初始化最大利润maxProfit = 0;
  2. 遍历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]范围内的最小值

则有

  1. 初始c[0] = 0;
  2. 当prices[i]<min时: 即当前值比前面的最小值还要小,则c[i] = c[i-1];并且把min赋值为prices[i];
  3. 当prices[i]>=min时: c[i] = max{c[i-1],prices[i]-min};
  4. 我们要求的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[]的所有单调序列的头(最低点)买入,尾(最高点)卖出;

 

然后就想有没有可能不按照上述规则算出的利润更大呢?思考了下,发现不可能

  1. 对于单调序列内部的买入卖出不难思考 ,肯定是头买入,尾卖出最高。
  2. 那考虑跨越单调序列的买入卖出:
  3.  假设[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;   
	    }

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值