(转)Maximum subarray problem--Kadane’s Algorithm
转自: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的更多相关文章
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- 动态规划法(八)最大子数组问题(maximum subarray problem)
问题简介 本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...
- 【数据结构】算法 Maximum Subarray
最大子数组:Maximum Subarray 参考来源:Maximum subarray problem Kadane算法扫描一次整个数列的所有数值,在每一个扫描点计算以该点数值为结束点的子数列的最大 ...
- [leetcode53]最长子数组 Maximum Subarray Kadane's算法
[题目] Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- LeetCode OJ 53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- Oracle Imp and Exp (导入和导出) 数据 工具使用
Oracle 提供两个工具imp.exe 和exp.exe分别用于导入和导出数据.这两个工具位于Oracle_home/bin目录下. 导入数据exp 1 将数据库ATSTestDB完全导出,用户名s ...
- ios中怎么样调节占位文字与字体大小在同一高度
在设置好字体以后,在占位文字中设置leading这个字体属性,用leading来乘以一个比例(CGFloat)来调节位置.
- CoreLocation简单应用
1.获取locationManager let locationManager: CLLocationManager = CLLocationManager() 2.设置locationManager ...
- asdoc 档案
1.asdoc air项目会出现无法找到NativeApplication等错误 解决办法:add args -load-config ....../frameworks/air-config.x ...
- opencv 2.4.9+pcl 1.6+vs2010+win7 32开发环境配置
最近在做图像方面的开发,需要对软件开发平台进行配置,我查找了关于这些方面的内容,由于软件版本很多,每个人的开发平台又不一样所以在对平台进行搭建过程中遇到了很多问题,下面我将我搭建平台的流程做一个记录. ...
- Careercup - Google面试题 - 5898529851572224
2014-05-06 07:56 题目链接 原题: Flatten an iterator of iterators ,], [,[,]], ], it should ,,,,,]. Implemen ...
- Tomcat性能参数设置
Tomcat性能参数设置 Tomcat性能参数设置 博客分类: Java LinuxTomcat网络应用多线程Socket 默认参数不适合生产环境使用,因此需要修改一些参数 1.修改启动时内存参数.并 ...
- Java 7 中 NIO.2 的使用——文件递归操作
众所周知,递归编程是一项有争议的技术,因为它需要大量的内存,但是它能简化一些编程任务.基本上,一个递归操作都是程序调用自己传递参数修改的值或者参数传递到当前的程序循环中.递归编程通常用来计算阶乘斐波那 ...
- IT小小鸟读后感
我是一只小小鸟(读后感) 当我成为闽江学院的一名学子时,我选择了我自己的专业(软件工程).成为大一新生的我对着未来充满着希望憧憬.但现实并不是美好的我得替今后的我想想以后大学毕业了到底我到了社会上要用 ...
- HTML 表格生成
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...