Maximum sub array
Here I post a way to solve maximum sub array problem:
The problem described like this: here is an array like [1,4,5,32,4,8,7,1,23,88], we should find a sub array such that the difference between tail and head of the sub array is maximum.
def getMaxSubarray(ls):
a,b = ls[],ls[]
ixl,ixr = ,
summ =
for i in range(,len(ls)):
if ls[i] > b:
b = ls[i]
ixr = i
#print 'b: ',b,'and bix: ',ixr
if ls[i] < a:
summ2 = b - a
if summ2>summ:
summ = summ2
fixl = ixl
fixr = ixr
a = ls[i]
b = ls[i]
ixl = i
ixr = i
#print 'a: ',a,' and ax: ',ixl,' and b: ',b,' and bx: ',ixr
if (b - a) > summ:
summ = b-a
fixl = ixl
fixr = ixr
return(fixl,fixr,summ) ls = [,,,8.5,10.5,10.2,6.7,10.1,9.4,10.6,11.2,,,,,6.8,,10.1,7.9,9.3,,9.7]
getMaxSubarray(ls)
Maximum sub array的更多相关文章
- 164. Maximum Gap (Array; sort)
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Maximum Gap (ARRAY - SORT)
QUESTION Given an unsorted array, find the maximum difference between the successive elements in its ...
- 53. Maximum Subarray (Array; DP)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 《量化投资:以MATLAB为工具》连载(1)基础篇-N分钟学会MATLAB(上)
http://blog.sina.com.cn/s/blog_4cf8aad30102uylf.html <量化投资:以MATLAB为工具>连载(1)基础篇-N分钟学会MATLAB(上) ...
- UFLDL实验报告1: Softmax Regression
PS:这些是今年4月份,跟斯坦福UFLDL教程时的实验报告,当时就应该好好整理的…留到现在好凌乱了 Softmax Regression实验报告 1.Softmax Regression实验描述 So ...
- MATLAB学习之内存溢出的管理方法
今天用Matlab跑程序,由于数据量太大,又出现 Out of memory. Type HELP MEMORY for your options.的问题.看到这篇文章非常实用,转过来方便查阅~ 用 ...
- win7 32位解决matlab out of memory问题
由于最近在做DL,matlab load数据时由于内存只有2G,会出现out of memory的情况,网上百度了下都是在xp下打开3GB来解决该问题,但是由于win7没有boot.ini无法在win ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- Leetcode: Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
随机推荐
- 用Qemu模拟vexpress-a9 --- 配置 qemu 的网络功能
转载:http://wiki.sylixos.com/index.php/Linux%E7%8E%AF%E5%A2%83%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97 环境介 ...
- Robot Framework+AutoItLibrary使用
目的:用Robot Framework测试win7桌面程序 因为安装完了才补的记录,估计有错漏:( 步骤: 1. 尝试pip install AutoItLibrary 失败 2. 下载A ...
- 【转载】常用 Java 静态代码分析工具的分析与比较
摘自:http://www.oschina.net/question/129540_23043常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基本概念及主要技术,随后 ...
- 18. --plic--=--ply--=--pli--=--ple--=--plex--=--plo-- to fold 倍,重,折叠 (词19、20)
词汇速记20
- 【译】第38节---EF6-基于代码的配置
原文:http://www.entityframeworktutorial.net/entityframework6/code-based-configuration.aspx EF6引入了基于代码的 ...
- Windows系统零开始前端开发环境配置
1. 安装nodejs 国内下载页面(推荐) 官网下载页面 现在的nodejs自带NPM,只需点击下一步下一步安装即可. 为了加速国内NPM包下载,可配置淘宝NPM镜像 2. 安装git 国内下载页面 ...
- 解決 Android Studio 不停 Indexing 的問題(Updating Indices: Indexing paused due to batch update)
遇到這個問題通常是 IDE 更新後,或是反覆使用 Android Studio 開啟其他專案所導致,解決方法其實非常簡單喔! 点击 這個選項的功用是「清除 IDE 暫存並重啟」,沒錯,會出現上述情形的 ...
- 传的参数是url地址时需要特殊处理
<a href="javascript:;" data-url="{$vo.url}" class="info_generate_qr" ...
- 设计模式(八)Proxy Parttern 代理模式
核心作用: 通过代理,控制对对象的访问 可以详细控制某个对象的方法,在调用这个方法做前置处理,调用这个方法后做后置处理(AOP的微观实现) AOP(Aspect Oriented Programmin ...
- Linux下的压缩解压缩命令详解及实例
实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...