资讯专栏INFORMATION COLUMN

leetcode 628 Maximum Product of Three Numbers

CoreDump / 3380人阅读

摘要:题目详情输入一个大小大于等于三的数组,给出其中任意三个数乘积中的最大乘积想法这道题最主要的是要考虑正负数的情况。如果全都是正数相乘比较大,就取三个最大值相乘即可。

题目详情
Given an integer array, find three numbers whose product is maximum and output the maximum product.

输入一个大小大于等于三的数组,给出其中任意三个数乘积中的最大乘积

Example 1:
Input: [1,2,3]
Output: 6
Example 2:
Input: [1,2,3,4]
Output: 24

想法

这道题最主要的是要考虑正负数的情况。

如果全都是正数相乘比较大,就取三个最大值相乘即可。

如果负数的绝对值比较大,我们可以取绝对值最大的两个负数参与相乘,最后比较一下两种算法的乘积哪个大。

解法一 时间复杂度O(n)

这个解法写起来麻烦一点,判断条件比较多,但是时间复杂度为O(n)

    public int maximumProduct(int[] nums) {
        Integer max1 = Integer.MIN_VALUE;Integer max2 = max1;Integer max3 = max1;
        Integer min1 = Integer.MAX_VALUE;Integer min2 = min1;
        
        for(int num : nums){
            if(num > max1){
                max3 = max2;
                max2 = max1;
                max1 = num;
            }else if(num > max2){
                max3 = max2;
                max2 = num;
            }else if(num > max3){
                max3 = num;
            }
            
            if(num < min1){
                min2 = min1;
                min1 = num;
            }else if(num < min2){
                min2 = num;
            }
            
        }
        
        
        return Math.max(max1*max2*max3, max1*min1*min2);
    }
解法二 预排序

先对数组进行预排序的算法比较简洁,但是时间复杂度为O(nlogn)

    public int maximumProduct(int[] nums) {
        
         Arrays.sort(nums);
         int a = nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3];
         int b = nums[0] * nums[1] * nums[nums.length - 1];
         return a > b ? a : b;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68419.html

相关文章

  • [LeetCode] 628. Maximum Product of Three Numbers

    Problem Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24Note:The length of the ...

    jindong 评论0 收藏0
  • leetcode 部分解答索引(持续更新~)

    摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...

    leo108 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • [LeetCode/LintCode] Largest Palindrome Product

    Problem Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example Input: 2Output: 987Ex...

    Barry_Ng 评论0 收藏0
  • [LeetCode] 575. Distribute Candies

    Problem Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribu...

    djfml 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<