Code Signal_练习题_adjacentElementsProduct
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.
Example
For inputArray = [3, 6, -2, -5, 7, 3]
, the output should beadjacentElementsProduct(inputArray) = 21
.
7
and 3
produce the largest product.
我的解答:
def adjacentElementsProduct(inputArray):
li = []
if len(inputArray) == 1:
return inputArray[0]
elif len(inputArray) == 2:
return inputArray[0]*inputArray[1]
else:
for i in range(len(inputArray)):
if i+2 <= len(inputArray)-1:
c_max = max(inputArray[i]*inputArray[i+1],inputArray[i+1]*inputArray[i+2])
li.append(c_max)
return max(li)
ret = adjacentElementsProduct([3, 6, -2, -5, 7, 3])
print(ret)
膜拜大神:
def adjacentElementsProduct(inputArray):
return max([inputArray[i] * inputArray[i+1] for i in range(len(inputArray)-1)])
Code Signal_练习题_adjacentElementsProduct的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- firebug中html显示为灰色的原因总结
1.被设置了display:none. 2.长.宽都为0.
- 《JAVA与模式》之不变模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述不变(Immutable)模式的: 一个对象的状态在对象被创建之后就不再变化,这就是所谓的不变模式. 不变模式的结构 不变模式可增强对象的 ...
- SQL一些问题
什么是索引: 一般说法:索引是与表关联的磁盘上结构,可以加快从表中检索行的速度.索引包含由表中的一列或多列生成的键.这些键存储在一个结构中,使 SQL Server 可以快速有效地查找与键值关联的行. ...
- C#之初识单例模式
当我们使用QQ的时候就会发现,他可以启动多个QQ,但是有时候,我们不想这样做,这时候我们就需要使用到单例模式. 1.将Form2的构造函数转为私有 using System.Windows.Forms ...
- ajax获取json数据及实现跨域请求
最近想练习一下ajax获取json数据 , 首先上网找一些在线的可用来测试的接口. -----------------------------------------------------这里是接口 ...
- POJ 1050
#include <stdio.h> #include <string.h> #define mt 101 int main() { int a[mt][mt]; int st ...
- SQLite使用入门
什么是SQLite SQLite是一款非常轻量级的关系数据库系统,支持多数SQL92标准.SQLite在使用前不需要安装设置,不需要进程来启动.停止或配置,而其他大多数SQL数据库引擎是作为一个单独的 ...
- mysql清空表命令:delete和truncate区别
mysql清空表可以用delete和truncate两个命令来完成: 1. delete ① 语法:delete from table_name: ② 示例:DELETE FROM `order`; ...
- jdbc调试sql语句方法
在main命令行输入三个参数到oracle 的 dept2表(自己建的 和dept一样(deptno,dname,loc)),插入到数据库中去.通过本例子,学习在java里调试sql的方法. 写完sq ...
- Android中的各种访问权限Permission含义
android.permission.EXPAND_STATUS_BAR 允许一个程序扩展收缩在状态栏,android开发网提示应该是一个类似Windows Mobile中的托盘程序 android. ...