leetcode第一刷_Best Time to Buy and Sell Stock III

这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类。只是相比之下稚嫩多了。有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律。这道题的情况还是比較少的,要么买卖了两次。要么一次。

买卖一次的情况,已经解决过了,如今分析买卖两次的情况。

两次买卖之间是没有交叉的,即下一次买之前一定已经卖掉了。最easy想到,穷去分点,每一个部分都依照买卖一次的方法做。

好,恭喜。你已经走在超时的路上了。那么怎么办呢。有没有一种方法,在线性时间内,等我走到一个分点的时候,就能知道划分两次求出来的结果?

没错。辅助空间。先从头往后扫一遍,记录下以这个点为终点时的最大收入,然后从尾向头扫一遍。记录下以这个点为起点的最大收入。

再对每一个分点扫一遍,看看已他为起点终点算出来的交易收入是不是最优的。

思路是这种。描写叙述起来比較清晰。实现的时候还是有能够优化的地方的,比如你发现从尾向头扫描的结果根本不用一整个数组。仅仅要保存好后一个位置的最优值就能够了。由于当前最优值仅仅跟峰值与当前值以及过去最优相关。

代码例如以下。未优化空间:

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int len = prices.size();
        if(len <= 1)
            return 0;
        vector<int> phistory(len);
        vector<int> pfuture(len);
        int valley = prices[0], peak = prices[len-1];
        for(int i=1;i<len;i++){
            valley = min(valley, prices[i]);
            phistory[i] = max(phistory[i-1], prices[i]-valley);
        }
        int res = 0;
        for(int i=len-2;i>=0;i--){
            peak = max(peak, prices[i]);
            pfuture[i] = max(pfuture[i+1], peak-prices[i]);
            res = max(res, pfuture[i]+phistory[i]);
        }
        return res;
    }
};
时间: 2024-11-18 13:17:48

leetcode第一刷_Best Time to Buy and Sell Stock III的相关文章

leetcode第一刷_Best Time to Buy and Sell Stock II

sched.c sched.h 代码分析笔记 首先上header file sched.h #ifndef _SCHED_H #define _SCHED_H #define HZ 100 #define NR_TASKS 64 #define TASK_SIZE 0x04000000 #define LIBRARY_SIZE 0x00400000 #if (TASK_SIZE & 0x3fffff) #error "TASK_SIZE must be multiple of 4M&qu

leetcode第一刷_Best Time to Buy and Sell Stock

这样的题就不要去考虑N^2的算法了.肯定会超时的.乍一看,非常可能会想到贪心,可是普通的贪心思路是不行的,比方想找到一个最小值用来买入.尽管它跟最大值之间的差一定是最好的,可是最大值出如今它前面就不行了,找出如今它后面的最大值,仅仅只是是一个局部最优,非常可能前面的最大和次小比他们要好. 所以呢,贪心找的不是两个点,而是差.这种话.每一步都维护一个最小值,然后算跟当前最小之间的差来更新最后的结果. 代码简洁,不在赘述. class Solution { public: int maxProfit

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

一. 题目描述 Say you have an array for which the i-th 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

leetcode || 123、Best Time to Buy and Sell Stock III

problem: 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

[leetcode]_Best Time to Buy and Sell Stock I &amp;&amp; II

一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换.要求只买卖一次,获得最大收益. 思路:一开始我认为是寻找最大.最小值,但由于最大值不一定总是出现在最小值的后面,因此WA. 参考思路:DP.对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比. 代码: 1 public int max

【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,

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