leetcode-122-买卖股票的最佳时机 II-java

题目及测试用例

package pid122;
//买卖股票的最佳时机 II
//
//给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
//
//设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
//
//注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
//
//示例 1:
//
//输入: [7,1,5,3,6,4]
//输出: 7
//解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
//     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
//
//示例 2:
//
//输入: [1,2,3,4,5]
//输出: 4
//解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
//     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
//     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
//
//示例 3:
//
//输入: [7,6,4,3,1]
//输出: 0
//解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。


public class main {
	
	public static void main(String[] args) {
		int[][] testTable = {{7,1,5,3,6,4},{1,2,3,4,5},{7,6,4,3,1},{1,6,2,7}};
		for (int[] ito : testTable) {
			test(ito);
		}
	}
		 
	private static void test(int[] ito) {
		Solution solution = new Solution();
		int rtn;
		long begin = System.currentTimeMillis();
		for (int i = 0; i < ito.length; i++) {
		    System.out.print(ito[i]+" ");		    
		}
		System.out.println();
		//开始时打印数组
		
		rtn = solution.maxProfit(ito);//执行程序
		long end = System.currentTimeMillis();	
		
		//System.out.println(ito + ": rtn=" + rtn);
		System.out.println(ito + ": rtn=" +rtn);
//		for (int i = 0; i < ito.length; i++) {
//		    System.out.print(ito[i]+" ");
//		}//打印结果几数组
		
		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}

}

解答1(成功,7ms,很慢)

package pid122;

public class Solution {
public int maxProfit(int[] prices) {
    int buy=0;
    int sell=0;
    int next;
    int now;
    int length=prices.length;
    boolean wantBuy=true;
    int profit;
    int sum=0;
    
    if(length==0||length==1){
         return 0; //异常情况
    }
    
    for(int i=0;i<length-1;i++){ //从第二个价格开始        
        now=prices[i];
        next=prices[i+1];
        if(wantBuy==true){
        	if(now<next){
        		buy=now;
        		wantBuy=false;
        	}          	
        }
        else{
        	if(now>next){
        		sell=now;
        		profit=sell-buy;
        		sum=sum+profit;
        		wantBuy=true;
        		//System.out.println("buy="+buy+"  sell="+sell+"  profit="+profit);
        	}
        }      	
     }
     if(wantBuy==false){
    	sell=prices[length-1];
 		profit=sell-buy;
 		sum=sum+profit;
 		wantBuy=true;
 		System.out.println("buy="+buy+"  sell="+sell+"  profit="+profit);     	
    }
    
     return sum;  
        
    
    }

}

解答2(成功,2ms,很快,优化过的代码,附带思路)

package pid122;

public class Solution {
/*	算法思想1:
    基本思想为贪婪算法,在每个阶段得到最大的利润,得到每个上升坡道的利润
	设置标志,是否要买,初始为true
	从第一个循环到最后一个
	标志为true,要买
	如果这个比后面的那个小,则买入
	标志为false,要卖
	如果这个比后面的大,则卖出
*/	
public int maxProfit(int[] prices) {
    int buy=0;
    int sell=0;
    int next;
    int now;
    int length=prices.length;
    boolean wantBuy=true;
    int profit;
    int sum=0;
    
    if(length==0||length==1){
         return 0; //异常情况
    }
    next=prices[0];
    for(int i=0;i<length-1;i++){ //从第二个价格开始        
        now=next;
        next=prices[i+1];
        if(wantBuy==true){
        	if(now<next){
        		buy=now;
        		wantBuy=false;
        	}          	
        }
        else{
        	if(now>next){
        		sell=now;
        		profit=sell-buy;
        		sum=sum+profit;
        		wantBuy=true;
        		//System.out.println("buy="+buy+"  sell="+sell+"  profit="+profit);
        	}
        }      	
     }
     if(wantBuy==false){
    	sell=next;
 		profit=sell-buy;
 		sum=sum+profit;
 		wantBuy=true;
 		//System.out.println("buy="+buy+"  sell="+sell+"  profit="+profit);     	
    }
    
     return sum;  
        
    
    }

}

解答3(成功,2ms,极快)
具体思路为设置prev为prices【0】 之后从i从1到length-1,now为对应数字,如果now比prev大,就直接加上now-prev(将天与天所有的利润都得到),否则就不加,然后重置prev
我们可以简单地继续在斜坡上爬升并持续增加从连续交易中获得的利润,而不是在谷之后寻找每个峰值。最后,我们将有效地使用峰值和谷值,但我们不需要跟踪峰值和谷值对应的成本以及最大利润,但我们可以直接继续增加加数组的连续数字之间的差值,如果第二个数字大于第一个数字,我们获得的总和将是最大利润。这种方法将简化解决方案。 这个例子可以更清楚地展现上述情况:
在这里插入图片描述
从上图中,我们可以观察到 A+B+C 的和等于差值 D 所对应的连续峰和谷的高度之差。

	public int maxProfit(int[] prices) {
	    int length=prices.length;
	    if(length==0||length==1){
	    	return 0;
	    }
	    int prev=prices[0];
	    int sum=0;
	    for(int i=1;i<length;i++){
	    	int now=prices[i];
	    	if(now>prev){
	    		sum+=now-prev;
	    	}
	    	prev=now;   	   	
	    }
	    return sum;
    }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值