转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/

本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重复劳动啦.

Here, I describe variants of Kadane’s algorithm to solve the maximum subarray and the minimum subarray problems. The maximum subarray problem is to find the contiguous subarray having the largest sum. Likewise, the minimum subarray problem is to find the contiguous subarray having the smallest sum. Variants of Kadane’s algorithm can solve these problems in O(N) time.

Kadane’s algorithm uses the dynamic programming approach to find the maximum (minimum) subarray ending at each position from the maximum (minimum) subarray ending at the previous position.

   1:  #include <cstdio>
   2:  #include <climits>
   3:  using namespace std;
   4:   
   5:  int maxSum(int *A, int lo, int hi)  {
   6:      int left = lo, right = lo, sum = INT_MIN, currentMaxSum = 0, maxLeft = lo, maxRight = lo;
   7:      for(int i = lo; i < hi; i++)    {
   8:          currentMaxSum += A[i];
   9:          if(currentMaxSum > sum) {
  10:              sum = currentMaxSum;
  11:              right = i;
  12:              maxLeft = left;
  13:              maxRight = right;
  14:          }
  15:          if(currentMaxSum < 0)   {
  16:              left = i+1;
  17:              right = left;
  18:              currentMaxSum = 0;
  19:          }
  20:      }
  21:      printf("Maximum sum contiguous subarray :");
  22:      for(int i = maxLeft; i <= maxRight; i++)
  23:          printf(" %d", A[i]);
  24:      printf("\n");
  25:      return sum;
  26:  }
  27:   
  28:  int minSum(int *A, int lo, int hi)  {
  29:      int left = lo, right = lo, sum = INT_MAX, currentMinSum = 0, minLeft = lo, minRight = lo;
  30:      for(int i = lo; i < hi; i++)    {
  31:          currentMinSum += A[i];
  32:          if(currentMinSum < sum) {
  33:              sum = currentMinSum;
  34:              right = i;
  35:              minLeft = left;
  36:              minRight = right;
  37:          }
  38:          if(currentMinSum > 0)   {
  39:              left = i+1;
  40:              right = left;
  41:              currentMinSum = 0;
  42:          }
  43:      }
  44:      printf("Minimum sum contiguous subarray :");
  45:      for(int i = minLeft; i <= minRight; i++)
  46:          printf(" %d", A[i]);
  47:      printf("\n");
  48:      return sum;
  49:  }
  50:   
  51:  int main()  {
  52:      int A[] = {3, 4, -3, -2, 6};
  53:      int N = sizeof(A) / sizeof(int);
  54:   
  55:      printf("Maximum sum : %d\n", maxSum(A, 0, N));
  56:      printf("Minimum sum : %d\n", minSum(A, 0, N));
  57:   
  58:      return 0;
  59:  }

(转)Maximum subarray problem--Kadane’s Algorithm的更多相关文章

  1. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  2. 动态规划法(八)最大子数组问题(maximum subarray problem)

    问题简介   本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...

  3. 【数据结构】算法 Maximum Subarray

    最大子数组:Maximum Subarray 参考来源:Maximum subarray problem Kadane算法扫描一次整个数列的所有数值,在每一个扫描点计算以该点数值为结束点的子数列的最大 ...

  4. [leetcode53]最长子数组 Maximum Subarray Kadane's算法

    [题目] Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...

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

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

  6. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  7. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

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

  8. Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum

    这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...

  9. LeetCode OJ 53. Maximum Subarray

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

随机推荐

  1. NOJ1066-堆排序

    堆排序 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 414            测试通过 : 220  比 ...

  2. Xhprof安装笔记(PHP性能监控)

    由facebook开源出来的一个PHP性能监控工具,占用资源很少,甚至能够在生产环境中进行部署.它可以结合graphviz使用,能够以图片的形式很直观的展示代码执行耗时 wget http://pec ...

  3. eclipse java.lang.OutOfMemoryError: Java heap space

    1.手动编译运行需要添加 java -Xms256m -Xmx1024m classname 2.在eclipse中,在run as -> run configurations -> ar ...

  4. MicrosoftProjectOxford 微软牛津计划

    光学字符识别 上传本地图片或者提供一个图片URL,查看光学字符识别的演示. 视觉特征分析 上传本地图片或者提供一个图片URL,查看视觉特征分析的演示. 缩略图 上传本地图片或者提供一个图片URL,查看 ...

  5. libevent简介 构成

    libevent简介 libevent是一个事件驱动的网络库,支持跨平台,如Linux, *BSD, MacOS X, Solaris, Windows.支持I/O多路复用,epoll.poll./d ...

  6. 如何在Window上使用Git

    开始的时候同事只给了一个地址,类似这样:git@111.111.1.1:ABCDEF (1)如何在Windows上使用Git 有一篇博客不错:http://www.tuicool.com/articl ...

  7. ASP.NET Web API 2 对 CORS 的支持

    CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览器施加的同域策略安全限制. 所谓同域策略, ...

  8. 设置VS2010中自带的ActiveX控件测试容器TstCon

    ActiveX控件:可以看做一个极小的服务器应用程序,他不能单独运行,需要嵌入到某个程序中才可以运行,我们可以自己写一个程序来测试自己写的程序(具体方法在下一篇文章阐述),第二种方法是利用VS(本人编 ...

  9. graphicsMagick 文档

    ImageMagick资料 ---------------------------------------------------------------------------- ImageMagi ...

  10. ArtJS(原创)

    <script> /* 方法目录: [IE7 8 不支持trim()方法的弥补] [ 终止冒泡] [数字千分位] [js运算的替代方法(js3.3/1.1不等于3浮点计算有bug)] [日 ...