题目:

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.

More practice:

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.  1     public int maxSubArray(int[] A) {
  2.  2         int[] sum = new int[A.length];
  3.  3         
  4.  4         int max = A[0];
  5.  5         sum[0] = A[0];
  6.  6  
  7.  7         for (int i = 1; i < A.length; i++) {
  8.  8             sum[i] = Math.max(A[i], sum[- 1] + A[i]);
  9.  9             max = Math.max(max, sum[i]);
  10.          }
  11.   
  12.          return max;
  13.      }

同时发现,这道题是经典的问题,是1977布朗的一个教授提出来的。

http://en.wikipedia.org/wiki/Maximum_subarray_problem

并发现,这道题有两种经典解法,一个是:Kadane算法,算法复杂度O(n);另外一个是分治法:算法复杂度为O(nlogn)。

1. Kadane算法

代码如下:

  1.  1     public int maxSubArray(int[] A) {
  2.  2         int max_ending_here = 0;
  3.  3         int max_so_far = Integer.MIN_VALUE;
  4.  4         
  5.  5         for(int i = 0; i < A.length; i++){  
  6.  6             if(max_ending_here < 0) 
  7.  7                  max_ending_here = 0;  
  8.  8             max_ending_here += A[i];  
  9.  9             max_so_far = Math.max(max_so_far, max_ending_here);   
  10.          }  
  11.          return max_so_far; 
  12.      }

2. 分治法:

代码如下:

  1.  1     public int maxSubArray(int[] A) {
  2.  2          return divide(A, 0, A.length-1); 
  3.  3     }
  4.  4     
  5.  5   public int divide(int A[], int low, int high){  
  6.  6         if(low == high)
  7.  7             return A[low];  
  8.  8         if(low == high-1)  
  9.  9             return Math.max(A[low]+A[high], Math.max(A[low], A[high]));
  10.              
  11.          int mid = (low+high)/2;  
  12.          int lmax = divide(A, low, mid-1);  
  13.          int rmax = divide(A, mid+1, high); 
  14.          
  15.          int mmax = A[mid];  
  16.          int tmp = mmax;  
  17.          for(int i = mid-1; i >=low; i--){  
  18.              tmp += A[i];  
  19.              if(tmp > mmax)
  20.                  mmax = tmp;  
  21.          }  
  22.          tmp = mmax;  
  23.          for(int i = mid+1; i <= high; i++){  
  24.              tmp += A[i];  
  25.              if(tmp > mmax)
  26.                  mmax = tmp;  
  27.          }  
  28.          return Math.max(mmax, Math.max(lmax, rmax));  
  29.            
  30.      } 

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的更多相关文章

  1. Maximum Subarray - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Maximum Subarray - LeetCode 注意点 最大值有可能是正负数交替着出现 解法 解法一:一次遍历即可.当sum小于0的时候就重新开始 ...

  2. Maximum Subarray——LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. LeetCode 53. 最大子序和(Maximum Subarray)

    53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...

  5. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  8. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  9. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

随机推荐

  1. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之自签TLS证书及Etcd集群部署(二)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.服务器设置 1.把每一 ...

  2. TRUNCATE can't with condition

    No, TRUNCATE is all or nothing. You can do a DELETE FROM <table> WHERE <conditions> but ...

  3. sklearn六大板块

    六大板块 分类 回归 聚类 数据降维 数据预处理 特征抽取 统一API estimator.fit(X_train,[y_train]) estimator.fit(X_train,[y_train] ...

  4. android studio 汉化包 美化包

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...

  5. 凡信(超仿微信Android版)开源了,内有源码下载 -

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 凡信(超仿微信Android版)开源了,内有源码下载 - IM Geek开发者社区-移动 ...

  6. [IOI2007]Miners

    [IOI2007]Miners 题目大意: 两个人吃东西,总共有\(3\)种食物,每个人每次吃到食物时可以获得的收益是当前食物和前两次吃的食物中,不同食物的种数.现在给定一个长度为\(n(n\le10 ...

  7. hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语

    题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=44 题意:给你n个n,如果能在n-1次运算之后(加减乘除)结果为24的输出n-1次运算的过程,如果不能输 ...

  8. bzoj 3240 矩阵乘法+十进制快速幂

    首先,构造出从f[][i]->f[][i+1]的转移矩阵a,和从f[i][m]->f[i+1][1]的转移矩阵b, 那么从f[1][1]转移到f[n][m]就是init*(a^(m-1)* ...

  9. Elasticsearch中document的基础知识

    写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...

  10. Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

    D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...