题目

最大子数组 II

给定一个整数数组,找出两个不重叠子数组使得它们的和最大。

每个子数组的数字在数组中的位置应该是连续的。

返回最大的和。

样例

给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3][2, -1, 2]或者[1, 3, -1, 2][2],它们的最大和都是7

注意

子数组最少包含一个数

挑战

要求时间复杂度为O(n)

解题

最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前两个最大子数组的和。然后就想到定义两个数组:left 、right 。

left[i] 表示从左开始到第i个值,并且包括第i个值的最大数组值

right[i]表示从size-1开始到第i个值,并且包括第i个值的最大数组

下面只需要找出两个数组和的最大值,并且两个数组的节点值不能有交集,这里需要遍历所有可能,时间复杂度是O(N2)

public class Solution {
/**
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
public int maxTwoSubArrays(ArrayList<Integer> nums) {
// write your code
int size = nums.size();
if(nums==null || size ==0)
return 0;
int[] left = new int[size];
int[] right = new int[size];
left[0] = nums.get(0);
right[size-1] = nums.get(size-1);
for(int i=1;i<size;i++){
left[i] = Math.max(left[i-1] + nums.get(i),nums.get(i));
}
for(int j=size-2;j>=0 ;j--){
right[j] = Math.max(right[j+1] + nums.get(j),nums.get(j));
}
int max = Integer.MIN_VALUE;
for(int i=0;i<size;i++){
for(int j=size-1;j>i ; j--){
int tmp = left[i] + right[j];
max = Math.max(max,tmp);
}
}
return max;
}
}

Java Code

只求出左到右某个位置的最大子数组,再增加标志位来表示该位置是否是新的子数组的起始位置,然而后面不知道怎么搞了。

参考博客 上面定义的left数组 还是right数组中的值 是包含当前元素的最大值是 局部最大值

在上面博客中定义了全局最大值,这样定义的数组就是一个非降序的数组

数组left ,left[i] 表示从0 到i 这个区间内元素的最大值

同样可以求从i 到 size-1 这个区间的最大值

上面两个值之和的最大值就是答案了

public class Solution {
/**
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
public int maxTwoSubArrays(ArrayList<Integer> nums) {
// write your code
int size = nums.size();
if(nums==null || size ==0)
return 0;
int max = Integer.MIN_VALUE;
int left[] = new int[size];
int localMax = 0;
int globalMax = Integer.MIN_VALUE;
for(int i=0;i< size;i++){
localMax = Math.max(localMax+nums.get(i) , nums.get(i));
globalMax = Math.max(localMax,globalMax);
left[i] = globalMax;
}
localMax = 0;
globalMax = Integer.MIN_VALUE;
for(int i = size -1;i>=0;i--){
if(i< size -1)
max = Math.max(max,globalMax+left[i]);
localMax = Math.max(localMax + nums.get(i),nums.get(i));
globalMax = Math.max(localMax,globalMax); }
return max;
}
}

Java Code

上面的第二个for循环也就是求i 到size -1 区间内的全局最大值,然后就想到可以再定义一个数组

right right[i] 表示从i 到 size -1这个区间的最大

下面只需要根据这个两个数组求出最大值的和就好了

max = Math.max(left[i] + right[i+1],max)

再说明下:

left[i] 表示从0 到i 这个区间的最大值

right[i+1] 表示从 i + 1 都 size-1 这个区间的最大值

显然上面的和的最大值就是答案了

public class Solution {
/**
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
public int maxTwoSubArrays(ArrayList<Integer> nums) {
// write your code
int size = nums.size();
if(nums==null || size ==0)
return 0;
int max = Integer.MIN_VALUE;
int left[] = new int[size];
int localMax = 0;
int globalMax = Integer.MIN_VALUE;
for(int i=0;i< size;i++){
localMax = Math.max(localMax+nums.get(i) , nums.get(i));
globalMax = Math.max(localMax,globalMax);
left[i] = globalMax;
}
localMax = 0;
globalMax = Integer.MIN_VALUE;
int right[] = new int[size];
for(int i = size -1;i>=0;i--){
localMax = Math.max(localMax + nums.get(i),nums.get(i));
globalMax = Math.max(localMax,globalMax);
right[i] = globalMax;
}
for(int i=0;i< size-1;i++){
max = Math.max(left[i] + right[i+1],max);
}
return max;
}
}

Java Code

Python

class Solution:
"""
@param nums: A list of integers
@return: An integer denotes the sum of max two non-overlapping subarrays
"""
def maxTwoSubArrays(self, nums):
# write your code here
if not nums:
return 0 size = len(nums)
left, right = [0 for i in range(size)], [0 for i in range(size)]
MAX, localMax, globalMax = -sys.maxint - 1, 0, -sys.maxint-1
for i in range(size):
localMax = max(localMax+ nums[i],nums[i])
globalMax = max(localMax,globalMax)
left[i] = globalMax
localMax = 0
globalMax = -sys.maxint - 1
for j in range(size-1,-1,-1):
localMax = max(localMax + nums[j],nums[j])
globalMax = max(localMax,globalMax)
right[j] = globalMax
for i in range(size-1):
MAX = max(left[i] + right[i+1],MAX)
return MAX

Python Code

这里为什么求了最小值,表示不理解

lintcode:最大子数组II的更多相关文章

  1. lintcode-42-最大子数组 II

    42-最大子数组 II 给定一个整数数组,找出两个 不重叠 子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [1 ...

  2. lintcode :最大子数组

    题目:  最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6 ...

  3. lintcode:合并排序数组 II

    题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...

  4. lintcode 最大子数组III

    题目描述 给定一个整数数组和一个整数 k,找出 k 个不重叠子数组使得它们的和最大.每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [-1 ...

  5. LintCode——合并排序数组II

    描述:合并两个排序的整数数组A和B变成一个新的数组 样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...

  6. [LintCode] 合并排序数组II

    class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...

  7. 最大子数组(I, II, III,IV,V)和最大子数组乘积 (动态规划)

    I 找一个连续最大子数组,sum加到nums[i], 如果前面子数组和<0则舍去,从头开始. class Solution { public: /** * @param nums: A list ...

  8. lintcode 中等题:搜索旋转排序数组II

    题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...

  9. lintcode:最大子数组差

    题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1], ...

随机推荐

  1. Bootstrap轮播(carousel)插件中图片变形的终极解决方案——使用jqthumb.js

    在顶求网的首页中我使用了BootStrap的轮播(carousel)插件来展示文章中的图片.我在程序中自动抓取文章的第一张图片作为该轮播控件中要显示的图片,由于文章的图片大小不一,而轮播插件的大小基本 ...

  2. 30个惊人的插件来扩展 Twitter Bootstrap

    Bootstrap Maxlength It is a lightweight plugin that allows detecting the HTML maxlength property of ...

  3. Mysql数据库常用的命令 数据备份 恢复 远程

    远程数据库 格式: mysql -h主机地址 -u用户名 -p用户密码数据库 mysql -h 42.51.150.68 -u yang -p discuz mysql设置密码 mysql>us ...

  4. TOMCAT内存大小调整

    Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个java虚拟机.JAVA程序启动时JVM都会分配一个初始内存和最大内存给这个应用程序.这个初始内存和最大内存在一定程度都会 ...

  5. CSS content内容生成技术以及应用

    content属性早在CSS2.1的时候就被引入了,可以使用:before以及:after伪元素生成内容.此特性目前已被大部分的浏览器支持:(Firefox 1.5+, Safari 3.5+, IE ...

  6. SVN四部曲之SVN使用详解进阶

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  7. 从零开始学ios开发(二十):Application Settings and User Defaults(下)

    在上一篇的学习中,我们知道了如何为一个App添加它的Settings设置项,在Settings设置项中我们可以添加哪些类型的控件,这些控件都是通过一个plist来进行管理的,我们只需对plist进行修 ...

  8. 执行umount 命令的时候出现 device is busy

    执行umount 命令的时候出现 device is busy ,有人在使用这块磁盘 umount /dev/sde1 umount: /u01/app/oracle: device is busy ...

  9. Version of SQLite used in Android?

    sing the emulators (adb shell sqlite3 --version): SQLite 3.7.11: 19-4.4-KitKat 18-4.3-Jelly Bean 17- ...

  10. VBS基础篇 - Dictionary对象

    Dictionary是存储数据键和项目对的对象,其主要属性有Count.Item.Key,主要方法有Add.Exists.Items.Keys.Remove.RemoveAll. '建立字典 Dim ...