Leetcode#123 Best Time to Buy and Sell Stock III
最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润
在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖出,当然,卖股票要在买股票之后。
用迭代法求这个问题非常容易,令profits[i]表示截至到第i天的最大利润,则profits[i+1] = max{profits[i], prices[i+1] - minPrice},其中,minPrice = min{prices[k]},0 <= k < n。但是从profits[i]没法直接得到profits[i-1]
倒过来迭代也完全没问题,令profits[i]表示从第i天开始到结束的最大利润,则profits[i] = max{profits[i+1], maxPrice - prices[i]},其中maxPrice = max{prices[k]},i+1 <= k < n。但是从profits[i]没法直接得到profits[i+1]
之后,依次枚举分割点,求解两个子问题的和即可。
代码:
int maxProfit(vector<int> &prices) {
int n = prices.size();
vector<int> profits(prices.size(), );
int result = ;
int tmp;
if (n < )
return ;
tmp = ;
int minPrice = prices[];
for (int i = ; i < n; i++) {
tmp = max(tmp, prices[i] - minPrice);
profits[i] = tmp;
minPrice = min(minPrice, prices[i]);
}
tmp = ;
int maxPrice = prices[n - ];
for (int i = n - ; i >= ; i--) {
tmp = max(tmp, maxPrice - prices[i]);
result = max(result, i > ? tmp + profits[i - ] : tmp);
maxPrice = max(maxPrice, prices[i]);
}
return result;
}
Leetcode#123 Best Time to Buy and Sell Stock III的更多相关文章
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [leetcode]123. 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 al ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 123. Best Time to Buy and Sell Stock III ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 【刷题-LeetCode】123 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 ...
- 【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 ...
随机推荐
- Centos6.5环境下安装SVN 整合Apache+SSL
弄了两天,终于在服务器上初步搭建起来了SVN(版本1.8). 服务器系统:Centos6.5 64位,搭建过程中全部采用源码编译安装(configure/make/make install),推荐大家 ...
- Eclipse插件推荐:UCDetector: Unnecessary Code Detector
正如其名,检查不必要的代码. 下载地址为:http://sourceforge.net/projects/ucdetector/files/latest/download?source=files 官 ...
- Windows 上如何安装Sqlite(转载)
1.获得命令行程序 SQLite命令行程序(CLP)是开始使用SQLite的最好选择,按照如下步骤获取CLP: 1).打开浏览器进入SQLite主页, www.sqlite.org. 2).单击页 ...
- python中的remove趣谈
首先我们要知道remove做的操作是顺序遍历list表,找到第一个匹配的项时删掉该项,并不会再往下找,那我们看下面的代码 mylist = [1,2,3] for i in mylist: print ...
- mini2440裸机之MMU(二)(mmu.c) (转)
分类: 嵌入式 http://blog.chinaunix.net/uid-26435987-id-3082166.html(转) /********************************* ...
- 第二十章 数据访问(In .net4.5) 之 使用LINQ
1. 概述 .net3.5中新添加给C#的LINQ查询,提供了直观便捷的数据查询方式.并且支持多种数据源的查询. 本章介绍标准的LINQ操作,如何用最优的方式使用LINQ 以及 LINQ to XML ...
- MongoDB 学习笔记(二)—— MongoDB Shell
MongoDB自带一个JavaScript shell 可以从命令行中与MongoDB交互,功能非常强大.如在上一节最后一张图所看到,可以执行JavaScript程序. 运行Shell 前提是启动Mo ...
- HelloWorld IL代码
.assembly extern mscorlib .assembly HelloWorld.class HelloWorld extends [mscorlib] System.Object { ...
- SQL Server 一些关键字详解(二)
1.LEFT JOIN 容易让人误解的地方 背景:因为在网上搜了下 LEFT JOIN 和 OUTER APPLY 的区别,时发现,有的网友解释为: 1) A left join B 的连接 ...
- Postgresql命令行和数据库备份与恢复
进入Postgresql 1 进入数据库 默认安装会创建postgres 用户,,使用postgres用户,psql命令会直接进入数据库: Bash代码 $ su postgres ...