原题地址

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

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

用迭代法求这个问题非常容易,令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. jMeter 监控cpu、内存

    http://jmeter-plugins.org/downloads/all/ 将JMeterPlugins.jar包复制到Jmeter的lib目录下面的ext目录下面,重新启动Jmeter,我们点 ...

  2. JS调用腾讯接口获取天气

    想做个直接通过JS获取某个城市的天气.本来想通过直接调用中国气象网的接口: http://www.weather.com.cn/weather/101070201.shtml,但是跨域问题一直无法解决 ...

  3. ContactsContract.CommonDataKinds【Translated By KillerLegend】

    http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.html interf ...

  4. ContactsContract中涉及数据库中的一些列属性值【Written By KillerLegend】

    ContactsContract.Data   http://developer.android.com/reference/android/provider/ContactsContract.Dat ...

  5. DevExpress 重编译 替换强命名 修改源码

    本文以DevExpress 11.1.8举例 必须满足几个条件 1. 必须有DXperience相应版本的全部源代码SourceCode.把全部源代码复制到X:\Program Files\DevEx ...

  6. Ztack学习笔记(4)-系统网络分析

    协调器的组网,终端设备和路由设备发现网络以及加入网络 //第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统 ...

  7. C# ArrayList的用法总结

    C# ArrayList的用法总结 System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度. 一.优点 1. 支持自动改变大小的功能 ...

  8. 部分手机不能连PC adb

    http://www.th7.cn/Program/java/201407/232139.shtml 1. 命令行中执行 android update adb [这一步的目的是产生下面第二步的路径和文 ...

  9. bootstrap bootstrapTable 分页 传值问题

    bootstrapTable 分页传值 配置项:将原始的 limit: params.limit, //页面大小  page: params.offset, //页码 改成 limit: params ...

  10. [磁盘管理与分区]——关于分区、磁盘分区表、MBR

    磁盘连接与设备文件名的关系 1. 如下图所示: