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 ...
随机推荐
- PL/SQL Developer登录出现——Using a filter for all users can lead to poor performance!
用PL/SQL Developer登录Oracle时提示:Using a filter for all users can lead to poor performance! 分析:与Oracle的 ...
- Codeforces 786 B. Legacy
题目链接:http://codeforces.com/contest/786/problem/B 典型线段树优化连边,线段树上的每一个点表示这个区间的所有点,然后边数就被优化为了至多${nlogn}$ ...
- python中while循环运算符及格式化输出
一,while循环 while 条件: while语句块(循环体) 运行: 判断你给的条件是否为真,如果真则执行循环体.否则跳出循环. 执行完循环体之后再次判断条件是否为真 例子1 我们玩联盟的时候喷 ...
- VS IIS Express 支持局域网访问
使用Visual Studio开发Web网页的时候有这样的情况:想要在调试模式下让局域网的其他设备进行访问,以便进行测试.虽然可以部署到服务器中,但是却无法进行调试,就算是注入进程进行调试也是无法达到 ...
- Spring官网下载各版本jar包
1:浏览器输入官网地址:http://spring.io/projects 2:点击如图树叶页面按钮. 3:点击如图小猫图片按钮. 4:查找downloading spring artifacts ...
- Python3 - MySQL适配器 PyMySQL
本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一 ...
- vue 脚手架搭建新项目以及element-ui等vue组件的使用
vue快速搭建项目(前提是你的电脑已经安装了node的环境和vue脚手架安装,不会的自行百度) 1:打开终端: 这里说下此时位置是在User下的lijuntao文件夹下面,我一般会在桌面新建一个文件夹 ...
- 第 5 章 网络 - 032 - 学容器必须懂 bridge 网络
bridge 网络 Docker 安装时会创建一个 命名为 docker0 的 linux bridge.如果不指定--network,创建的容器默认都会挂到 docker0 上. 创建一个容器 一个 ...
- 第 3 章 镜像 - 020 - 搭建本地 Registry
Docker Hub 虽然非常方便,但还是有些限制,比如: 需要 internet 连接,而且下载和上传速度慢. 上传到 Docker Hub 的镜像任何人都能够访问,虽然可以用私有 reposito ...
- 验证码之SimpleCaptcha (二)
上回说到了简单的使用simpleCaptcha,这次我们这次我们将讲解扩张simpleCaptcha. 回到正题,我们需要一些自定义的验证码,比如验证码的字体大小,背景,颜色等等,默认的验 ...