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 ...
随机推荐
- audio session config
#pragma mark - #pragma mark - audio session config - (void)setAudioSessionConfig { NSError *error; A ...
- iOS开源加密相册Agony的实现(三)
简介 虽然目前市面上有一些不错的加密相册App,但不是内置广告,就是对上传的张数有所限制.本文介绍了一个加密相册的制作过程,该加密相册将包括多密码(输入不同的密码即可访问不同的空间,可掩人耳目).Wi ...
- Spinner控件详解
Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...
- 在从1到n的正数中1出现的次数
#include <iostream> using namespace std; int cal1From0ToN(int n) { int pow1 = 1; int pow2 = 10 ...
- 不规则递归转换为while,留底
我发现当参数并不太多时,从性能的角度来看,没必要用一个class来保存参数(虽然看起来更加生动形象),直接用最简单的元组就可以了. from hanoi import * # example tree ...
- Android Multimedia框架总结(十四)Camera框架初识及自定义相机案例
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52738492 前言:国庆节告一段 ...
- EJB3基本概念、运行环境、下载安装与运行jboss
EJB3基本概念 什么是EJB: EJB(EnterpriceJavaBeans)是一个用于分布式业务应用的标准服务端组件模型.采用EJB架构编写的应用是可伸的.事务性的.多用户安全的.采用EJB编写 ...
- Android 5.1 添加硬件抽象层(HAL)和JNI接口总结
点击打开链接
- UNIX网络编程——设置套接字超时
在涉及套接字的I/O操作上设置超时的方法有以下3种: 调用alarm,它在指定超时期时产生SIGALRM信号.这个方法涉及信号处理,而信号处理在不同的实现上存在差异,而且可能干扰进程中现有的alarm ...
- 03-Git常用命令演示、冲突演示
Git常用命令演示 Git的的思想其实和SVN还是蛮像的,可以参考之前svn文章一起加深了解. 新建一个user2目录,clone下代码. 修改readme.txt git status 可以看到re ...