Algorithm-the maximum product of three numbers (TopK problem with small K)

1. The maximum product of three numbers

This question comes from: 628. The maximum product of three numbers

Difficulty is easy.

给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。

示例 1:

输入: [1,2,3]
输出: 6
示例 2:

输入: [1,2,3,4]
输出: 24
注意:

给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000]。
输入的数组中任意三个数的乘积不会超出32位有符号整数的范围。

The problem is easy to understand. It is to find the maximum value of the product of three numbers in the array. We find the product of the smallest two numbers and the largest one digit. Compared with the largest three-digit product, select the largest element.

So the problem becomes the problem of choosing TopK. Of course, we can use a heap to solve this problem. Build a minimum heap with a capacity of 2, and a maximum heap with a capacity of 3 (both small top heaps) to solve this problem. However, given that the number of elements to be selected is very small, using advanced data structures is a bit of a sledgehammer.

So we can manually find the largest and smallest elements.

    public int maximumProduct(int[] nums) {
    
    
        int min1=Integer.MAX_VALUE,min2=Integer.MAX_VALUE;
        int max1=Integer.MIN_VALUE,max2=Integer.MIN_VALUE,max3=Integer.MIN_VALUE;
        for(int 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 Math.max(min1*min2*max1,max1*max2*max3);
    }

2. Find the three largest numbers

When this question is a face byte, a question is given. The conventional solutions include heap method and quick selection method. The disadvantage is that the writing method is complicated (in fact, it is not complicated...). The solution I gave was similar to the solution below.

    //求最大的3个元素
    public int[] top3(int[] nums){
    
    
        int max1=Integer.MIN_VALUE,max2=Integer.MIN_VALUE,max3=Integer.MIN_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;
            }
        }
        int[] result={
    
    max3,max2,max1};
        return result;
    }

Guess you like

Origin blog.csdn.net/qq_23594799/article/details/105938721