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 ...
随机推荐
- Spark Streaming应用启动过程分析
本文为SparkStreaming源码剖析的第三篇,主要分析SparkStreaming启动过程. 在调用StreamingContext.start方法后,进入JobScheduler.start方 ...
- Linux Mint 17一周使用体验
1 Win7下安装Mint双系统 Linux Mint支持直接从Win7硬盘引导安装,非常方便,不用制作U盘引导,更不用刻盘安装了.Mint有Cinnamon和Mate两种桌面,听说Mate更加简洁节 ...
- Programming In Scala笔记-第十七章、Scala中的集合类型
本章主要介绍Scala中的集合类型,主要包括:Array, ListBuffer, Arraybuffer, Set, Map和Tuple. 一.序列 序列类型的对象中包含多个按顺序排列好的元素,可以 ...
- Swift:Minimizing Annotation with Type Inference
许多程序猿更喜欢比如Python和Javascript这样的动态语言,因为这些语言并不要求程序猿为每个变量声明和管理它们的类型. 在大多数动态类型的语言里,变量可以是任何类型,而类型声明是可选的或者根 ...
- Android Multimedia框架总结(二十二)MediaCodec中C++中创建到start过程及状态变换
上一章介绍MediaCodec中创建到start过程(到jni部分),从今天开始,将深入源码中看看其c++过程,看下Agenda如下: mediacodec.h CreateByType initMe ...
- Servlet - 基础
Servlet 标签 : Java与Web HTTP协议 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器(Browser)和万维网服务 ...
- Xcode7.3.1中通过最新的CocoaPod安装pop动画引擎
CocoaPod是一个用ruby实现,用于方便的管理Xcode中第三方插件的管理器.用它我们可以很方便的安装和升级插件而不用担心破坏原有的项目. 而pop是一个用于实现App中动画的引擎,它是由Fac ...
- 高仿腾讯QQ即时通讯IM项目
前言:其实这个项目早就开发完成了,在本人的github上,本来没打算写成博客的形式,因为一个项目要写出来要花很久,但是最近看到很多 人在我的github上download后随意发布到网上,本来上传到g ...
- (一二三)基于GCD的dispatch_once实现单例设计
要实现单例,关键是要保证类的alloc和init只被调用一次,并且被自身强引用防止释放. 近日读唐巧先生的<iOS开发进阶>,受益匪浅,通过GCD实现单例就是收获之一,下面把这个方法与大家 ...
- Linux内存映射--mmap函数
Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数声明: 头文件: < ...