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 ...
随机推荐
- svn截图
一.合并一个范围的版本 此类型应用最为广泛,主要是把分支中的修改合并到主干上来.在主干上点击右键选择合并,然后选择合并类型:合并一个范围的版本.合并的源URL填写的是要合并的分支的URL,待合 ...
- Ajax 填充 前端页面
- Lazarus for Raspbian安装
春节前看到树莓派 2代开始销售,第一时间在淘宝下单购买,无奈春节期间放假,要到3月份才可能收到,只能用QEMU模拟器先熟悉树莓系统.对从turbo Pascal开始的人来讲,如果能在树莓系统使用Pas ...
- 20145211 《Java程序设计》实验报告二:Java面向对象程序设计
实验要求 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验内容 单元测试 面向对象三要素 设计模式初步 练习 实 ...
- partial类与[MetadataType(typeof(类名))]有什么区别?
在MVC的Model中,我们可以定义很多与视图相关的元数据,这些元数据对我们开发视图起着相当重要的作用,特别是在数据验证方面.这些元数据一般情况下我们是不会定义在业务实体(或持久化实体)上面,所以很多 ...
- H264关于RTP协议的实现
完整的C/S架构的基于RTP/RTCP的H.264视频传输方案.此方案中,在服务器端和客户端分别进行了功能模块设计. 服务器端:RTP封装模块主要是对H.264码流进行打包封装:RTCP分析模块负责产 ...
- 在RHEL5.2 64bit下编译安装log4cxx错误
apache-log4cxx.0.10.0 依赖于apache-apr和apache-apr-utils两个包 先安装他们俩,然后编译log4cxx,发现make失败,错误如下 ronized.o . ...
- placeholder兼容
<!------------placeholder兼容-------------><script type="text/javascript"> $( ...
- Upgrade Image&ntext to varbinarymax&nvarchar(max)
CREATE PROCEDURE SP_EXEC_WITH_LOG(@I_TICKETNO VARCHAR(10),@I_SQLSTR nvarchar(max))ASBEGIN DECLARE ...
- Spring第13篇—–Spring整合Hibernate之声明式事务管理
不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...