Given an array with integers.

Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest.

Return the largest difference.

Note
The subarray should contain at least one number Example
For [1, 2, -3, 1], return 6 Challenge
O(n) time and O(n) space.

思路:把数组分成两部分,可以从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 indicate the value of maximum difference between two
* Subarrays
*/
public int maxDiffSubArrays(ArrayList<Integer> nums) {
// write your code
if (nums==null || nums.size()==0) return 0;
int len = nums.size();
int[] lGlobalMax = new int[len];
int[] lGlobalMin = new int[len];
int lLocalMax = nums.get(0);
int lLocalMin = nums.get(0);
lGlobalMax[0] = lLocalMax;
lGlobalMin[0] = lLocalMin;
for (int i=1; i<len; i++) {
lLocalMax = Math.max(lLocalMax+nums.get(i), nums.get(i));
lGlobalMax[i] = Math.max(lLocalMax, lGlobalMax[i-1]); lLocalMin = Math.min(lLocalMin+nums.get(i), nums.get(i));
lGlobalMin[i] = Math.min(lLocalMin, lGlobalMin[i-1]);
} int[] rGlobalMax = new int[len];
int[] rGlobalMin = new int[len];
int rLocalMax = nums.get(len-1);
int rLocalMin = nums.get(len-1);
rGlobalMax[len-1] = rLocalMax;
rGlobalMin[len-1] = rLocalMin;
for (int i=len-2; i>=0; i--) {
rLocalMax = Math.max(rLocalMax+nums.get(i), nums.get(i));
rGlobalMax[i] = Math.max(rLocalMax, rGlobalMax[i+1]); rLocalMin = Math.min(rLocalMin+nums.get(i), nums.get(i));
rGlobalMin[i] = Math.min(rLocalMin, rGlobalMin[i+1]);
} int maxDiff = Integer.MIN_VALUE;
for (int i=0; i<len-1; i++) {
if (maxDiff < Math.abs(lGlobalMax[i]-rGlobalMin[i+1]))
maxDiff = Math.abs(lGlobalMax[i]-rGlobalMin[i+1]); if (maxDiff < Math.abs(lGlobalMin[i]-rGlobalMax[i+1]))
maxDiff = Math.abs(lGlobalMin[i]-rGlobalMax[i+1]);
}
return maxDiff;
}
}

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

  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 II

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

  4. LintCode: Maximum Subarray

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

  5. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  6. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  7. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  8. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  9. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

随机推荐

  1. Calculate its MTBF assuming 2000 FITS for each DRAM

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A common unit of meas ...

  2. Tab指示符——Indicator

    先说说我们的思路吧. 其实思路也很简单,就是在咱们的导航下面画一个小矩形,不断的改变这个矩形距离左边的位置. 思路就这么简单,有了思路,接下来就是实现了,看代码: public class Indic ...

  3. Windows下查看机器监听端口

    1.查看所有端口占用情况 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 2.查看指定端口的占用情况      netstat -an |findstr :21 

  4. 用httpPost对JSON发送和接收

    HTTPPost发送JSON: private static final String APPLICATION_JSON = "application/json"; private ...

  5. [LeetCode]题解(python):062 Unique path

    题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x  ...

  6. ASP.NET关于Login控件使用 (转)

    分类: C# 2011-02-21 10:38 4599人阅读 评论(0) 收藏 举报 loginasp.netstringurlserverbutton 今天上网找了一些关于Login控件的使用资料 ...

  7. Java学习-002-Java初识

    此文主要讲述什么是 Java,以及 Java 常识性知识,方便亲们进一步了解 Java 语言相关的常识. 一.Java 概述 Java 语言是美国 Sun Microsystems 公司于 1995 ...

  8. emmc boot1 boot2 partition

    使用mfg tool烧写android5.1的镜像之后,再使用旧版的mfg tool烧写linux或者android镜像,都不能正常启动,而且运行的uboot还是android5.1版本的uboot. ...

  9. JavaScript:下拉列表框的事件处理

    下拉列表框处理操作主要使用的是一个onchang的事件,此事件描述的是内容改变后行为. 范例:观察下拉列表框的事件处理 代码: <!doctype html> <html lang ...

  10. 加州wonders教材扫盲

    加州语文教材主要包含以下内容: 1.主教材Reading/Writing Workshop(读写研讨) 2.拓展教材Literature Anthology(文学选集) 3.延伸阅读材料Leveled ...