转自: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. openSUSE13.1安装搜狗输入法 for Linux

    一句话总结:爽死我了!什么叫输入的快感终于体会到了,搜狗输入法,码农的好伙伴!!! 转自openSUSE论坛 女王陛下 https://forum.suse.org.cn/viewtopic.php? ...

  2. angularjs2 学习笔记(三) 服务

    在anglar2中服务是什么? 如果在项目中有一段代码是很多组件都要使用的,那么最好的方式就是把它做成服务. 服务是一种在组件中共享功能的机制,当我们使用标签将多个组件组合在一起时我们需要操作一些数据 ...

  3. C++类实现三维数组算法

    在学习北京大学教授的<程序设计实习 / Practice on Programming>中,遇到了一个习题,花了很长时间研究,现在分享出来: 课题地址:https://class.cour ...

  4. 数据结构中的堆棧在C#中的实现

    一.大致学习 堆棧是一种面向表的数据结构,堆棧中的数据只能在标的某一短进行添加和删除操作,是一种典型的(LIFO)数据结构. 现实生活中的理解:自助餐厅的盘子堆,人们总是从顶部取走盘子,当洗碗工把洗好 ...

  5. db2新建数据库

    一.建表空间和数据库 1.在db2ad.db2db和db2ap上均执行: [sql] view plaincopyprint? db2set db2comm=tcpip db2set db2codep ...

  6. quartz集群调度机制调研及源码分析---转载

    quartz2.2.1集群调度机制调研及源码分析引言quartz集群架构调度器实例化调度过程触发器的获取触发trigger:Job执行过程:总结:附: 引言 quratz是目前最为成熟,使用最广泛的j ...

  7. AppCan应用开发之插件实践篇-支付插件

    电商O2O无疑是目前最受用户欢迎的APP类型,而在这类APP中必不可少的就是在线支付功能了.在AppCan平台开发这类应用时,官方封装了微信支付.支付宝.银联等多种支付SDK,更接入了多家聚合支付平台 ...

  8. IOS中 如何去除Tabview里面cell之间的下划线

    可以利用Tabview的separatorStyle属性来设置,选择其中的UITableViewCellSeparatorStyleNone 即可去除cell之间的下划线 self.tableView ...

  9. mysql存储过程执行权限问题

    tags: mysql  PROCEDURE 存储过程 definer SECURITY 权限 以下存储过程,限定了DEFINER为root,也就是root之外的账户是无法调用这个存储过程的. 1 2 ...

  10. Xcode全局断点

    1.将导航器视图切换到断点导航器视图下,也可以用快捷键Command+7一步搞定,键盘是window风格的用户Command键是win键(有微软logo),然后点击左下角的+号,选择Add Symbo ...