[leetcode]628. Maximum Product of Three Numbers

题目链接:https://leetcode.com/problems/maximum-product-of-three-numbers/#/description

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

Note:

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


方法一:

The first solution could be to sort the given numsnums array(in ascending order) and find out the product of the last three numbers. But, we can note that this product will be maximum only if all the numbers in numsnums array are positive. But, in the given problem statement, negative elements could exist as well. Thus, it could also be possible that two negative numbers lying at the left extreme end could also contribute to lead to a larger product if the third number in the triplet being considered is the largest positive number in the numsnums array. Thus, either the product nums[0]nums[0]xnums[1]nums[1]xnums[n-1]nums[n1] or nums[n-3]nums[n3]xnums[n-2]nums[n2]xnums[n-1]nums[n1] will give the required result. Thus, we need to find the larger one from out of these values.

class Solution {
public:
    int maximumProduct(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int res=max(nums[0]*nums[1]*nums[nums.size()-1],nums[nums.size()-3]*nums[nums.size()-2]*nums[nums.size()-1]);
        return res;
    }
};


方法二:

We need not necessarily sort the given numsnums array to find the maximum product. Instead, we can only find the required 2 smallest values(min1min1 and min2min2) and the three largest values(max1, max2, max3max1,max2,max3) in the numsnums array, by iterating over the numsnums array only once. At the end, again we can find out the larger value out of min1min1xmin2min2xmax1max1 and max1max1xmax2max2xmax3max3 to find the required maximum product.

class Solution {
public:
    int maximumProduct(vector<int>& nums) {
        int min1=INT32_MAX,min2=INT32_MAX;
        int max1=INT32_MIN,max2=INT32_MIN,max3=INT32_MIN;
        for(auto n:nums)
        {
            if(n<=min1)
            {
                min2=min1;
                min1=n;
            }
            else if(n<=min2)
            {
                min2=n;
            }
            if(n>=max1)
            {
                max3=max2;
                max2=max1;
                max1=n;
            }
            else if(n>=max2)
            {
                max3=max2;
                max2=n;
            }
            else if(n>=max3)
            {
                max3=n;
            }
        }
        return max(min1*min2*max1,max1*max2*max3);
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值