原题地址

最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润

在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖出,当然,卖股票要在买股票之后。

用迭代法求这个问题非常容易,令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的更多相关文章

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

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

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

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

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

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

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

  8. 【刷题-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 ...

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

随机推荐

  1. mariadb一些命令介绍及mariadb架构图和索引

    mariadb> SHOW GLOBAL VARIABLES; 全局变量影响服务器的全局操作 mariadb> SHOW [SESSION] VARIABLES; 客户端变量,只对当前会话 ...

  2. C语言-sizeof()与strlen()的区别【转】

    先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...

  3. c#写入Mysql中文显示乱码 解决方法

    如题,mysql字符集utf8,c#写入中文后,全部显示成?,一个汉字对应一个?解决方法:在数据库连接字符串中增加字符集的说明,Charset=utf8,如 MySQLConnection con = ...

  4. 使用WIF实现单点登录Part III —— 正式实战

    我们接下来的demo将包括以下的工程: SiteA —— 基于.net framework 4.5的MVC 4程序,使用WIF 4.5的SDK,第一个RP SiteB —— 基于.net framew ...

  5. 使用WIF实现单点登录Part I——Windows Identity Foundation介绍及环境搭建

    首先先说一下什么是WIF(Windows Identity Foundation).由于各种历史原因,身份验证和标识的管理一般都比较无规律可循.在软件里加入“身份验证”功能意味着要在你的代码里混进处理 ...

  6. 从一个标准URL中提取文件的扩展名

    例如:http://www.sina.cn/abc/de.php?id=1  提出php 1. $url = 'http://www.sina.cn/abc/de.php?id=1'; $arr = ...

  7. 微信分享朋友链接显示js代码

    通常自己做的一个页面想通过微信像朋友分享时,展示的标题和描述都是不是自己想要的,自己查了一些资料,原来是通过js来进行控制 展示效果如下: 标题.描述.还有分享的图片都是有js来控制的. js代码如下 ...

  8. Drawable

    今天简单的介绍一下有关以下5中的应用: Statelistdrawable Layerdrawable Shapeddrawable Clipdrawable Animationdrawable 1. ...

  9. Android--从相册中选取照片并返回结果

    启动系统相册去选择图片 //从相册中选取的方法 private void selectPhoto(){ Intent intent = new Intent(Intent.ACTION_PICK); ...

  10. c++基础(一):数据类型和结构

    1.map map<int, int> rankDict;//定义map rankDict[1] = 5; rankDict[2] = 6;//map赋值 int dictSize = r ...