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

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).

思路:

(1)题意为给定一个数组,数组中第i个元素的值对应着第i天的股票,最多只能进行两次交易,每次交易只能买入一次并卖出,求能得到的最大利润。该题为Best Time to Buy and Sell Stock和Best Time to Buy and SellStockⅡ的加强版。

(2)该题同样考查的是最大差值,但是与前面类似的两题有较大的区别。由于最多只能进行两次交易,假设在数组中存在4个点(其中数组长度大于等于4)a,b,c,d四个点使得到最值,其中a<b<=c<d,f(max)=(d-c) + (b-a),这样可以从任意位置x将数组分为两个区间,分别为0~x和x~len-1。考虑将两个区间的值保存在两个不同的数组中,通过遍历整个数组,就得到了任意点经过两次交易的分布在两个数组中的利润值,然后遍历这两个数组,同一位置上相加得到的最大值即为所得结果。详情见下方代码。

(3)希望本文对你有所帮助。

算法代码实现:

/*** @author liqq*/
public class Solution {public int maxProfit(int[] x) {if (x == null || x.length <= 1)return 0;int[] right = new int[x.length];int[] left = new int[x.length];int rmin = x[0];for (int i = 1; i < x.length; i++) {rmin = Math.min(rmin, x[i]);right[i] = Math.max(right[i - 1], x[i] - rmin);}int lmax = x[x.length - 1];left[x.length - 1] = 0;for (int i = x.length - 2; i >= 0; i--) {lmax = Math.max(lmax, x[i]);left[i] = Math.max(left[i + 1], lmax - x[i]);}int sum = 0;for (int i = 0; i < x.length; i++) {sum = Math.max(sum, right[i] + left[i]);}return sum;}
}

Leetcode_123_Best Time to Buy and Sell Stock III相关推荐

  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. Best Time to Buy and Sell Stock III

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

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

  6. 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 ...

  7. Best Time to Buy and Sell Stock III O(n) 求解方法

    leetcode的题目:http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ leetcode的题目都很简练,但是很 ...

  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. hung-yi lee_p11_逻辑回归
  2. OKR 怎么突然火起来了?
  3. C++实现树的建立,查找,遍历输出
  4. springboot的jsp应该放在哪_详解SpringBoot 添加对JSP的支持(附常见坑点)
  5. 01.Python基础_菜单_快捷键_基本语法_变量_输入输出
  6. 操作系统之计算机系统概述:5、中断和异常
  7. Windows10远程桌面连接提示:出现身份验证错误,要求的函数不受支持
  8. C语言课后习题(11)
  9. java的scanner使用步骤
  10. mvc core2.1 Identity.EntityFramework Core 导航状态栏(六)
  11. pbrt源码中用全主元消去法求矩阵逆的实现
  12. FreeCAD 扫掠空间曲线
  13. IT运营新世界大会:广通软件开启双态运维大时代
  14. 深层和浅层卷积_浅层vs深层javascript
  15. [转载]1986年吴图南 马岳梁 吴英华 孙剑云等名家大師
  16. 请不要在“微服务”的狂热中迷失自我!
  17. php配置设置时区,php如何设置时区
  18. 网络编程:Reactor与Proactor的概念
  19. 怎样把内网IP映射外网
  20. 解决光影精灵锁win键的问题

热门文章

  1. 为什么好好的就不快乐了?
  2. R语言如何绘制韦恩图(6)
  3. PHP高级面试题(三)
  4. 操作系统ucore lab1实验报告
  5. PostgreSQL修炼之道之SQL语言入门(四)
  6. 对数据库三大范式及BC范式的理解
  7. ERROR: Failed building wheel for inplace-abn
  8. 什么是Nginx?有什么用?
  9. 基于python高校学生管理系统
  10. 【图像分类损失】Encouraging Loss:一个反直觉的分类损失