题目:

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

解题思路:

前缀pre[i]处理 0 ~ i 买卖一次最优解,后缀suf[i]处理 i ~ prices.size() - 1 买卖一次最优解。
所有位置pre[i] + suf[i]最大值为答案O(n)。
处理最优解的时候是维护前(后)缀prices最小(大)值,与当前prices做差后和前(后)缀最优解比较取最优,O(n)。
总复杂度O(n)。

代码:

class Solution {
public:int maxProfit(vector<int> &prices) {int min_price = 0x3fffffff;vector<int> pre(prices.size()), suf(prices.size());for (int i = 0; i < prices.size(); i++) {min_price = min(min_price, prices[i]);pre[i] = max(prices[i] - min_price, i ? pre[i - 1] : 0);}int max_price = 0x80000000;for (int i = prices.size() - 1; i >= 0; i--) {max_price = max(max_price, prices[i]);suf[i] = max(max_price - prices[i], i < prices.size() - 2 ? suf[i + 1] : 0);}int ans = 0;for (int i = 0; i < prices.size(); i ++) {ans = max(ans, pre[i] + suf[i]);}return ans;}
};

转载于:https://www.cnblogs.com/dongguangqing/p/3727805.html

LeetCode OJ - Best Time to Buy and Sell Stock III相关推荐

  1. 【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 ...

  2. [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 ...

  3. LeetCode OJ - Best Time to Buy and Sell Stock II

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/33817395 Say you have ...

  4. leetcode 123. Best Time to Buy and Sell Stock III | 123. 买卖股票的最佳时机 III(总结DP 模型套路)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ DP 模型套路 DP 套路之:暴力递 ...

  5. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  6. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

  7. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

  8. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

  9. 【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV

    LeetCode 188. Best Time to Buy and Sell Stock IV Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyan ...

最新文章

  1. golang指针与c指针的异同
  2. python爬虫框架排行榜-8个最高效的Python爬虫框架,你用过几个?
  3. MapReduce的简单实例WordCount
  4. dot-- 资源汇总
  5. 通过在Global.asax文件中配置Application来统计的方法
  6. 中国双面柔性印刷电路板(FPC)市场趋势报告、技术动态创新及市场预测
  7. 《MapReduce 2.0源码分析与编程实战》一1.5 看,大象也会跳舞
  8. ElasticSearch + Logstash进行数据库同步
  9. 动态规划实战1-leetcode 983.Minimum Cost For Tickets
  10. 7-60 致命的珠宝 (10分)
  11. Nginx系列 (1)--Nginx安装升级打补丁
  12. Cmd命令检测电脑配置:
  13. 书评精益创业-新创企业的成长思维 (上)
  14. 仰望星空,脚踏实地——吴燕生
  15. 人工智能轨道交通行业周刊-第14期(2022.9.12-9.18)
  16. 字节跳动---雀魂启动
  17. Android系统开发 ----- 系统服务开发
  18. 想不到好的标题,所以决定摆烂。
  19. 网站推荐-极简壁纸网站
  20. 数值分析实验 实验3-1 牛顿下山法 python3实现

热门文章

  1. Mysql 索引(学习笔记十二)
  2. python进阶07 MySQL
  3. AMD第七代桌面APU揭秘:主流首次上DDR4
  4. NPOI读写Excel sheet操作
  5. Loadrunner报错“Too many local variablesAction.c”解决方法
  6. 从“做什么”到“怎么做”,说说一只蚊子
  7. vb.net与FLASH的完美结合
  8. mysql将最大金额加1000_Mysql数据库笔记
  9. varnish与squid比较
  10. 一键开启微信“开关头像”,快试试!