算法:寻找maximum subarray
《算法导论》一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单。寻找maximum subarray稍微复杂点。
题目是这样的:给定序列x = [1, -4, 4, 4, 5, -3, -4, 9, 6 - 4, 6, 4, 3, -5];寻找一个连续的子序列,使得其和是最大。
这个题目有意义的地方在于,序列X的元素有正有负。
思路很简单,把序列分为相同的两部分A和B,在其内寻找maximum subarray,那么maximum subarray有可能在A中,也有可能在B中,也有可能横跨A和B。
所以,一、递归地在A,B中寻找最大子序列;二、在序列X中寻找横跨中间点的maximum subarray;
最后,比较三者,哪个打,结果就是哪个喽。
代码如下:
在序列X中寻找横跨中间点的maximum subarray
def findmaxcrosssubarr(arr, low, mid, high):
lefmax = -10000
sum_l = 0
i = mid
index_l=mid
index_r=mid
while (i > low):
sum_l += arr[i]
if sum_l > lefmax:
lefmax = sum_l
index_l = i
i -= 1
rightmax = -10000
sum_r = 0
j = mid + 1
while (j < high):
sum_r += arr[j]
if sum_r > rightmax:
rightmax = sum_r
index_r = j
j += 1
return lefmax + rightmax, index_l, index_r
递归地寻找maximum subarray
def maxsubarr(arr, low, high):
if high-low < 1:
return arr[low], low, high
mid = (high+low)/2
value_l, low_l, high_l = maxsubarr(arr, low, mid)
value_r, low_r, high_r = maxsubarr(arr, mid+1, high)
value_m, low_m, high_m = findmaxcrosssubarr(arr, low, mid, high)
maxvalue = max(value_l, value_m, value_r)
if maxvalue==value_l:
return value_l, low_l ,high_l
if maxvalue==value_r:
return value_r, low_r, high_r
if maxvalue==value_m:
return value_m, low_m, high_m
感想:递归解决问题的几个考虑的点:1、做好问题的分解,形成递归(就是说子问题和原问题是同类型的)后,就可以假设可以解决了,最多就是把初始情况解决了;2、子问题合并回原问题比较有技巧性,需要多思考。
当然,也可以使用暴力解法,遍历所有可能的情况,通过比大小,找出答案。
def brutemaxsub(arr):
m=-10000
s,t=0,0
for i in range(len(arr)):
j=i
maxj=0
while j<len(arr):
maxj+=arr[j]
if maxj>m:
m=maxj
s=i
t=j
j+=1
return m,s,t
当然啦,这个问题也可以在线性时间内解决。
代码如下,有点绕。
def maxsub(arr):
m=m1=arr[0]
s,t=s1,t1=0,0
i = 1
while i<len(arr):
m1+=arr[i]
if m1>m:
s=s1
t=t1=i
m=m1
if m1<0:
s1=i+1
t1=i + 1
m1=0
i+=1
return m,s,t
算法:寻找maximum subarray的更多相关文章
- 【算法】LeetCode算法题-Maximum Subarray
这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数据结构】算法 Maximum Subarray
最大子数组:Maximum Subarray 参考来源:Maximum subarray problem Kadane算法扫描一次整个数列的所有数值,在每一个扫描点计算以该点数值为结束点的子数列的最大 ...
- 动态规划法(八)最大子数组问题(maximum subarray problem)
问题简介 本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (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 ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 53. Maximum Subarray最大求和子数组12 3(dp)
[抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...
随机推荐
- JDBC数据库连接池技术
在JDBC中,获得连接或释放资源是非常消耗系统资源的两个过程,为了解决此类性能问题,通常采用连接池技术,来共享连接.这样我们就不需要每次都创建连接.释放连接了,这些操作都交给了连接池. 用池的概念来管 ...
- left和offsetLeft
left: 1.当该对象的定位position为absolute时left是相对于拥有定位属性(position的值为默认值"static"时除外)的父级对象的左边距. 例1:当父 ...
- JSON生成c#类代码小工具
JSON生成c#类代码小工具 为什么写这么个玩意 最近的项目中需要和一个服务端程序通讯,而通讯的协议是基于流行的json,由于是.net,所以很简单的从公司代码库里找到了Newtonsoft.dll( ...
- linux中Makefile文件相关内容
第一章.概述什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional(专业)的程序员,m ...
- jupyter nb + scite 混合搭建成我的最爱IDE
jupyter nb + scite 混合搭建成我的最爱IDE 自从体验过jupyter notebook之后, 就深深地爱上了你, jupyter. jupyter这个名字也很古怪的. 它应该是ju ...
- Node.js 基础库
全局对象 Node.js 中的全局对象是 global,所有全局变量(除了 global 本身以外)都是 global对象的属性. 我们在 Node.js 中能够直接访问到对象通常都是 global ...
- Eclipse右下角一直提示Computing additional info解决办法
今天不知道按了什么,发现Eclipse右下角一直提示computing additional info,打开progress,里面同时有好几条一样的信息,但是一直也执行不完,上网查找方案,发现原来是用 ...
- 2017 New Year’s Greetings from Sun Yat-sen University
As winter turns to spring, the world around us begins to take on an air of freshness. As 2017 is fa ...
- PHP 文件的操作
操作文件的步骤: 1.打开文件2.做操作PS!!!3.关闭文件 打开 操作
- 【转】 linux编程之GDB调试
GDB是一套字符界面的程序集,可以用它在linux上调试C和C++程序,它提供了以下的功能: 1 在程序中设置断点,当程序运行到断点处暂停 2 显示变量的值,可以打印或者监视某个变量,将某个变量的值显 ...