Leetcode_123_Best Time to Buy and Sell Stock III
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- 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- ...
 - 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 ...
 - 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 ...
 - 【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 ...
 - 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 ...
 - 【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: 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 ...
 - [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 ...
 - 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 ...
 
随机推荐
- 关于bitmap你不知道的一些事
			
1.计算机表示图形的几种方式 1)BMP :几乎不进行压缩 占用空间比较大 2)JPG : 在BMP的基础上对相邻的像素进行压缩,占用空间比BMP小 3) PNG : 在JPG的基础上进一步压缩 占用 ...
 - ejabberd编译更新脚本
			
ejabberd编译更新脚本 (金庆的专栏 2016.8) 用rebar编译ejabberd源码,然后复制编译所得beam文件到ejabberd安装目录, 调用ejabberdctl热更新. call ...
 - Redis 4.0新功能介绍
			
Redis 的作者 antirez 在三天之前通过博客文章<The first release candidate of Redis 4.0 is out>发布了 Redis 4.0 的第 ...
 - 自制DbHelper实现自动化数据库交互
			
之前一直对apache的DbUtils很好奇,也很佩服其中的设计上的智慧.于是就自己模拟实现了一个更加简便的小框架.我们只需要在配置文件中写上数据库层面的连接信息,就可以随心所欲的实现自己的需求了. ...
 - 剑指Offer——笔试题+知识点总结
			
剑指Offer--笔试题+知识点总结 情景回顾 时间:2016.9.23 12:00-14:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:笔试 注意事项:要有大局观, ...
 - ThreadLocal的使用[代码片段]
			
1.ThreadLocal定义,在一个类中定义: 在类A中: private static ThreadLocal<String> kcsHtmlPath = new ThreadLoca ...
 - Cocos2D创建多彩文本显示标签
			
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Cocos2D中默认的CCLableTTF类从源代码里看是支持 ...
 - little kernel中如何决定app目录下应该包含哪个app
			
lk中是会为每个app建立一个thread,所以的app都是放在app这个路径下,那是在哪里决定的呢?一般是通过在project下面的MODULE决定的,例如下面这个例子就只用app下面的aboot这 ...
 - UNIX网络编程——非阻塞connect: Web客户程序
			
非阻塞的connect的实现例子出自Netscape的Web客户程序.客户先建立一个与某个Web服务器的HTTP连接,再获取一个主页.该主页往往含有多个对于其他网页的引用.客户可以使用非阻塞conne ...
 - jquery实战---标签页效果
			
在前面的博客中,小编主要简单的介绍了jquery的一些基本知识,今天这篇博文,小编继续来学习jquery的相关知识,今天我们来学习一个标签页的小例子,相关源码小编已经上传,有需要的小伙伴可以自己去下载 ...