Leetcode - 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).

 

[balabala] 自己的想法是先找出收益最大的一笔收益,然后从剩余区间中寻找次大的收益,但是在一些case上会失败,比如 [6,1,3,2,4,7]。为什么这种贪婪做法得不到全局最优解呢?如yb君指点,因为第一次交易确定会影响第二次交易的可选范围,子问题之间相互影响,所以不可用最优子问题叠加获取最优解。
正确的思路:至多能做两笔交易,而且交易之间没有重叠,想到可以用divide and conque. 如yb君说,需要综合使用多种算法的题目就会显得比较难。这题就是分治 + 动归 综合题。

 

public class Solution {
    // 以每一天为分界点,分别求出左边和右边能获得的最大收益,然后求出最大值
    // 分治 + 动归
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2)
            return 0;
        
        int[] left = new int[prices.length];
        int low = prices[0];
        int maxleft = 0;
        for (int i = 0; i < prices.length; i++) {
            left[i] = Math.max(maxleft, prices[i] - low);
            low = Math.min(low, prices[i]);
        }
        
        int[] right = new int[prices.length];
        int high = prices[prices.length - 1];
        int maxright = 0;
        for (int i = prices.length - 1; i >=0 ;i--) {
            right[i] = Math.max(maxright, high - prices[i]);
            high = Math.max(high, prices[i]); 
        }
        
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            maxprofit = Math.max(maxprofit, left[i] + right[i]);
        }
    }
    
    ////////////////////////////////下面为不正确思路的实现//////////////////////////////
    //fail @ [6,1,3,2,4,7]
    class ProfitInfo {
        int buyDay;
        int sellDay;
        int profit;
        public ProfitInfo(int buyDay, int sellDay, int profit) {
            this.buyDay = buyDay;
            this.sellDay = sellDay;
            this.profit = profit;
        }
    }
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2)
            return 0;
        ProfitInfo profit1 = maxProfit(prices, 0, prices.length - 1);
        ProfitInfo profit2 = maxProfit(prices, 0, profit1.buyDay - 1);
        ProfitInfo profit3 = maxProfit(prices, profit1.sellDay + 1, prices.length - 1);
        return profit1.profit + Math.max(profit2.profit, profit3.profit);
    }
    
    private ProfitInfo maxProfit(int[] prices, int start, int end) {
        if (start >= end) // length of range <= 1
            return new ProfitInfo(start, end, 0);
        int min = start;
        int buy = start;
        int profit = 0;
        int sell = start;
        for (int i = start + 1; i <= end; i++) {
            if (prices[i] < prices[min]) {
                min = i;
            } else if (prices[i] > prices[min]){
                int currProfit = prices[i] - prices[min];
                if (currProfit >= profit) {
                    profit = currProfit;
                    buy = min;
                    sell = i;
                }
            }
        }
        return new ProfitInfo(buy, sell, profit);
    }
    
    // fail @ [2, 4, 1] buy不能同时承担最小价格日以及当前最大profit的购买日,两者不一定是同一个值
    private ProfitInfo maxProfit(int[] prices, int start, int end) {
        if (start >= end) // length of range <= 1
            return new ProfitInfo(start, end, 0);
        int buy = start;
        int profit = 0;
        int sell = start;
        for (int i = start + 1; i <= end; i++) {
            if (prices[i] < prices[buy]) {
                buy = i;
            } else if (prices[i] > prices[buy]){
                int currProfit = prices[i] - prices[buy];
                if (currProfit >= profit) {
                    profit = currProfit;
                    sell = i;
                }
            }
        }
        return new ProfitInfo(buy, sell, profit);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值