本文是在学习中的总结,欢迎转载但请注明出处: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 StockBest 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. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  2. LeetCode 笔记23 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 ...

  3. Best Time to Buy and Sell Stock | & || & III

    Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...

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

  5. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

    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

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  7. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

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

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

随机推荐

  1. 粗浅看Struts2和Hibernate框架

    ----------------------------------------------------------------------------------------------[版权申明: ...

  2. JFinal中使用QuartzPlugin报ClassCastException解决方法

    JDK1.8中泛型反射修改对旧版本的影响 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 问题复现环境: JDK1.8 JFinal1.9 quart ...

  3. Linux Mint 17一周使用体验

    1 Win7下安装Mint双系统 Linux Mint支持直接从Win7硬盘引导安装,非常方便,不用制作U盘引导,更不用刻盘安装了.Mint有Cinnamon和Mate两种桌面,听说Mate更加简洁节 ...

  4. 剑指Offer——知识点储备-网络基础

    剑指Offer--知识点储备-网络基础 计算机网络 http和https的区别 (1)http是http协议运行在tcp之上,所传输的内容都是明文,客户端和服务器端都无法验证对方的身份. (2)htt ...

  5. DoesNotExist at /account/

    DoesNotExist at /account/ User has no account. Request Method: GET Request URL: http://127.0.0.1:800 ...

  6. Linux动态频率调节系统CPUFreq之二:核心(core)架构与API

    上一节中,我们大致地讲解了一下CPUFreq在用户空间的sysfs接口和它的几个重要的数据结构,同时也提到,CPUFreq子系统把一些公共的代码逻辑组织在一起,构成了CPUFreq的核心部分,这些公共 ...

  7. Linux日志管理高级进阶:实例详解syslog

    syslog已被许多日志函数采纳,它用在许多保护措施中,任何程序都可以通过syslog记录事件.syslog可以记录系统事件,可以写到一个文件或设备中,或给用户发送一个信息.它能记录本地事件或通过网络 ...

  8. JVM学习之-栈

    JVM栈解决程序的运行问题,即程序如何执行,或者说如何处理数据;JVM堆解决的是数据存储的问题,即数据怎么放.放在哪儿,另外JVM堆中存的是对象.JVM栈中存的是基本数据类型和JVM堆中对象的引用. ...

  9. 剑指Offer——网易校招内推笔试题+模拟题知识点总结

    剑指Offer--网易校招内推笔试题+模拟题知识点总结 前言 2016.8.2 19:00网易校招内推笔试开始进行.前天晚上利用大约1小时时间完成了测评(这个必须做,关切到你能否参与面试).上午利用2 ...

  10. How to Find the Self Service Related File Location and Versions

     How to Find the Self Service Related File Location and Versions (文档 ID 781385.1) In this Document ...