628.Maximum Product Of Three Numbers

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

Note: The length of the given array will be in range [3,104] and all
elements are in the range [-1000, 1000]. Multiplication of any three
numbers in the input won’t exceed the range of 32-bit signed integer.

题目描述:给一个整数数组,找到三个整数之积最大的值并输出

思路:这道题我上去一看这不是非常简单,然后看了一下注意,原来还有负数,差点踩坑

我的思路是首先将数组排序,然后判断最大值的正负,如果为正,那么只需要比较最小的两个值和第二大以及第三大之间积的大小,如果为0那么直接返回0
如果最大值为负数,那么只需要返回最后三个数的积就可以了

时间复杂度为 nlog2 n

通过的时间比较长,所以看了最优解,方法跟414 题的思路一样 在循环中记录最大的三个值 以及最小的两个值
这里的只找最小的两个值需要make sense,
例如[min1 =-9, min2=-8, max3=1, max2=2, max1=5]。我们需要使用的唯一条件min1 and min2是这两个是否定的。
我们必须得到一个积极的结果,只有一个解决方案:--+ (min1*min2*max1),然后比较+++ (max1*max2*max3)找到最大的一个。
在对输入数组进行排序并讨论特殊情况之后,将很容易想到:
1、所有数字> 0 || 一个数字<0且其余> 0
2、两个或更多<0且其余> 0
3、所有数字<0


 public int maximumProduct(int[] nums) {
        Arrays.sort(nums);
        int len = nums.length;
        int a = nums[0], b = nums[1], c = nums[2];
        int d = nums[len - 3], e = nums[len - 2], f = nums[len - 1];
        if (f > 0) {
            return Math.max(f * a * b, f * d * e);
        } else if (f == 0) {
            return 0;
        } else {
            return f * d * e;
        }

        /*
         int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE, min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        for (int n : nums) {
            if (n > max1) {
                max3 = max2;
                max2 = max1;
                max1 = n;
            } else if (n > max2) {
                max3 = max2;
                max2 = n;
            } else if (n > max3) {
                max3 = n;
            }

            if (n < min1) {
                min2 = min1;
                min1 = n;
            } else if (n < min2) {
                min2 = n;
            }
        }
        return Math.max(max1*max2*max3, max1*min1*min2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值