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程序——串
1.串的定义 串(String):是由零个或多个字符组成的有限序列,又名为字符串. 一般记为s="a1a2a3.........an"(n>=0),其中,s是串名称,用双引号 ...
- Java管道流
管道流的主要作用可以用于两个线程之间的通信,有管道输出流 PipeOutputStream和管道输入流 PipeInputStream.然后通过connect将两个管道连接起来. import jav ...
- nginx搭建http和rtmp协议的流媒体服务器
nginx搭建http和rtmp协议的流媒体服务器 时间:2013-09-23 23:52来源:佚名 作者:本站 举报 点击:232次 实验目的:让Nginx支持flv和mp4格式文件,同时支持Rtm ...
- Android 一键直接查看Sqlite数据库数据
转自:http://www.cnblogs.com/trinea/archive/2012/11/16/2773656.html 本文主要介绍Android开发中如何一键直接查看sqlite数据库中的 ...
- 【转】 class 和 struct 区别
转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...
- Qt的学习资料比起其它C/C++的GUI组件来说已经算很全的了
Qt的学习资料比起其它C/C++的GUI组件来说已经算很全的了.Google的话能解决很多问题,如果没搜到资料的话,如果不是问题太过具体或者奇葩,那就是搜索方法的问题.中文教程中,Qt学习之路系列很不 ...
- HTML5 拖拽功能
本地文件拖动到页面实例:(支持IE) <script> var DragFile = function (goalId) { var g = document.getElementById ...
- Sublime text插件使用技巧
1.CSScomb 一个css代码格式化插件,在css文件中或选中css代码,使用快捷键: [ctrl+shift+c],即可实现代码的对齐等格式的优化. mac下修改快捷键: Preferenc ...
- Effective Project Communications
I was recently invited to speak at a conference in Singapore on Effective Project Communications. I' ...
- golang AES/ECB/PKCS5 加密解密 url-safe-base64
因为项目的需要用到golang的一种特殊的加密解密算法AES/ECB/PKCS5,但是算法并没有包含在标准库中,经过多次失败的尝试,终于解码成功,特此分享: /* 描述 : golang AES/EC ...