题目:

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

Maximum Subarray leetcode java相关推荐

  1. [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray

    41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...

  2. [LeetCode] Maximum Subarray 最大子数组

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

  3. LeetCode: Maximum Subarray 解题报告

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

  4. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  5. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  6. LeetCode(53):Maximum Subarray

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

  7. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  8. 【leetcode】Maximum Subarray

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

  9. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

最新文章

  1. 【NIO】异步模型之Callback -- 封装NIO
  2. Android 程序自动更新功能模块实现
  3. 机器学习(MACHINE LEARNING)MATLAB动态规划解决背包问题
  4. 按键抖动仿真Testbench(repeat、task、random)的用法
  5. 把文件夹下的所有文件打包成一个zip包
  6. RocketMQ 很慢?引出了一个未解之谜
  7. Windows XP \Windows 2003启动过程的学习及故障分析处理(四)
  8. 期权定价理论及其matlab实现过程,期权定价理论及其Matlab实现过程
  9. python中exp_python中的exp是什么
  10. Microsoft将Linux带至桌面操作系统
  11. oracle database 11g plsql 程序设计,oracle-database-11g-plsql-编程实战笔记
  12. python和c 先学哪个-C和Python我该先学什么?
  13. java中的POJO、PO、VO分别是什么?
  14. bcp入库oracle,Sybase中的BCP用法实践 与 Oracle的SQLLDR用法实践
  15. python关键字define_在Python中,使用关键字define定义函数。
  16. HDU ACM 2647 Reward (topology----拓扑排序)
  17. “华为杯”第十八届中国研究生数学建模竞赛一等奖经验分享
  18. 让玩家提升游戏耐玩度的8个小技巧
  19. SCSS 中这些实用技巧,你可能还不知道!
  20. requir与include的区别

热门文章

  1. [填坑]SerialPort的“端口被关闭”
  2. 20非常有用的Java程序片段(11-15)
  3. SqlServer为什么自动在主键上建立聚集索引
  4. LeetCode 627. 交换工资
  5. RabbitMQ(六) Routing路由模式
  6. CentOS+Nginx一步一步开始配置负载均衡
  7. EASYUI 表单(FORM)用法
  8. 消息队列解耦是骗小孩儿的
  9. Kubernetes群集的零停机服务器更新
  10. STL_stack/queue