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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM