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. win2003 老的ASP程序报错 Microsoft OLE DB Provider for Orac(0x80004005)

    ASP连接ORACLE报错,记得环境刚配置完成的时候一切正常,今天莫名其妙的报错了 报错位置78行: 这是一个很老的系统,代码没人去东,只是从老的机器迁移到新的服务器中,想想应该是环境的问题 网上搜索 ...

  2. Address localhost:1099 is already in use 的错误

    http://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683

  3. scala手动编译运行

    1  Person.scala class Person { var name = "" } object Person { // a one-arg constructor de ...

  4. Prism&MEF构建开发框架 (一)

    Shell框架XECA shell.xaml主要起到是一个容器或壳的作用 <Window x:Class="XECA.Shell"      xmlns="http ...

  5. input[type=checkbox]

    一个问题,今天用jquery-1.11.3.min.js时遇到的关于input复选框的问题. 类似于以下代码: <ul class="demo">  <li> ...

  6. sort,ksort,asort的区别

    sort--对数组的val进行排序 ksort--对数组的key值进行排序 asort--对数组进行排序,键与值的对应关系不变 1.sort对数组排序 格式如下:bool sort(array &am ...

  7. Nodejs电影建站开发实例(下)

    作为一个真正的网站,不能没有数据的支持,下面使用的数据库为mongodb,电影可能有的数据:电影名称.导演.国家.语言.上映时间.图片.简介.视频 4.使用路由 app.js var express ...

  8. 七步实现magento迁移

    很多朋友都在为magento搬家烦恼,要想把magento从一台服务器迁移到另一台服务器上并不难,下面给大家介绍一种简单方法就能轻松实现magento迁移. 范例:从http://magento.yo ...

  9. MFC的简单加法器(二)

    创建对话框主要分两大步,第一,创建对话框资源,主要包括创建新的对话框模板.设置对话框属性和为对话框添加各种控件:第二,生成对话框类,主要包括新建对话框类.添加控件变量和控件的消息处理函数等.鸡啄米在本 ...

  10. WeUI—微信官方UI库

    WeUI 为微信 Web 服务量身设计 概述 WeUI是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含button.cell ...