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

    https://en.wikipedia.org/wiki/Greedy_algorithm

  2. 一段检测IP设备是否在线的代码

    原理是通过发送ARP包来检测 uses WinSock function SendARP(const DestIP, SrcIP: Cardinal; pMacAddr: PULONG; var Ph ...

  3. OC接收数据时毫秒转date时间最简略方法

    一般项目中接收后台的数据会收到毫秒格式的date,需要换算成正规日期格式,这时候我们的好朋友command + c 和 command + v就得出来帮忙了: 可以复制使用如下方法: + (NSStr ...

  4. 关于HIVE的配置

    一:安装配置hive 1.检测hadoop 2.解压hive 3.修改环境 sudo vi /etc/profile 4.source以下 5.复制hive-env.sh 6.编辑hive-env.s ...

  5. sublime删除安装的插件

    如果想要删除插件,Ctrl+Shift+P调出命令面板,输入remove,调出Remove Package选项并回车,选择要删除的插件即可

  6. MANIFEST.MF详解(转)

    转载自http://blog.csdn.net/zhifeiyu2008/article/details/8829637 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, ...

  7. Four Operations---hdu5938(暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938 题意:给一个不超过20位并且大于2位的数字字符串(只包含1-9)然后找到适当的位置依次放入&qu ...

  8. Java学习-032-JavaWeb_001 -- Tomcat环境部署及基本配置

    首先到 Tomcat 官网,下载对应的版本,我本机的系统是 WIN7 64BIT 的,因而我选择的是64bit 的zip包,如下图所示:

  9. java 中间件

    先说中间件:非底层操作系统软件.非业务应用软件,不是直接给最终用户使用的,不能直接给客户带来价值的软件,统称中间件.常见的有如下几种:服务中间件.集成中间件.数据中间件.消息中间件.安全中间件. 其中 ...

  10. synchronized锁自旋2

    http://www.infoq.com/cn/articles/java-se-16-synchronized 1 引言 在多线程并发编程中Synchronized一直是元老级角色,很多人都会称呼它 ...