(转)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动画 storyboard
<Window x:Class="StoryBoard.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- MIFARE系列5《存储结构》
Mifare S50把1K字节的容量分为16个扇区(Sector0-Sector15),每个扇区包括4个数据块(Block0-Block3),我们也将16个扇区的64个块按绝对地址编号为0~63,每个 ...
- Outlook打不开? 进程一大堆!
问题描述: ====== 关闭Outlook应用程序后,Outlook.exe进程仍在任务管理器里继续运行,不能关闭. 原因: ====== Outlook的插件或者扩展程序阻止Outlook关闭 解 ...
- DB2批处理数据导入
这里需要两个BAT文件 first.bat @echo off @set /p databaseName=1)请输入数据库名: @set /p userName=2)请输入用户名: @set /p u ...
- oracle 查询今天哪个表增加的数据多
一.创建一个表 create table A( TABLE_NAME VARCHAR2(200), COUNT_NUM NUMBER) 二.创建一个存储过程create or replace ...
- 九度oj 1521 二叉树的镜像
原题链接:http://ac.jobdu.com/problem.php?pid=1521 水题,如下.. #include<algorithm> #include<iostream ...
- ExtJS MVC结构
概述 大型的应用在开发和运维上都存在着困难.应用功能的调整和开发人员的调动都会影响对项目的掌控.ExtJS4带来了一种新的应用结构.这种结构不止用于组织代码,也能有效的减少必要的代码量. 这次ExtJ ...
- c# Random Class usage
Random Usage sometimes, we hope to generate some random infor to manipulate our data structor. we ca ...
- C# 字符串转换值类型
bool status = int.TryParse(m_Judge(12)+"ds",out j); int iParse = int.Parse("4"); ...
- 如何设置电脑的IP
先找到自己的ip,在设置ip. 图1 图2 图3 图4 图5 图6