(转)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 ...
随机推荐
- WPF数据双向绑定
设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人. 即:Pr ...
- 关于activity_main.xml与fragment_main.xml
第一种解决办法 新版安装SDK文件一开始有两个XML文件,activity_main.xml和fragment_main.xml,不习惯的可以这样处理:1.删除fragment_main.xml整个文 ...
- ORACLE 单实例完全卸载数据库
1.用oracle用户登录如果要再次安装, 最好先做一些备份工作.包括用户的登录脚本,数据库自动启动关闭的脚本,和Listener自动启动的脚本.要是有可能连创建数据库的脚本也保存下来 2.使用SQL ...
- Learning note for Binding and validation
Summary of my learning note for WPF Binding Binding to DataSet. when we want to add new record, we s ...
- Ubuntu 12.04 Desktop安装XAMPP
1/打开终端 在Dash里搜索.打开Dash,在里面搜索“gnome-terminal”,就可以找到终端应用序.快捷键Ctrl+Alt+L也可以,不过如果是虚拟机的话可能会有问题. 如果想以后快捷打开 ...
- iTween基础之功能简介
一.iTween 介绍 .二.iTween 原理.三.iTween 下载.四.iTween 类介绍.五.主要功能介绍 原文地址:http://blog.csdn.net/dingkun520wy/ar ...
- 使用C语言在Win控制台中输出带颜色的文字
学了这么久的C语言,一直停留在编写“控制台”程序的水平.黑色窗口,白色的文字,看多了着实让人感到枯燥无味.但是作为业余爱好者,我既没有那么多时间和精力去学习如何编写窗口程序,也没有那个必要一定用C去调 ...
- VBS数组函数学习实例分析
Array 函数 返回包含数组的Variant. Array(arglist) 参数:arglist是赋给包含在Variant中的数组元素的值的列表(用逗号分隔).如果没有指定此参数,则将会创建零长度 ...
- Java应用的优秀管理工具Maven的下载安装及配置
1.进入Maven的官方下载地址:http://maven.apache.org/download.cgi 2.向下滚动页面,点击这个zip包进行下载: 3.将压缩包解压后剪切到Mac的某个目录下就完 ...
- Careercup - Google面试题 - 5724823657381888
2014-05-06 06:37 题目链接 原题: Given an array of (unsorted) integers, arrange them such that a < b > ...