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 ...
随机推荐
- PHP数组操作大全
<?php /** * File: phpstudy : array_test.php * Created by PhpStorm. * User: IhMfLy Pheonix@jtv-070 ...
- Hbase之shell操作
一. 介绍 HBase是一个分布式的.面向列的 开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源 ...
- 09-排序3 Insertion or Heap Sort
和前一题差不多,把归并排序换成了堆排序.要点还是每一次排序进行判断 开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误...一直在检查逻辑 建立最大堆 排 ...
- SegmentFault 2014黑客马拉松 北京 作品demo
1号作品展示——最熟悉的陌生人 app 利用录音(声纹识别)和照片来让好久不见的见面变得不那么尴尬. 2号作品展示——神奇魔镜 app 灵感来自通话<白雪公主>,穿越到今天的“魔镜”功能依 ...
- wpf的datepicker处理
如果有2个datepicker,控制时间起和止的话,可以把第二个datepicker加一个属性,DisplayDateStart = "{Binding SelectedDate,Eleme ...
- linux命令行下的ftp 多文件下载和目录下载(转)
目标ftp服务器是一个非标准端口的ftp 1.通过shell登录 #ftp //shell下输入ftp命令,进入到ftp提示符 >open IP PORT //IP ,PORT对 ...
- 软件工程个人作业4(课堂练习&&课堂作业)
题目:返回一个整数数组中最大子数组的和. 要求:1.输入一个整型数组,数组里有正书和负数. 2.数组中连续的一个或者多个整数组,每个子数组都有一个和. 3.求所有子数组的和的最大值.要求时间复杂度为0 ...
- PHP(一)
最近一段时间一直忙于新版本的开发工作,所以虽然自己脑中有一些想法,但是苦于没有足够的时间去写下来.好了,昨天终于将大体的功能开发完成,时间上面也不会那么的紧张了.下来我想要好好的梳理一下,自己最近一段 ...
- libmemcached upcoming ISO C++ standard, C++0x
在编译我的小程序的时候,触发了一个编译错误,程序中使用了libmemcached,错误如下: 1 2 3 4 5 6 7 8 9 In file included from /usr/lib/gcc/ ...
- extjs 表格可复制
在GridPanel的配置项中,加入这个配置就可以了: viewConfig:{ enableTextSelection:true }