Lintcode: Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Note
The subarray should contain at least one number Example
For given [1, 3, -1, 2, -1, 2], the two subarrays are [1, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7. Challenge
Can you do it in time complexity O(n) ?
思路:把数组分成两部分,可以从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 denotes the sum of max two non-overlapping subarrays
*/
public int maxTwoSubArrays(ArrayList<Integer> nums) {
// write your code
if (nums==null || nums.size()==0) return 0;
int len = nums.size();
int lLocal = nums.get(0);
int[] lGlobal = new int[len];
lGlobal[0] = lLocal;
for (int i=1; i<len; i++) {
lLocal = Math.max(lLocal+nums.get(i), nums.get(i));
lGlobal[i] = Math.max(lLocal, lGlobal[i-1]);
} int rLocal = nums.get(len-1);
int[] rGlobal = new int[len];
rGlobal[len-1] = rLocal;
for (int i=len-2; i>=0; i--) {
rLocal = Math.max(rLocal+nums.get(i), nums.get(i));
rGlobal[i] = Math.max(rLocal, rGlobal[i+1]);
} int res = Integer.MIN_VALUE;
for (int k=0; k<len-1; k++) {
if (res < lGlobal[k]+rGlobal[k+1])
res = lGlobal[k] + rGlobal[k+1];
}
return res;
}
}
Lintcode: Maximum Subarray II的更多相关文章
- [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 Difference
Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...
- LintCode: Maximum Subarray
1. 暴力枚举 2. “聪明”枚举 3. 分治法 分:两个基本等长的子数组,分别求解T(n/2) 合:跨中心点的最大子数组合(枚举)O(n) 时间复杂度:O(n*logn) class Solutio ...
- Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- leetcode644. Maximum Average Subarray II
leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- Maximum Average Subarray II
Description Given an array with positive and negative numbers, find the maximum average subarray whi ...
随机推荐
- 基于LR的数据库性能测试
web services协议 在LR的web services协议中提供了如下函数进行数据库的连接和执行sql语句,以实现对数据库的性能测试,也可以实现向数据库中自动生成批量数据. lr_db_dis ...
- mysql从只有一个备份文件(多个数据库的备份)中恢复数据到指定数据库
mysql -uroot -p 要恢复的数据库的名字 --one-database<备份文件
- springMVC框架下——通用接口之图片上传接口
我所想要的图片上传接口是指服务器端在完成图片上传后,返回一个可访问的图片地址. spring mvc框架下图片上传非常简单,如下 @RequestMapping(value="/upload ...
- Ubuntu apparmor何方神圣
AppArmor 是一款与SeLinux类似的安全框架/工具,其主要作用是控制应用程序的各种权限,例如对某个目录/文件的读/写,对网络端口的打开/读/写等等. 来之Novell网站的引用: AppAr ...
- java NIO ;mvn
http://ifeve.com/java-nio-scattergather/ mvn introduction(install,conf, proxy,plugin,samples) http:/ ...
- html代码转义到js时,往往会遇到问题,这代码实现html和js互转
这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- C++控制程序只运行一个实例
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { ...
- 帝国CMS备忘
一. 2级导航: 类似下图这种导航: 实现方式如: 1. 定义一个标签模板,记住ID,具体内容如: 页面模板内容: <li><a href=”[!—bclassurl—]”>[ ...
- js的执行顺序
js是顺序执行的,但是在一个<script></script>标签中,后面的函数会预加载.如: <script type="text/javascript&qu ...
- http://blog.csdn.net/yunhua_lee/article/details/52710894
http://blog.csdn.net/yunhua_lee/article/details/52710894