Given an array of integers, find two non-overlapping subarrays which have the largest sum.

The number in each subarray should be contiguous.

Return the largest sum.

Note
The subarray should contain at least one number Example
For given [1, 3, -1, 2, -1, 2], the two subarrays are [1, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7. Challenge
Can you do it in time complexity O(n) ?

思路:把数组分成两部分,可以从i和i+1(0<=  i < len-1)之间分开,a[0, i] a[i+1, len-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
if (nums==null || nums.size()==0) return 0;
int len = nums.size();
int lLocal = nums.get(0);
int[] lGlobal = new int[len];
lGlobal[0] = lLocal;
for (int i=1; i<len; i++) {
lLocal = Math.max(lLocal+nums.get(i), nums.get(i));
lGlobal[i] = Math.max(lLocal, lGlobal[i-1]);
} int rLocal = nums.get(len-1);
int[] rGlobal = new int[len];
rGlobal[len-1] = rLocal;
for (int i=len-2; i>=0; i--) {
rLocal = Math.max(rLocal+nums.get(i), nums.get(i));
rGlobal[i] = Math.max(rLocal, rGlobal[i+1]);
} int res = Integer.MIN_VALUE;
for (int k=0; k<len-1; k++) {
if (res < lGlobal[k]+rGlobal[k+1])
res = lGlobal[k] + rGlobal[k+1];
}
return res;
}
}

Lintcode: Maximum Subarray II的更多相关文章

  1. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  2. Lintcode: Maximum Subarray III

    Given an array of integers and a number k, find k non-overlapping subarrays which have the largest s ...

  3. Lintcode: Maximum Subarray Difference

    Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...

  4. LintCode: Maximum Subarray

    1. 暴力枚举 2. “聪明”枚举 3. 分治法 分:两个基本等长的子数组,分别求解T(n/2) 合:跨中心点的最大子数组合(枚举)O(n) 时间复杂度:O(n*logn) class Solutio ...

  5. Maximum Subarray II

    Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...

  6. Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum

    这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...

  7. leetcode644. Maximum Average Subarray II

    leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...

  8. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  9. Maximum Average Subarray II

    Description Given an array with positive and negative numbers, find the maximum average subarray whi ...

随机推荐

  1. image hover

    http://www.nxworld.net/tips/css-image-hover-effects.html

  2. 以大写字母“J”来展示位图

    '0' => '00011100001111111001100011011000001111000001111000001111000001111000001111000001111000001 ...

  3. MetaWeblog 同时管理51cto,csdn,sina,163,oschina,cnblogs等博客

    我们技术人一般都会有自己的一个博客,用于记录一些技术笔记,也期望自己的笔记文章可以让更多人知道. 如何让更多人知道自己的博客? 搜索引擎收录,用户通过关键词搜索可能会进入 内容运营,但是一般技术人为了 ...

  4. uploadify

    uploadify 返回值(回调函数)总结: 最近使用开发一个图片上传模块的时候使用了一个jq插件--uploadify,但是下面就是让人很苦逼的一个下午……一直调试不好,无法接收返回值.google ...

  5. Link Management Protocol (LMP)

    1.1. Link Management Protocol (LMP)   1.1.1.   Introduction and Theory The Link Manager (LM) transla ...

  6. 低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析

    代码实例:点击打开链接 实现的功能是从uart口发送数据至另一个蓝牙串口,或是从蓝牙读取数据通过uart打印出数据. int main(void) { // Initialize leds_init( ...

  7. mysql-zabbix-agent

    使用Zabbix监控MySQL服务器方法 01/27/2014 从Zabbix 2.2开始,Zabbix官方已经支持了MySQL监控,但是MySQL监控默认是不可用的,需要经过额外的设置才可以使用.K ...

  8. JavaScript正则表达式(二)

    定义 JavaScript种正则表达式有两种定义方式,定义一个匹配类似 <%XXX%> 的字符串 1. 构造函数 var reg=new RegExp('<%[^%>]+%&g ...

  9. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

  10. E1114 Temp Ambient

    这2天DELL服务器的指示灯变为了黄色 ,显示“ E1114 Ambient Temp exceeds allowed range“  原来是周围环境温度超出了许可范围 ,难道最近的天真的是太冷了 ”