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) + ...
随机推荐
- (1)Oracle基础--用户与登录
一.用户 · 系统用户 <1> sys,system sys和system是权限比较高的用户,且sys比system权限高.使用sys登录必须使用sysdba或者sysoper的权限,而 ...
- SpringBoot程序远程debug
所谓的远程调试就是服务端程序运行在一台远程服务器上,我们可以在本地服务端的代码(前提是本地的代码必须和远程服务器运行的代码一致)中设置断点,每当有请求到远程服务器时时能够在本地知道远程服务端的此时的内 ...
- 一种基于zookeeper的分布式队列的设计与实现
package com.ysl.zkclient.queue; import com.ysl.zkclient.ZKClient; import com.ysl.zkclient.exception. ...
- ubuntu 16.04 docker下安装klee环境
手动构建docker映象: 从github上获取klee源码 git clone https://github.com/klee/klee.git cd klee 使用存储库根目录中dockerfil ...
- 初次学习Vue,输出Hello Vue!
Vue.js作为目前比较流行的js框架,而我却迟迟没有接触,深感不安! 使用vue之前先要下载vue.js文件,然后在html里面导入vue.js文件,下面试着输出"Hello Vue!&q ...
- Window Navigator对象
Window Navigator对象 txt = "<p>浏览器代号: " + navigator.appCodeName + "</p>&quo ...
- spring boot的拦截器简单使用
1.spring boot拦截器默认有: HandlerInterceptorAdapter AbstractHandlerMapping UserRoleAuthorizationIntercept ...
- ActiveMQ发布-订阅消息模式
一.订阅杂志我们很多人都订过杂志,其过程很简单.只要告诉邮局我们所要订的杂志名.投递的地址,付了钱就OK.出版社定期会将出版的杂志交给邮局,邮局会根据订阅的列表,将杂志送达消费者手中.这样我们就可以看 ...
- mysql数据库修改字符编码问题
遇到这种情况,现有项目的数据库已经建好,数据表也已经创建完成. 问题来的,数据库不能插入中文,调试时候发现中文数据从发送请求到最后请求处理完成这些步骤,中文还没有发生乱码. 只有在存储到数据库后查询数 ...
- nginx的gzip模块
gzip模块是我们在nginx里面经常用到的,压缩响应的数据,这通常有助于将传输数据的大小减少一半甚至更多.可以让我们访问网站更为流畅. Syntax Default Context gzip on ...