LeetCode OJ - 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, 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相关推荐
- 【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 ...
- [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 ...
- LeetCode OJ - Best Time to Buy and Sell Stock II
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/33817395 Say you have ...
- 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 套路之:暴力递 ...
- LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...
- 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/ 题解 经典的 暴力递归 - ...
- 【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 ...
- 【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 ...
- 【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 ...
最新文章
- golang指针与c指针的异同
- python爬虫框架排行榜-8个最高效的Python爬虫框架,你用过几个?
- MapReduce的简单实例WordCount
- dot-- 资源汇总
- 通过在Global.asax文件中配置Application来统计的方法
- 中国双面柔性印刷电路板(FPC)市场趋势报告、技术动态创新及市场预测
- 《MapReduce 2.0源码分析与编程实战》一1.5 看,大象也会跳舞
- ElasticSearch + Logstash进行数据库同步
- 动态规划实战1-leetcode 983.Minimum Cost For Tickets
- 7-60 致命的珠宝 (10分)
- Nginx系列 (1)--Nginx安装升级打补丁
- Cmd命令检测电脑配置:
- 书评精益创业-新创企业的成长思维 (上)
- 仰望星空,脚踏实地——吴燕生
- 人工智能轨道交通行业周刊-第14期(2022.9.12-9.18)
- 字节跳动---雀魂启动
- Android系统开发 ----- 系统服务开发
- 想不到好的标题,所以决定摆烂。
- 网站推荐-极简壁纸网站
- 数值分析实验 实验3-1 牛顿下山法 python3实现
热门文章
- Mysql 索引(学习笔记十二)
- python进阶07 MySQL
- AMD第七代桌面APU揭秘:主流首次上DDR4
- NPOI读写Excel sheet操作
- Loadrunner报错“Too many local variablesAction.c”解决方法
- 从“做什么”到“怎么做”,说说一只蚊子
- vb.net与FLASH的完美结合
- mysql将最大金额加1000_Mysql数据库笔记
- varnish与squid比较
- 一键开启微信“开关头像”,快试试!