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 ...
随机推荐
- 百度Ueditor编辑器的Html模式自动替换样式的解决方法
百度的Ueditor编辑器出于安全性考虑,用户在html模式下粘贴进去的html文档会自动被去除样式和转义.虽然安全的,但是非常不方便. 做一下修改把这个功能去掉. 一.打开ueditor.all.j ...
- ubuntu 工作区中拖动一个窗体到另一个工作区就卡住回不到桌面了
ubuntu 工作区中拖动一个窗体到另一个工作区就卡住回不到桌面了 解决方法: 按 alt + 回车 键直接就返回去了
- xinwajueji
#include<stdio.h> int map[10][10]={0}; int step[30]={0}; int max=99999; int ans[99]={0}; int ...
- 【转】Cocos2d - 观察者模式NotificationCenter
http://shahdza.blog.51cto.com/2410787/1611575 [唠叨] 观察者模式 也叫订阅/发布(Subscribe/Publish)模式,是 MVC( 模型-视图-控 ...
- 实验三--for语句及分支结构else-if
本节课学习到的知识点: 1.for语句的表达式的应用与掌握.流程形式. 2.多分支else-if,用来判断真假等. 实验中遇到的问题及解决方法: 这次课的逻辑要求比之前的课要难许多,而且对于一些数学逻 ...
- 002_base64的编码实现
一.先贴一张ASCII码的图 二.再贴一张base64转换规则的图 二.python代码实现.
- angularJS自定义属性作为条件中转
<html> <head> <meta charset="utf-8"/> <title></title> </h ...
- ArcGIS Engine开发的ArcGIS 版本管理的功能
原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的Arc ...
- SQL Server 用SQL语句查找某个表的触发器
select * from sysobjects where xtype='TR' and parent_obj=object_id('表名') 再用sp_helptext ...
- LeetCode Longest Increasing Path in a Matrix
原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...