• 三个数的最大乘积 leetcode


    方法一:排序
    我们将数组进行升序排序,如果数组中所有的元素都是非负数,那么答案即为最后三个元素的乘积。

    如果数组中出现了负数,那么我们还需要考虑乘积中包含负数的情况,显然选择最小的两个负数和最大的一个正数是最优的,即为前两个元素与最后一个元素的乘积。

    上述两个结果中的较大值就是答案。注意我们可以不用判断数组中到底有没有正数,0 或者负数,因为上述两个结果实际上已经包含了所有情况,最大值一定在其中。

    public class Solution {
        public int maximumProduct(int[] nums) {
            Arrays.sort(nums);
            return Math.max(nums[0] * nums[1] * nums[nums.length - 1], nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3]);
        }
    }

    复杂度分析

    时间复杂度:O(NlogN),其中 NN 是数组的长度。

    空间复杂度:O(logN),为排序使用的空间。

    方法二:线性扫描

    在方法一中,我们实际上只要求出数组中最大的三个数以及最小的两个数,因此我们可以不用排序,用线性扫描直接得出这五个数。

    public class Solution {
        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) {     // n lies between min1 and min2
                    min2 = n;
                }
                if (n >= max1) {            // n is greater than max1, max2 and max3
                    max3 = max2;
                    max2 = max1;
                    max1 = n;
                } else if (n >= max2) {     // n lies betweeen max1 and max2
                    max3 = max2;
                    max2 = n;
                } else if (n >= max3) {     // n lies betwen max2 and max3
                    max3 = n;
                }
            }
            return Math.max(min1 * min2 * max1, max1 * max2 * max3);
        }
    }

    复杂度分析

    • 时间复杂度:O(N)

    • 空间复杂度:O(1)

  • 相关阅读:
    表连接问题
    public interface Serializable?标记/标签接口
    4.21
    第十周周记
    测试
    第九周周记
    第七周周记
    fighting.
    fighting
    作业一
  • 原文地址:https://www.cnblogs.com/ziytong/p/12987979.html
Copyright © 2020-2023  润新知