先说思路。参考了这篇:http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-and-sell-stock-iii%EF%BC%8C%E4%B8%80%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/

我们可以看到这个相当于要分两段,取前一段和后一段的分别最大值。这样分割点遍历,最后是O(n^2)的复杂度。

然后优化后,使用一维的DP。可以降低到O(n)。也就是先从左到右扫一遍求得以i结尾的最大利润的数组,然后从右到左扫一遍求得以i开头的最大利润的数组。然后一加就可以了。

public class Solution {public int maxProfit(int[] prices) {// Start typing your Java solution below// DO NOT write main() functionif (prices.length == 0 || prices.length == 1) return 0;int ans = 0;int max = 0;int len = prices.length;int historyProfit[] = new int[len];int futureProfit[] = new int[len];int valley  = prices[0];for (int i = 1; i < len; i++) {if (prices[i] >= prices[i-1]) {int val = prices[i] - valley ;if (val > max) max = val;             }else {if (prices[i] < valley ) valley  = prices[i];}historyProfit[i] = max;}       int peek = prices[len - 1];ans = max;max = 0;for (int i = len - 2; i >= 1; i--) {if (prices[i+1] >= prices[i]) {int val = peek - prices[i];if (val > max) max = val;}else {if (prices[i] > peek) peek = prices[i];}futureProfit[i] = max;int p = futureProfit[i] + historyProfit[i-1];if (p > ans) {ans = p;}}return ans;}
}

我的代码比较乱,看看参考代码多么简洁。同时,该讨论第二楼里还有关于更一般情况的讨论,有空好好看看。

class Solution {
public:int maxProfit(vector<int> &prices) {// null checkint len = prices.size();if (len==0) return 0;vector<int> historyProfit;vector<int> futureProfit;historyProfit.assign(len,0);futureProfit.assign(len,0);int valley = prices[0];int peak = prices[len-1];int maxProfit = 0;// forward, calculate max profit until this timefor (int i = 0; i<len; ++i){valley = min(valley,prices[i]);if(i>0){historyProfit[i]=max(historyProfit[i-1],prices[i]-valley);}}// backward, calculate max profit from now, and the sum with historyfor (int i = len-1; i>=0; --i){peak = max(peak, prices[i]);if (i<len-1){futureProfit[i]=max(futureProfit[i+1],peak-prices[i]);}maxProfit = max(maxProfit,historyProfit[i]+futureProfit[i]);}return maxProfit;}
};

  

转载于:https://www.cnblogs.com/lautsie/p/3239029.html

[leetcode]Best Time to Buy and Sell Stock III相关推荐

  1. Leetcode Best Time to Buy and Sell Stock III

    Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...

  2. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III

    LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...

  4. leetcode: Best Time to Buy and Sell Stock 系列

    leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...

  5. Leetcode Best Time to Buy and Sell Stock

    Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...

  6. LeetCode Best Time to Buy and Sell Stock II

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...

  7. [LeetCOde][Java] 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 a ...

  8. [leetcode]_Best Time to Buy and Sell Stock I II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  9. Best Time to Buy and Sell Stock III

    解题思路来自:https://blog.csdn.net/u012501459/article/details/46514309 Say you have an array for which the ...

最新文章

  1. css规则_CSS规则,将使您的生活更轻松
  2. 手机壳释放致癌物质“苯”吗?专家:确实会
  3. [译]ElasticSearch数据类型--string类型已死, 字符串数据永生
  4. 保定理工学院专科计算机专业分数线,保定理工学院历年分数线 2021保定理工学院录取分数线...
  5. 雷达多普勒频率计算公式_雷达系统的用途、分类,影响雷达性能的关键要素
  6. 安卓imageView加载MYSQL图片_Android调用相机拍摄照片并显示到 ImageView控件中
  7. mybatis更新Blob类型字段要用updateByPrimaryKeyWithBLOBs
  8. 几种排序与最大K问题
  9. 【00】why集搜客网络爬虫?
  10. MapReduce InputFormat之FileInputFormat
  11. 数据结构_C语言_实验二_树 ——还原二叉树
  12. Linux 下文件IO编程进程控制实验
  13. JVM内存模型和性能调优:JVM整体结构及内存模型- 第10篇
  14. 七张图总结了我的2021年,心依然热,情依然真----感谢2021年的自己,感谢CSDN
  15. handsontable的核心方法
  16. Nero 7 Premium Reloaded 7.8.5.0
  17. js函数 argements参数
  18. 2021-2027全球与中国半导体封装基板(IC载板)市场现状及未来发展趋势
  19. ubuntu 8.04 配置okl4编译环境
  20. 微信小程序--菜鸡队初步构思

热门文章

  1. Flask cookie API(response.set_cookie,request.cookies)
  2. 编程语言对比 主程序传参
  3. 软件测试测试用例编写 不超过7步骤_教你快速编写一个合格的测试用例!
  4. vSphere 7 Kubernetes 初体验
  5. 截止2020年06月06日证书获得
  6. oracle中长字符串长度吗,Oracle 字符串长度函数
  7. PHP中的pack和unpack函数
  8. 运行时动态的开关 Spring Security
  9. “安防+人工智能”,安企选择哪种抱大腿姿势才有出息?
  10. Kerberos 协议和 KDC 实现 Apache Kerby