leetcode的题目:http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

leetcode的题目都很简练,但是很有趣,认真地做吧。

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

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

a. 对于输入为n,最多可以买卖两次,求最大的盈利。

b. 买两次的收益通常比买一次来的大。

c. 对于输入规模为n,当n>=4时可以将n分为两部分,且分类的方法有n-3种。每份的最少个数为2。因为n个元素,总共有n-1条缝隙可以划分,又最左或最右至少要有两个元素,所以n-1-2 = n-3 种。

d. 求得划分一次(买卖两次)的每种划分的最大盈利,和不划分的最大盈利,即得到最大的盈利。

  max_profit = max{买卖两次(划分一次),买卖一次(不进行划分)}

e. 定义状态矩阵dp_be[i], 表示以0为起点,i为终点的子串的最大盈利。

   dp_be[0] = 0;int i, max_profit = 0, cur_min = prices[0];for(i = 1; i < len; i++) {max_profit = max(max_profit, prices[i]-cur_min);cur_min = min(cur_min, prices[i]);dp_be[i] = max_profit; // 在i时卖出的最大收益}

f. 定义状态矩阵dp_af[i],表示以i为起点,n-1(最后元素)为终点的子串的最大盈利

 max_profit = 0; int cur_max = prices[len-1];for(i = len-2; i >= 0; i--) {max_profit = max(max_profit, cur_max-prices[i]);cur_max = max(cur_max, prices[i]);dp_af[i] = max_profit;}

g. 由此可得,对于以i为划分点,将n个元素分成两个子串,要求两个子串之和最大,根据贪心算法思想,要求每个子串都去的最大值。对i从1到n-3循环得到最大值。

     // 放入只买一次的最大值int max = dp_be[len-1];// 在同一天卖了再买是没有意义的// 求两个数组和的最大值for(i = 1; i < len-1; i++) {if(dp_be[i]+dp_af[i+1] > max)max = dp_be[i]+dp_af[i+1];}

主要:dp_be[i]表示0开始,i结束的子串的最大盈利,所以它对应的尾部子串为dp_af[i+1],表示以i+1开始,n-1为终点的子串的最大盈利。

h. 代码

int Solution::maxProfit2(vector<int> &prices) {int len = prices.size();if(len < 2)return 0;int *dp_be = new int[len]; // 记录前向最大int *dp_af = new int[len]; // 记录后向最大dp_be[0] = 0;int i, max_profit = 0, cur_min = prices[0];for(i = 1; i < len; i++) {max_profit = max(max_profit, prices[i]-cur_min);cur_min = min(cur_min, prices[i]);dp_be[i] = max_profit; // 在i时卖出的最大收益}dp_af[len-1] = 0;max_profit = 0; int cur_max = prices[len-1];for(i = len-3; i >= 0; i--) {max_profit = max(max_profit, cur_max-prices[i]);cur_max = max(cur_max, prices[i]);dp_af[i] = max_profit;}if(len == 2)return dp_be[len-1];else {// 放入只买一次的最大值int max = dp_be[len-1];// 在同一天卖了再买是没有意义的// 求两个数组和的最大值for(i = 1; i < len-1; i++) {if(dp_be[i]+dp_af[i+1] > max)max = dp_be[i]+dp_af[i+1];}return max;}
}

i. 画个图说明以下

 

转载于:https://www.cnblogs.com/themo/p/3684113.html

Best Time to Buy and Sell Stock III O(n) 求解方法相关推荐

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

  2. Leetcode Best Time to Buy and Sell Stock III

    Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...

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

  4. Leetcode_123_Best Time to Buy and Sell Stock III

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43740415 Say you have an array ...

  5. Best Time to Buy and Sell Stock III

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

  6. 最多两次股票交易-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 ...

  7. LeetCode OJ - 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. 【Best Time to Buy and Sell Stock III 】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  9. leetcode 123. Best Time to Buy and Sell Stock III | 123. 买卖股票的最佳时机 III(总结DP 模型套路)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ DP 模型套路 DP 套路之:暴力递 ...

最新文章

  1. TENSORFLOW变量作用域(VARIABLE SCOPE)
  2. GitHub超4.4k星:程序员求职,一个算法模板就够了
  3. 智能车竞赛相关资料获取
  4. [moka同学笔记转载]Yii 设置 flash消息 创建一个渐隐形式的消息框
  5. python读取txt文件并写入excel-Python实现读取txt文件并转换为excel的方法示例
  6. mysql查询通过两个字段逆序
  7. python基础知识四——局部作用域和//运算符
  8. linux如何捕获9的信号,2018-9-17-bash之信号捕捉
  9. C和指针之动态内存分配之输入很多整数进行排序
  10. 没有数学天赋是一种什么体验?
  11. 信息学奥赛一本通(1159:斐波那契数列)
  12. Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net...
  13. 最新手机号段 归属地数据库(20191210,共439265条,包括最新的号段)
  14. 软件测试基础理论知识
  15. iOS10获得系统权限
  16. token失效 判断access_记一次微信公众号accesstoken失效的排查
  17. mysql 数据横向拼接_Mysql 合并结果接横向拼接字段
  18. CodeForces - Feng Shui(半平面交)
  19. win10如何改变登陆界面背景
  20. c语言程序设计 计算机2级,计算机二级c语言程序设计考试试题

热门文章

  1. 彻底理解js中的和||
  2. 开源:秋式广告杀手源码
  3. ?Sized 和 Sized
  4. windows live 2012发布文章解决办法??
  5. 句柄泄漏与应用程序体验查找服务(AELookupSvc)
  6. windows2003在打‘SP2’补丁时提示“拒绝访问”
  7. [WPF Bug清单]之(4)——点击RadioButton的空白没有反应
  8. 18 | 理论四:接口隔离原则有哪三种应用?原则中的“接口”该如何理解?
  9. java short s=s 1_Java 面试题 short s = 1; s = s + 1; 与 s += 1; 背后的秘密
  10. 什么叫单模光纤_什么是单模单纤/双纤光纤收发器?二者有何区别?