-
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 be
adjacentElementsProduct(inputArray) = 21
.7
and3
produce the largest product.我:
const adjacentElementsProduct = arr =>{ const lenght = arr.length let max for (let i = 0; i < lenght-1; i++){ max = max > arr[i] * arr[i+1]? max:arr[i] * arr[i+1] } return max }
别人:
function adjacentElementsProduct(arr) { return Math.max(...arr.slice(1).map((x,i)=>[x*arr[i]])) }
array.map(function(currentValue,index,arr), thisValue)
参数 描述 function(currentValue, index,arr) 必须。函数,数组中的每个元素都会执行这个函数 函数参数:
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue,或者传入 null、undefined,那么回调函数的 this 为全局对象。 -
Below we will define an
n
-interesting polygon. Your task is to find the area of a polygon for a givenn
.A
1
-interesting polygon is just a square with a side of length1
. Ann
-interesting polygon is obtained by taking the <
09.05 五道算法题
最新推荐文章于 2021-11-12 17:36:20 发布
这篇博客包含了多个算法题目,包括寻找数组中最大相邻元素的乘积,计算特定形状的多边形面积,以及解决排列雕像和构建严格递增序列的问题。还涉及到在有鬼房间的矩阵中计算适合CodeBots居住的房间总费用的题目。
摘要由CSDN通过智能技术生成