《算法导论》一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单。寻找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的更多相关文章

  1. 【算法】LeetCode算法题-Maximum Subarray

    这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...

  2. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. 【数据结构】算法 Maximum Subarray

    最大子数组:Maximum Subarray 参考来源:Maximum subarray problem Kadane算法扫描一次整个数列的所有数值,在每一个扫描点计算以该点数值为结束点的子数列的最大 ...

  4. 动态规划法(八)最大子数组问题(maximum subarray problem)

    问题简介   本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...

  5. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  6. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  8. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  9. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

随机推荐

  1. centos7 docker zookeeper

    docker run --name=zookeepertmp -i -t centos7/jdk7 /bin/bash cd /home wget http://apache.fayea.com/zo ...

  2. 详解MVC设计模式

    1 MVC介绍 众所周知MVC不是设计模式,是一个比设计模式更大一点的模式,称作设计模式不合理,应该说MVC它是一种软件开发架构模式,它包含了很多的设计模式,最为密切是以下三种:Observer (观 ...

  3. es6还欠完善的地方

    const的可变性 const用于声明常量. 什么是常量,声明后的值不可更改. 对于值类型,比如string,number等等.const声明确实有效. const str = "strin ...

  4. MariaDB 在 Windows 下 noinstall 版本的安装和配置

    1. 下载并解压 noinstall 压缩包,假设解压到 D:\APP\mariadb-10.0.17-winx64 目录 下载地址(MariaDB 10.0.17 Stable):llarian.n ...

  5. 给博客添加Flash时钟

    依稀记得在cnblogs很多大牛的博客里见到过大牛的新闻公告栏里那种动感十足的Flash时钟控件,先上图: 作为一名刚的接触博客菜鸟,自然免不了一番好奇.博客设置选项里翻来覆去找(以为是cnblogs ...

  6. /etc/ethers【地址映射】

    该文件存放硬件地址和 IP 地址的映射关系. 格式如下: 00-00-00-00-00-00 0:0:0:0 每一行代表一个 IP 地址.

  7. HTML5 十大新特性(九)——Web Storage

    H5的webStorage技术一共提供了两个对象:window.sessionStorage和window.localStorage. 一.window.sessionStorage--会话级存储 存 ...

  8. windows使用python3.4生成二维码

    1.首先下载qrcode库 使用pip命令: pip install qrcode python3.x以上的版本默认是安装好pip的,如果出现无法找到pip指令的信息的话,则需要首先安装pip. 2. ...

  9. Attendance

    1.打怪 福利好美味(色.... 努力是为了,遇到你时,可以不用因为种种而错过. 一公司的老板对一位职员说:“我出10万买你的老婆,你卖吗?” 职员微笑着说:“那我出15万买你老婆,你同意吗?” 老板 ...

  10. haligong2016

    A 采用递推的方法,由于要到达棋盘上的一个点,只能从左边或者上边过来,根据加法原则,到达某一点的路径数目,就等于到达其相邻的上点和左点的路径数目的总和.所有海盗能达到的点将其路径数置为0即可. #in ...