lintcode:买卖股票的最佳时机 III
买卖股票的最佳时机 III
假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格。设计一个算法来找到最大的利润。你最多可以完成两笔交易。
样例
给出一个样例数组 [4,4,6,1,1,4,2,5], 返回 6
解题
尝试参考买卖股票的最佳时机 II 提交运行发现错误,每次找到连续的递增子数组记录前后的差值,找到两个最大的。如下程序,其实有问题,最大的差值,可能跨两个子数组的。
如:{1,2,4,2,5,7,2,4,9,0}
三个递增数组:{1,2,4}、{2,5,7}、{2,4,9},起始数组的差是:3、5、7,最大两个和是;5+7= 12
然后对前两数组,第二个数组的起始数大于第一个的起始数,而第二个的结束数大于第一个的结束数,通过递增子数组还大于2个,所有有个更大的其实数组差是:7-1 = 6.
public int maxProfit(int[] A) {
// write your code here
if(A == null || A.length == 0)
return 0;
if(A.length == 1)
return 0;
int sum=0;
int i = 0;
int j = 0;
int subSum1 = Integer.MIN_VALUE;
int subSum2 = Integer.MIN_VALUE;
int tmpSum = 0;
while(i < A.length && j < A.length){
tmpSum = 0;
while(j<A.length-1 && A[j] <= A[j+1])
j++;
tmpSum += A[j] - A[i];
if(subSum1 > subSum2){ // subSum1 是较小者
int tmp = subSum1;
subSum1 = subSum2;
subSum2 = tmp;
}
// 当 tmpSum 比较小 subSum1 大 的时候更新sumSum1
if(tmpSum > subSum1){
subSum1 = tmpSum;
}
i = j + 1;// 下一个位置从新开始
j = j + 1;
}
if( subSum2 == Integer.MIN_VALUE)
return subSum1;
return subSum1 + subSum2;
}
题目标签中有个前后遍历,就想到定义两个数组
left[i] 表示0 - i 并且i是卖出的最大收益
right[i] 表示i - A.length-1 并且i 是买入的最大收益
最后求两个数组的最大和,但是这里时间复杂度是O(N^2)可以进一步的降低的
class Solution {
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int[] A) {
// write your code here
if(A == null || A.length == 0)
return 0;
if(A.length == 1)
return 0;
int sum=0;
int[] left = new int[A.length];
int[] right = new int[A.length];
int min = A[0];
// left[i] 表示在 0 - i 中能够 卖出的最大收益,当是 0的时候表示不买也不卖
for(int i =1;i< A.length;i++){
if(min<= A[i]){
left[i] = A[i] - min;
}else
min = A[i];
}
int max = A[A.length - 1];
// right[i] 表示在 i - A.length-1 中能够卖出的最大收益
for( int i = A.length -2;i>=0;i--){
if(max >= A[i]){
right[i] = max - A[i];
}else{
max = A[i];
}
}
max = Integer.MIN_VALUE;
for(int i = 0;i< A.length;i++){
for(int j = i;j<A.length;j++)
max = Math.max(max,left[i] + right[j]);
}
return max;
}
};
如果我们更改定义的两个数组
left[i] 表示0 - i 这段数组的最大收益
right[i] 表示i - A.length-1 这段数组的最大收益
在求两个数组的和时候只需要线性的时间复杂度
class Solution {
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int[] A) {
// write your code here
if(A == null || A.length == 0)
return 0;
if(A.length == 1)
return 0;
int sum=0;
int[] left = new int[A.length];
int[] right = new int[A.length];
int min = A[0];
// left[i] 表示在 0 - i 中能够 卖出的最大收益,当是 0的时候表示不买也不卖
for(int i =1;i< A.length;i++){
if(min<= A[i]){
left[i] = Math.max(left[i-1], A[i] - min);
}else{
left[i] = left[i-1];
min = A[i];
}
}
int max = A[A.length - 1];
// right[i] 表示在 i - A.length-1 中能够卖出的最大收益
for( int i = A.length -2;i>=0;i--){
if(max >= A[i]){
right[i] = Math.max(right[i+1],max - A[i]);
}else{
right[i] = right[i+1];
max = A[i];
}
}
max = Integer.MIN_VALUE;
for(int i = 0;i< A.length;i++){
max = Math.max(max,left[i] + right[i]);
}
return max;
}
};
lintcode:买卖股票的最佳时机 III的更多相关文章
- lintcode:买卖股票的最佳时机 IV
买卖股票的最佳时机 IV 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格. 设计一个算法来找到最大的利润.你最多可以完成 k 笔交易. 注意事项 你不可以同时参与多笔交易(你必须在再次 ...
- Leetcode 123.买卖股票的最佳时机III
买卖股票的最佳时机III 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你 ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- Java实现 LeetCode 123 买卖股票的最佳时机 III(三)
123. 买卖股票的最佳时机 III 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与 ...
- lintcode:买卖股票的最佳时机 I
买卖股票的最佳时机 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. 样例 给出一个数组样例 [3,2,3 ...
- lintcode:买卖股票的最佳时机 II
买卖股票的最佳时机 II 假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再 ...
- 【LeetCode】123、买卖股票的最佳时机 III
Best Time to Buy and Sell Stock III 题目等级:Hard 题目描述: Say you have an array for which the ith element ...
- LeetCode(123):买卖股票的最佳时机 III
Hard! 题目描述: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必 ...
- [Swift]LeetCode123. 买卖股票的最佳时机 III | 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 ...
随机推荐
- 转:CentOS 7 安装Nginx
一.准备工作: 1.安装必备工具: ? 1 2 3 $ yum -y install gcc gcc-c++ autoconf automake $ yum -y install zlib zli ...
- 001--VS2013 c++ 游戏框架
头文件:MainClass.h 内容: #include <Windows.h> //全局函数声明LRESULT CALLBACK WndProc(HWND hwnd, UINT mess ...
- asp.net mvc 错误路由默认配置
问题描述:默认情况下mvc已经将路由参数设置配置好了,这里就不在讲解,请到园子搜索,有很多这方面相关的文章.这里讲述的是,一个MVC项目中,我们输入一个错误的URL,或者根本不存在的URL,如:htt ...
- Matlab 支持向量机(SVM)实现多分类
1.首先,你需要安装完成Matlab. 2.将libsvm-3.17.zip和drtoolbox.tar文件解压到:libsvm-3.17文件夹和drtoolbox,并放到MATLAB的工具箱安装目录 ...
- android selector
android 选择器的使用 1.在drawable文件夹下面建一个xml文件,如item.xml,在eclipse中有selector这个选项 2.可以在布局文件.xml(配置android:lis ...
- 20145129 《Java程序设计》第8周学习总结
20145129 <Java程序设计>第8周学习总结 教材学习内容总结 NIO NIO使用频道(channel)来衔接数据节点,对数据区的标记提供了clear(),rewind(),fli ...
- CS小分队第一阶段冲刺站立会议(5月7日)
昨日完成任务:1.完成了游戏2048退出自动保存功能,进入自动读取存档功能, 2.完成游戏失败判断函数, 3.为游戏添加了背景音乐并且可以手动开关 遇到的困难:使用数据库时由于使用的ACCESS版本比 ...
- Centos6.5以下Samba服务配置
一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的 ...
- Java 字符编码归纳总结
String newStr = new String(oldStr.getBytes(), "UTF-8"); java中的String类是按照unicode进行编码的 ...
- How to Build FFmpeg for Android
http://www.roman10.net/how-to-build-ffmpeg-for-android/ ffmpeg is an open-source platform for record ...