买卖股票的最佳时机 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. Android Studio SDK 更新方法

    通常情况下,下载Android SDK需要连接谷歌的服务器进行下载,由于国内水深火热的网络,速度基本为0.好在国内也有一个更新的镜像地址.本文章介绍如何在不FQ的情况下,使用国内镜像地址,更新andr ...

  2. Install sheild设置了Blue皮肤,但是有的窗口更改不了问题

    发现和顺序有关系:1.先指定skins:2.Release:3.再改对话框

  3. scjp考试准备 - 3 - 关于Arrays

    判断如下程序的最终输出值: import java.util.*; public class Quest{ public static void main(String[] args){ String ...

  4. Android开发随笔之ScrollView嵌套GridView[ 转]

    今天在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全,为了解决这个问题查了N多资料,某个谷歌的 ...

  5. 关于Objective-C格式化处理相关规范

    Objective-C格式字符串和C#有很大的差别,下面我们就来看看 在C#中我们可以这么做,简单例举几个: //格式化输出字符串 string word = "world"; s ...

  6. Android编程: MVC模式、应用的生命周期

    学习内容:Android的应用剖析.MVC模式.应用的生命周期 ====Android的应用剖析==== 一个Android应用程序会使用如下组件: Android Activities   界面 A ...

  7. VHDL----基础知识1

    摘要: 打算分几篇,来理清VHDL的基础知识 ----------------------------------------------------------------------------- ...

  8. inout用法浅析

    inout io_data; reg out_data; reg io_link; assign io_data=io_link? out_data:'bz; //当IO_data作为输入口使用时,一 ...

  9. Js作用域与作用域链详解[转]

     一直对Js的作用域有点迷糊,今天偶然读到JavaScript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作 ...

  10. 【收藏】Myeclipse优化

    1 .关闭MyEclipse的自动validation       windows > perferences > myeclipse > validation       将Bui ...