算法:寻找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 ...
随机推荐
- A simple script to get all pictures
#-*- coding:utf-8 -*- import shutil import os from Tkinter import * import time import re def get_al ...
- python走起之第十七话
选择器 #id 概述 根据给定的ID匹配一个元素. 使用任何的元字符(如 !"#$%&'()*+,./:;<=>?@[\]^`{|}~)作为名称的文本部分, 它必须被两个 ...
- road习题(二)
答案:[C] 解析:2 8 原则,考虑的是最高峰,所以安装12小时算 ,80%的 PV 也就是访问都是在 早上6点到下午6点这12个小时里,晚上6点到早上6点的PV总量是40000 ,服务器台数为3, ...
- Sublime Text 3插件安装
自动安装: 1.通过快捷键 ctrl+` 或者 View > Show Console 菜单打开控制台 2.粘贴对应版本的代码后回车安装 适用于 Sublime Text 3: import ...
- Python递归报错:RuntimeError: maximum recursion depth exceeded in comparison
Python中默认的最大递归深度是989,当尝试递归第990时便出现递归深度超限的错误: RuntimeError: maximum recursion depth exceeded in compa ...
- ctags使用细节
在src code目录中运行下面的命令(我自己使用的命令): $ctags --langmap=c++:.h --languages=c++,c,perl,verilog -R 其中,指定cta ...
- DPDK编译步骤
大页内存分配: NUMA系统(现在的linux一般都是) echo 1024 > /sys/devices/system/node/node0/hugepages/hugepages-2048 ...
- linux指令(一)文件的操作
ls -i 查看文件的inode号 find ./ inum 1651190 -exec rm -i {} \; 根据inode号删除文件
- 数据挖掘算法(四)Apriori算法
参考文献: 关联分析之Apriori算法
- 在linux上部署web环境
1.升级python到2.7版本(通过源码包重新安装一个2.7版本的python):wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9. ...