Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构。可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列为right,整个串为whole,可以推出当前串的最大值一定是在:a.左序列一次transaction最大值(left_max)与右序列一次transaction最大值(right_max)之和,b.整个序列一次交易最大值;两者之间产生。有一种情况.

class Solution {
public:int maxProfit(vector<int>& prices) {int max = 0;findMax(prices, max, 0, prices.size() - 1);return max;}int findMax(vector<int>& prices, int& max, int start, int end) {if (start >= end) {return 0;}int begin = start;int cur_max = 0;int left_max = 0;int right_max = 0;int min_pos = -1;int pre = start;int descent_count = 0;int backward = 0;for (int i = start + 1; i <= end; i ++) {// get the consective descent element numif (prices[i] <= prices[pre]) {descent_count ++;} else {descent_count = 0;}// get the minest elementif ((prices[i] <= prices[min_pos] || min_pos == -1) &&prices[i] < prices[pre]) {min_pos = i;backward = descent_count;}// calculate the max profite by one transactionif (prices[i] < prices[begin]) {begin = i;} else {int tmp = prices[i] - prices[begin];cur_max = tmp > cur_max? tmp : cur_max;}pre = i;}// calculate the left and the rightif (min_pos != -1) {left_max = findMax(prices, max, start, min_pos - backward);right_max = findMax(prices, max, min_pos, end);}// get the current maxif (cur_max > max) {max = cur_max;}if (left_max + right_max > max) {max = left_max + right_max;}return cur_max;}
};
test: ./a.out 3 2 4 2 5 7 2 4 9 0
re: 12

Leetcode Best Time to Buy and Sell Stock III相关推荐

  1. [leetcode]Best Time to Buy and Sell Stock III

    先说思路.参考了这篇:http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-a ...

  2. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  3. 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III

    LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...

  4. leetcode: Best Time to Buy and Sell Stock 系列

    leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...

  5. Leetcode Best Time to Buy and Sell Stock

    Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...

  6. LeetCode Best Time to Buy and Sell Stock II

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...

  7. [LeetCOde][Java] 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 a ...

  8. [leetcode]_Best Time to Buy and Sell Stock I II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  9. Best Time to Buy and Sell Stock III

    解题思路来自:https://blog.csdn.net/u012501459/article/details/46514309 Say you have an array for which the ...

最新文章

  1. linux下的vi与vim
  2. 向vSphere迁移虚拟机,Converter需要升级至4.0.1
  3. ACR2010_MTX单药治疗临床疗效良好但放射学进展的早期RA患者的预测因素和临床意义...
  4. 有线节点与无线节点的混合仿真模拟实验
  5. 搜索引擎重复网页发现技术分析
  6. 一个强大的图表库 -- ECharts
  7. 加拿大上市公司Vinergy更新投资政策 将涉及比特币数字货币等投资
  8. 绑定图片路径处理img 或asp:image
  9. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_3_转换流的原理...
  10. hdu 1026【Ignatius and the Princess I】
  11. 报道|香港科大校友“盐马行”活动成功举办
  12. Win10安装软件弹出:当前无法访问>SmartScreen
  13. 爱情大数据 | 你的专属微信聊天记录统计
  14. c语言 有15个数按由大到小,C++ 有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出“无此数”。...
  15. 分布式文件系统FastDFS集群搭建
  16. EXE4J_JAVA_HOME错误
  17. Google SEM和谷歌SEO的区别
  18. mysql复制多行_mysql中的复制方式总结(半同步复制,并行复制,多源复制)
  19. 跟踪误差分析中的实际位置计算
  20. OpenGL特点及功能

热门文章

  1. Grafana如何导出自己做的报表数据
  2. 网站超过一个月一直不收录怎么办?亲测有效(百度和360)
  3. git删除远程文件夹或文件
  4. 计算机演示文稿方案文稿,全国计算机等级考试二级officeppt演示文稿题目
  5. 主板、科创板、创业板、北交所IPO中介机构收费排名(1-3月份)
  6. 关于i386和amd64的详细解释
  7. TwoThink目录结构-基于ThinkPHP和OneThink
  8. 哈夫曼树以及哈夫曼算法
  9. PS笔刷:带连接线粒子抽象图形
  10. php中fmod什么意思,PHP fmod()用法及代码示例