Maximum Subarray leetcode java
题目:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle
题解:
这道题要求 求连续的数组值,加和最大。
试想一下,如果我们从头遍历这个数组。对于数组中的其中一个元素,它只有两个选择:
1. 要么加入之前的数组加和之中(跟别人一组)
2. 要么自己单立一个数组(自己单开一组)
所以对于这个元素应该如何选择,就看他能对哪个组的贡献大。如果跟别人一组,能让总加和变大,还是跟别人一组好了;如果自己起个头一组,自己的值比之前加和的值还要大,那么还是自己单开一组好了。
所以利用一个sum数组,记录每一轮sum的最大值,sum[i]表示当前这个元素是跟之前数组加和一组还是自己单立一组好,然后维护一个全局最大值即位答案。
代码如下;
1 public int maxSubArray(int[] A) {
2 int[] sum = new int[A.length];
3
4 int max = A[0];
5 sum[0] = A[0];
6
7 for (int i = 1; i < A.length; i++) {
8 sum[i] = Math.max(A[i], sum[i - 1] + A[i]);
9 max = Math.max(max, sum[i]);
}
return max;
}
同时发现,这道题是经典的问题,是1977布朗的一个教授提出来的。
http://en.wikipedia.org/wiki/Maximum_subarray_problem
并发现,这道题有两种经典解法,一个是:Kadane算法,算法复杂度O(n);另外一个是分治法:算法复杂度为O(nlogn)。
1. Kadane算法
代码如下:
1 public int maxSubArray(int[] A) {
2 int max_ending_here = 0;
3 int max_so_far = Integer.MIN_VALUE;
4
5 for(int i = 0; i < A.length; i++){
6 if(max_ending_here < 0)
7 max_ending_here = 0;
8 max_ending_here += A[i];
9 max_so_far = Math.max(max_so_far, max_ending_here);
}
return max_so_far;
}
2. 分治法:
代码如下:
1 public int maxSubArray(int[] A) {
2 return divide(A, 0, A.length-1);
3 }
4
5 public int divide(int A[], int low, int high){
6 if(low == high)
7 return A[low];
8 if(low == high-1)
9 return Math.max(A[low]+A[high], Math.max(A[low], A[high]));
int mid = (low+high)/2;
int lmax = divide(A, low, mid-1);
int rmax = divide(A, mid+1, high);
int mmax = A[mid];
int tmp = mmax;
for(int i = mid-1; i >=low; i--){
tmp += A[i];
if(tmp > mmax)
mmax = tmp;
}
tmp = mmax;
for(int i = mid+1; i <= high; i++){
tmp += A[i];
if(tmp > mmax)
mmax = tmp;
}
return Math.max(mmax, Math.max(lmax, rmax));
}
Reference:
http://en.wikipedia.org/wiki/Maximum_subarray_problem
http://www.cnblogs.com/statical/articles/3054483.html
http://blog.csdn.net/xshengh/article/details/12708291
Maximum Subarray leetcode java的更多相关文章
- Maximum Subarray - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Maximum Subarray - LeetCode 注意点 最大值有可能是正负数交替着出现 解法 解法一:一次遍历即可.当sum小于0的时候就重新开始 ...
- Maximum Subarray——LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
随机推荐
- TCP可靠传输及流量控制实现原理
一.为什么TCP是可靠传输? 1. 停止等待协议 通过确认与超时重传机制实现可靠传输 在发送完一个分组后,必须暂时保留已发送的分组的副本. 分组和确认分组都必须进行编号. 超时计时器的重传时间应当比数 ...
- 【面试总结】网易2019秋招一站式面试总结(等offer中……)
岗位:运维工程师(网易杭州) 面试时间:一天 上午十一点二十,准时开启面试,初面面试官是个看起来就像是主管的人,厚实的身体,中气浑厚的声音,整齐朴素的衬衫. 简要问题摘录如下:(后续补充答案内容) 1 ...
- 线程池--ThreadPoolExecutor
线程池的实现原理 1)如果当前运行的线程少于corePoolSize,则创建新线程来执行任务(注意,执行这一步骤 需要获取全局锁). 2)如果运行的线程等于或多于corePoolSize,则将任务加入 ...
- 浅谈 Java 中的枚举
枚举也就是一一列举,常用来表示那些可以明确范围的集合,比方说性别,季节,星期,月份等. 在 JDK 1.5 才出现枚举类,在没有出现枚举类之前,我们要表示几个确定的值通常会使用常量来表示,形如 pub ...
- 【java】Hibernate saveOrUpdate失效以及补救方案
有个需求是如果unique key不存在则插入,存在则更新. 简单的方式先select一下再insert 或者update,但是嫌太麻烦而且慢,所以采用Hibernate中session.saveOr ...
- BZOJ 3130: [Sdoi2013]费用流 网络流 二分 最大流
https://www.lydsy.com/JudgeOnline/problem.php?id=3130 本来找费用流的题,权当复习一下网络流好了. 有点麻烦的是double,干脆判断大小或者二分增 ...
- Educational Codeforces Round 45 (Div 2) (A~G)
目录 Codeforces 990 A.Commentary Boxes B.Micro-World C.Bracket Sequences Concatenation Problem D.Graph ...
- bzoj3173 Splay 维护前缀中的最大值
大致题意: 有一个空序列,依次插入1~N到该序列中,每次指定插入的位置,每次插入完成返回当前序列的LIS的长度. 题解: 设dp[i]表示 前缀1~i的最长上升子序列的长度. 因为是按照递增顺序插入的 ...
- dos系统下的游戏~ 不断更新中
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...
- 解决CIFilter滤镜后图片大小和方向发生变化
调用contextWithOptions:和createCGImage: fromRect:方法创建CIContext.与以往不同的地方是CIImage没有frame与bounds属性:只有exten ...