每日一题——LeetCode1464.数组中两元素的最大乘积

这题就是找数组里的最大值和次大值 

方法一 排序

var maxProduct = function(nums) {
    nums.sort((a,b)=>b-a)
    return (nums[0] - 1) * (nums[1] - 1);
};

消耗时间和内存情况:

方法二 一次遍历:

var maxProduct = function(nums) {
   let first=-1,second=-2
   for(let num of nums){
       if(num>first){
           second=first
           first=num
           continue
       }
       if(num>second){
           second=num
       }
   }
   return (first-1)*(second-1)
};

 消耗时间和内存情况:

最近更新

  1. TCP协议是安全的吗?

    2024-02-20 16:20:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-20 16:20:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-20 16:20:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-20 16:20:01       18 阅读

热门阅读

  1. Element UI组件的安装及使用

    2024-02-20 16:20:01       26 阅读
  2. kube-ovn自定义vpc

    2024-02-20 16:20:01       23 阅读
  3. 开源BLHELI-S 代码详细解读(四)

    2024-02-20 16:20:01       22 阅读
  4. Vue3中watch与watchEffect的区别

    2024-02-20 16:20:01       23 阅读
  5. OSS业务存储适配器模式

    2024-02-20 16:20:01       21 阅读
  6. python数据分析numpy基础之var求数组方差

    2024-02-20 16:20:01       26 阅读
  7. 缓存使用常见思路及问题

    2024-02-20 16:20:01       18 阅读
  8. BUG:required a single bean, but 2 were found:

    2024-02-20 16:20:01       22 阅读
  9. Prompt Engineering 提示工程教程详情

    2024-02-20 16:20:01       29 阅读