Lintcode: Maximum Subarray Difference
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的更多相关文章
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- Lintcode: Maximum Subarray III
Given an array of integers and a number k, find k non-overlapping subarrays which have the largest s ...
- Lintcode: Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...
- LintCode: Maximum Subarray
1. 暴力枚举 2. “聪明”枚举 3. 分治法 分:两个基本等长的子数组,分别求解T(n/2) 合:跨中心点的最大子数组合(枚举)O(n) 时间复杂度:O(n*logn) class Solutio ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
随机推荐
- Java内存管理和垃圾回收
笔记,深入理解java虚拟机 Java运行时内存区域 程序计数器,线程独占,当前线程所执行的字节码的行号指示器,每个线程需要记录下执行到哪儿了,下次调度的时候可以继续执行,这个区是唯一不会发生oom的 ...
- Arrays.toString Arrays.asList
import java.util.Arrays; public class TestCalc{ public static void main(String[] args) { ,,,,,,,}; / ...
- 高级 Synth
http://www.ibm.com/developerworks/cn/java/j-synth/
- PIC12F629帮我用C语言写个程序,控制三个LED亮灭
http://power.baidu.com/question/240873584599025684.html?entry=browse_difficult PIC12F629帮我用C语言写个程序,控 ...
- VC更换图标文件
步骤 1. 删除图标 2. 在VC中移除图标 3. 用新图标替换原来图标 4. 添加图标到VC 5. 重新编译 参考文档 http://www.cnblogs.com/BloodAndBone/arc ...
- Bluetooth数据包捕获
目录 1. 前提 2. 开启功能 3. 抓包 这里介绍一种在Android上捕获蓝牙数据包的方法 1. 前提 首先你要有一部Android手机 然后你的Android系统版本要在4.4及以上 我没有做 ...
- 设计模式:策略模式(Strategy)
定 义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...
- JSON格式解析和libjson使用简介(关于cjson的使用示例)
JSON格式解析和libjson使用简介 在阅读本文之前,请先阅读下<Rss Reader实例开发之系统设计>一文. Rss Reader实例开发中,进行网络数据交换时主要使用到了两种数据 ...
- springmvc返回值、数据写到页面、表单提交、ajax、重定向
实验是在前一篇文章的项目上做的: 数据写到页面 后台往前台传数据 TestController添加 /** * 方法的返回值采用ModelAndView, new ModelAndView(" ...
- Swift-09-可空链式调用(Optional Chaining)
我对这个的理解就是:我们有可能会用到其他的属性或者方法,当我们在使用其他的时候,可以使用点语法去访问另一个的属性,这样的使用,就形成了链式访问. 可空链式调用是一种可以请求和调用属性.方法及下表的过程 ...