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 ...
随机推荐
- ARM伪指令
1.伪指令是什么 ARM伪指令不是ARM指令集中的指令,只是为了编程方便人们定义了伪指令. 在汇编时这些指令将会被等效的ARM指令或arm指令的组合代替. 编程时可以像其他ARM指令一样使用伪指令,区 ...
- python中关于列表和元祖的基础知识
一,列表 1)定义: []内以逗号分隔,按照索引,存放各种数据类型,每一个位置代表一个元素 2)特性: 1 可存放多个值 2 按照从左到右的顺序定义列表元素,下标为零开始顺序访问,有序 3可修改指定索 ...
- c++ cmakelist 详解
基本元素 首先cmaklist必须包括以下几个部分: #工程名 project(study_case) #cmake最低版本需求 cmake_minimum_required(VERSION 2.8. ...
- Qt5_当前exe所在路径
可以通过以下方式来获取: 1. #include <QDir>#include <QDebug> QDir dir; qDebug() << "curre ...
- typeScript入门基础 (1)
1.ts是js的超集,可使用es5,es6的代码 2. ts的安装与编译: a. 首先需要Node.js环境 . 相信都有,略过. 不会的请百度,或者留言. b. npm install - ...
- vuex深入理解 modules
一.什么是module? 背景:在Vue中State使用是单一状态树结构,应该的所有的状态都放在state里面,如果项目比较复杂,那state是一个很大的对象,store对象也将对变得非常大,难于管理 ...
- 学习笔记45—Linux压缩集
1.压缩功能 安装 sudo apt-get install rar 卸载 sudo apt-get remove rar 2.解压功能 安装 sudo apt-get install unrar 卸 ...
- 学习笔记27—python中numpy.ravel() 和 flatten()函数
简介 首先声明两者所要实现的功能是一致的(将多维数组降位一维).这点从两个单词的意也可以看出来,ravel(散开,解开),flatten(变平).两者的区别在于返回拷贝(copy)还是返回视图(vie ...
- d3.select(this)不能用箭头函数
d3中典型的数据绑定片段 const items = svg.selectAll('g') .data(gdfs,(d)=> d.name); const enter = items.enter ...
- AtCoder Regular Contest 100 Equal Cut
Equal Cut 思路: 枚举中间那个分界点,然后两边找使得切割后差值最小的点,这个可以用双指针 代码: #include<bits/stdc++.h> using namespace ...