买卖股票的最佳时机 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的更多相关文章

  1. lintcode:买卖股票的最佳时机 IV

    买卖股票的最佳时机 IV 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格. 设计一个算法来找到最大的利润.你最多可以完成 k 笔交易. 注意事项 你不可以同时参与多笔交易(你必须在再次 ...

  2. Leetcode 123.买卖股票的最佳时机III

    买卖股票的最佳时机III 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你 ...

  3. 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 ...

  4. Java实现 LeetCode 123 买卖股票的最佳时机 III(三)

    123. 买卖股票的最佳时机 III 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与 ...

  5. lintcode:买卖股票的最佳时机 I

    买卖股票的最佳时机 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. 样例 给出一个数组样例 [3,2,3 ...

  6. lintcode:买卖股票的最佳时机 II

    买卖股票的最佳时机 II 假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再 ...

  7. 【LeetCode】123、买卖股票的最佳时机 III

    Best Time to Buy and Sell Stock III 题目等级:Hard 题目描述: Say you have an array for which the ith element ...

  8. LeetCode(123):买卖股票的最佳时机 III

    Hard! 题目描述: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必 ...

  9. [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. mysql substring_index

    select * from tablename where substring_index(field1,'_',-1)=‘abc' #表中field1的值结构为123_abc

  2. Window7上搭建symfony开发环境(PEAR)

    http://blog.csdn.net/kunshan_shenbin/article/details/7162243 1. 更新PEAR 进入PHP所在目录,找到go-pear.bat并双击. 一 ...

  3. DataView usage combind with event and ViewModel From ERP-DEV

    reflesh the selected item in DataView when we use DataView to display a set of data. Generally, we b ...

  4. MYSQL与 R

    1. 配置MySQL ODBC必须先安装MySQL ODBC driver下载地址可以为:http://www.mysql.com/downloads/connector/odbc/ 2. 控制面板\ ...

  5. 【工作总结】LLDB调试技巧 - 篇一

    备忘命令 : 命令“l”可以查看程序当前运行的位置 (lldb) l } - (void)rightBarButtonAction { 命令“bt”也能查看程序运行的调用栈 (lldb) bt * t ...

  6. 基于.net mvc的校友录(七、文件上传以及多对多关系表的LINQ查询实现)

    图片的上传与调用 图片的上传就是文件的上传,在前台使用的是type="file"的input,但是,要将表单声明为multipart/form-data模式,方法是在BeginFo ...

  7. Notes of the scrum meeting(11/3)

    meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪                  ...

  8. 查看Android应用签名信息

    本文档介绍在Android下如何查看自己的应用签名及三方APK或系统APK签名信息,包含其中的MD5.SHA1.SHA256值和签名算法等信息. 1.查看自己的应用签名 可以通过两种方式查看 (1)  ...

  9. 菜鸟搭建Android环境~~~~绝对靠谱

    因为要测试移动设备.搭建了一下Android环境 这是菜鸟级别的安装 因为sdk版本,eclipse版本,adt版本各自有版本要求,所以我选择都去官网下载新版本,这样总不会出现版本兼容性问题了吧~~ ...

  10. 深入分析windows下配置wamp环境各模块的版本兼容性

    版本相关概念说明: ts/nts: thread safety 线程安全 TS refers to multithread capable builds. NTS refers to single t ...