09.05 五道算法题

这篇博客包含了多个算法题目,包括寻找数组中最大相邻元素的乘积,计算特定形状的多边形面积,以及解决排列雕像和构建严格递增序列的问题。还涉及到在有鬼房间的矩阵中计算适合CodeBots居住的房间总费用的题目。
摘要由CSDN通过智能技术生成
  1. 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 and 3 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 为全局对象。
  2. Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.

    A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the <

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值