天题系列:Best Time to Buy and Sell Stock III

public class Solution {
    public int maxProfit(List<Integer> prices) {
        // http://blog.csdn.net/linhuanmars/article/details/23236995
        if(prices==null||prices.size()<1) return 0;
        int len = prices.size();
        int[] global = new int[3] ;
        int[] loc = new int[3] ;
        for(int i=1;i<len;i++){ // 算每天都进行交易,进行完k次交易,的可能性
            int dif = prices.get(i)-prices.get(i-1);
            for(int j=2;j>0;j--){ //倒着进行是因为i的loop里顺序的把global和loc的值都排好了,所以要update的话需要用到i-1,j-1这样的数据,为了避免mess,从后update
                loc[j] = Math.max(global[j-1]+ (dif>0?dif:0), loc[j]+dif); // global可以加0 的缘故是,在最后一天买进,卖出,不做赔本生意了
                global[j] = Math.max(loc[j],global[j]);
            }
        }
        return global[2];
    }
}

ref 尤其是关于为啥第二层loop要倒着走的解释

http://blog.csdn.net/linhuanmars/article/details/23236995

时间: 2024-10-11 06:48:48

天题系列:Best Time to Buy and Sell Stock III的相关文章

【leetcode刷题笔记】Best Time to Buy and Sell Stock III

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,

123. Best Time to Buy and Sell Stock III

/* * 123. Best Time to Buy and Sell Stock III * 2016-5-21 by Mingyang * 根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作. * 所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出. * 进而,我们可以划分为2个区间[0,i]和[i,len-1],i可以取0~len-1. * 那么两次买卖的最大利润为:在两个区间的最大利益和的最大利润. * 一次划分的最大利益为:Prof

LeetCode: Best Time to Buy and Sell Stock III 解题报告

Best Time to Buy and Sell Stock IIIQuestion SolutionSay 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 en

LeetCode——Best Time to Buy and Sell Stock III

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,

Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III 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. c++版本代码: class Solution { public: i

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

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,

【leetcode】Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III 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

LeetCode 笔记23 Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III 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. 现在A股涨这么好,要是股票都提前知道价格就好了(@[email pro

LeetCode: Best Time to Buy and Sell Stock III [123]

[题目] 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

LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

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,