原题地址

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

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

用迭代法求这个问题非常容易,令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. vc++ 内存连续读写操作

    //初始化内存 int *data=(int*)malloc(sizeof(int)*4); ZeroMemory(data, sizeof(int)*4); int *m=(int*)malloc( ...

  2. 点击后弧形展开的炫酷菜单--第三方开源-- CircularFloatingActionMenu(一)

    CircularFloatingActionMenu在github上项目主页地址:https://github.com/oguzbilgener/CircularFloatingActionMenu ...

  3. 限制<input>输入内容 只允许数字 或者 字母

    只能输入数字: 有回显 <input onkeyup="value=value.replace(/[^\d]/g,'')"> 只能输入数字:无回显 <input ...

  4. How to executing direct SQL statements [Axapta, AX4.0, AX2009, AX2012]

    Today I want to talk about executing SQL statements in X++ on both the current AX database and exter ...

  5. 配置github上的SSH key及上传自己的项目到github

    这篇文章比较好,链接如下:http://www.jianshu.com/p/b81eeb5d7858 需要指出的几点:1.

  6. 解决在sublime text3在ubuntu下无法输入中文的问题

    方法链接:https://github.com/lyfeyaj/sublime-text-imfix 效果图:

  7. salt-ssh安装及简单使用

    需要 salt-master 0.17以上版本支持 1.安装 相关依赖包可查看requirements.txt Jinja2 M2Crypto msgpack-python pycrypto PyYA ...

  8. Python脚本控制的WebDriver 常用操作 <九> 定位一组对象

    下面将使用WebDriver来模拟操作定位一组对象的操作 测试用例场景 从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需 ...

  9. linux C socket

    socket套接字和管道同样可以提供进程内通信.但套接字更胜一筹,不同的进程可以跨越不同的主机(说白了,支持网络通信).使用套接字的知名程序:telnet.rlogin.ftp等. 你需要知道的一些基 ...

  10. iframe 父子窗口相互之间调用语法

    一.父窗口调用iframe子窗口方法 1.HTML语法:<iframe name="myFrame" src="child.html"></i ...