LeetCode(123) Best Time to Buy and Sell Stock III (Java)

题目如下:

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.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).


分析如下:

这里


我的代码:

第一次错误的代码如下,错在没有记录历史的最大值信息。

/*
Input:	  [2,1,2,0,1]
Output:	  1
Expected: 2
*/
public class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length == 0) {
            return 0;
        }
        
        int[] leftProfit = new int [prices.length];
        int[] rightProfit = new int [prices.length];
        
        int minPrice = prices[0];
        for (int i = 0; i < prices.length; ++i) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            }
            leftProfit[i]= prices[i] - minPrice;
        }
        
        int maxPrice = prices[prices.length - 1];
        for (int i = prices.length - 1; i >= 0; --i) {
            if (prices[i] > maxPrice) {
                maxPrice = prices[i];
            }
            rightProfit[i] = maxPrice - prices[i];
        }
    
        // int final = 0 // java中算错误,是保留的关键字
        int finalResult = 0;
        for (int i = 0; i < prices.length; ++i) {
            int result = leftProfit[i] + rightProfit[i];
            if (result > finalResult) {
                finalResult = result;
            }
        }
        return finalResult;
    }
}


修改后如下, 

/*
Input:	[2,1,2,0,1]
Output:	1
Expected:	2
*/
//350ms
public class Solution {
	public int maxProfit(int[] prices) {
        if(prices.length == 0) {
            return 0;
        }
        
        int[] leftProfit = new int [prices.length];
        int[] rightProfit = new int [prices.length];
        
        int minPrice = prices[0];
        for (int i = 0; i < prices.length; ++i) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            }
            leftProfit[i]= prices[i] - minPrice;
            // BUG1: leftProfit是记录从index = 0 ~ index = i中的所有可能情况的最大值,
            // 即,index = i时候的leftProit由之前的index算出来,而不是当前的index算出来。
            // 所以这里还要更新leftProfit数组。
            if (i != 0 && leftProfit[i] < leftProfit[i - 1]) {
            	leftProfit[i] = leftProfit[i - 1];
            }
        }
        	
        int maxPrice = prices[prices.length - 1];
        for (int i = prices.length - 1; i >= 0; --i) {
            if (prices[i] > maxPrice) {
                maxPrice = prices[i];
            }
            rightProfit[i] = maxPrice - prices[i];
            // BUG2: 同BUG1
            if (i != prices.length - 1 && rightProfit[i] < rightProfit[i + 1] ) {
            	rightProfit[i] = rightProfit[i + 1];
            }
        }

        // int final = 0 // java中使用final作为变量名字是错误的,这是保留的关键字
        int finalResult = 0;
        for (int i = 0; i < prices.length; ++i) {
            int result = leftProfit[i] + rightProfit[i];
            if (result > finalResult) {
                finalResult = result;
            }
        }

        return finalResult;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值