(转)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 ...
随机推荐
- 自己写算法---java的堆的非递归遍历
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { S ...
- DB2数据库之间联邦
现在有以下两个数据库:sample,QIN 需要在数据库QIN中访问sample中的表ACT 1.数据库编目 C:\Users\QIN>db2 catalog tcpip node OLIVER ...
- MySQL 设置允许远程登录
1.修改数据表 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在 localhost 的那台电脑,登入MySQL后,更改 "MySQL" 数据库里的 &qu ...
- iOS进阶学习-数据库
一.数据库管理系统 1.SQL语言概述:SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 2.常见 ...
- iOS另类的内存管理
iOS的内存管理算是老生常谈的问题了,我们写iOS的时候无时无刻不在涉及到内存管理.从开始的MRR(manual retain-release)到后来ARC(Automatic Reference C ...
- 60.ISE PhysDesignRules ERROR
PhysDesignRules:2100 - Issue with pin connections and/or configuration on block:<U_ila_pro_0/U0/I ...
- 新安装Ubuntu加载时提示“为/检查磁盘时发生严重错误”的解决方法
本文部分内容转载自: http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6419.html http://www.aichengxu.com/v ...
- 代码实现native2assci
public static void main(String[] args) { String unicode = ""; String s="用户名"; ch ...
- TWaver初学实战——基于HTML5的交互式地铁图
每天坐地铁,经常看地铁图,有一天突然想到,地铁图不也是一种拓扑结果吗?TWaver到底能与地铁图擦出怎样的火花呢? 想到就干,先到网上找幅参考图.各种风格的地铁图还挺多,甚至有大学生自主设计制作, ...
- UVALive - 6529 找规律+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...