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) + ...
随机推荐
- 极验(Geetest) Laravel 5 集成开发包,让验证更安全
简述 在网站开发中使用频率最高的工具之一便是验证码,验证码在此也是多种多样,不过简单的图片验证码已经可以被机器识别,极验验证码提供了一个安全可靠的滑动验证码体系,让网站开发更加安全. 先感受一下这种验 ...
- 下载一个vue项目执行npm install 后运行项目npm run dev后出错 - 问题解决
在SVN上拉下来一个vue项目,上面没有提交项目里面的node_modules文件夹,所以要自己执行 npm install 安装,但安装完后运行项目后却报错了: $ npm run dev > ...
- 在VMware 14中安装Centos7
在VMware 14中安装Centos7 一.安装前准备 安装VMware14.1 Centos7 64位镜像下载 在VMware中安装Centos7的步骤为: 1.创建虚拟机 创建虚拟机有两种方式: ...
- HTML 遍历
HTML 遍历 HTML基本格式: 1.下行遍历: 属性 说明 contents 子节点的列表,将所有儿子节点存入列表 children 子节点的迭代类型,与.contents类似,用于循环遍历儿子节 ...
- leetcode-917-仅仅反转字母
题目描述: 给定一个字符串 S,返回 “反转后的” 字符串,其中不是字母的字符都保留在原地,而所有字母的位置发生反转. 示例 1: 输入:"ab-cd" 输出:"dc-b ...
- 解决Mysql Workbench的Error Code: 1175错误 无法删除数据
使用workbench,如果你要批量更新或删除数据,一般会报“ Error Code: 1175 You are using safe update mode and you tried to upd ...
- JDK1.10+scala环境的搭建之windows环境
第一步:安装jdk 1,http://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html 去找下 ...
- 关于Item
0.Item The base class of all items, which implements default behavior. 项(Item)代表一个字节码中可以寻址的实体,有许多种类的 ...
- Linux -- 使用笔记
Linux新增分辨率1920x1080 sudo gedit /etc/default/grub 找到:#GRUB_GFXMODE=640x480 在这行下面加一行GRUB_GFXMODE=1920x ...
- C/C++练习题(一)
1. volatile 关键字在 C++ 中的性能和 C 的一样? 作用是一样的,但是其内部实现原理可能不同. 2. scanf 格式化输入是怎么赋值的? 由于scanf输入的数据个数是不定的,从键盘 ...