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 ...
随机推荐
- 1.总结---tr()和QTextCodec对象
1. 关于Qt 中的tr()函数-------http://tscsh.blog.163.com/blog/static/200320103201310213312518/ 在论坛中漂,经常遇到有人遇 ...
- 27.some company's Spi Flash chip replace altera epcsxxx
由于altera公司的epcsxxx芯片比较贵,所以一般用其它公司的spi flash芯片代替也可以.据AlteraFAE描述:“EPCS器件也是选用某家公司的SPIFlash,只是中间经过Alter ...
- html a 链接
1.链接元素 文本.图像.热区 2.标记 描述性文字 target窗口形式 _self:在自身窗口打开(默认) _blank:在新窗口打开 _parent:在父窗口打开 _top:在顶窗口打开 框架或 ...
- 【BZOJ 2120】 数颜色
Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜 ...
- Java应用的优秀管理工具Maven的下载安装及配置
1.进入Maven的官方下载地址:http://maven.apache.org/download.cgi 2.向下滚动页面,点击这个zip包进行下载: 3.将压缩包解压后剪切到Mac的某个目录下就完 ...
- 在FreeBSD上搭建Mac的文件及time machine备份服务
上周将工作用电脑由公司配备的台式机切换到自己低配的macbook air上面,小本本的128G SSD远远不能满足工作的储存需要,但又不舍得入手昂贵的AirPort Time Capsule,于是考虑 ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- C#外挂QQ找茬辅助源码,早期开发
这是一款几年前开发的工具,当年作为一民IT纯屌,为了当年自己心目中的一位女神熬夜开发完成.女神使用后找茬等级瞬间从眼明手快升级为三只眼...每次看到这个就会想起那段屌丝与女神的回忆.今天特地把代码更新 ...
- 【LRU Cache】cpp
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 用PHP向数据库中添加数据
显示页面(用户可见) <body><form action="chuli.php" method="post"> //将该页面接收的数 ...