zoukankan      html  css  js  c++  java
  • 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.

    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     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]);
    10         }
    11  
    12         return max;
    13     }

    同时发现,这道题是经典的问题,是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);   
    10         }  
    11         return max_so_far; 
    12     }

     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]));
    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

  • 相关阅读:
    黄聪:C#多线程教程(1):BeginInvoke和EndInvoke方法,解决主线程延时Thread.sleep柱塞问题(转)
    黄聪:C#类似Jquery的html解析类HtmlAgilityPack基础类介绍及运用
    黄聪:国内com域名转移到Godaddy详细教程(转)
    黄聪:Navicat for MySQL的1577错误解决
    黄聪:VPS配置Filezilla Server支持FTP的Passive被动模式(FTP连接不上怎么办?有详细教程)
    黄聪:Microsoft office 2013版下载、安装及破解工具下载破解教程(Windows Toolkit)
    黄聪:mysql搬家,直接复制data文件夹(*.MYD,*.MYI,innodb)出错,无法正常显示
    黄聪:WordPress默认编辑器可视化切换不见了,非插件导致消失问题
    黄聪:自定义WordPress顶部管理工具条的技巧(转)
    黄聪:VS2010中“新建项目”却没有“解决方案”节点,如何调出来
  • 原文地址:https://www.cnblogs.com/springfor/p/3877058.html
Copyright © 2011-2022 走看看