628 Maximum Product of Three Numbers 三个数的最大乘积
Description:
Given an integer array, find three numbers whose product is maximum and output the maximum product.
Example:
Example 1:
Input: [1,2,3]
Output: 6
Example 2:
Input: [1,2,3,4]
Output: 24
Note:
The length of the given array will be in range [3,10^4] 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.
题目描述:
给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。
示例 :
示例 1:
输入: [1,2,3]
输出: 6
示例 2:
输入: [1,2,3,4]
输出: 24
注意:
给定的整型数组长度范围是[3,10^4],数组中所有的元素范围是[-1000, 1000]。
输入的数组中任意三个数的乘积不会超出32位有符号整数的范围。
思路:
有两种情况需要考虑, 如果有 3个以上的正数, 则最大乘积为3个最大的正数的乘积; 或者最大乘积是两个绝对值最大的负数和最大的正数的乘积
- 遍历列表, 参考LeetCode #414 Third Maximum Number 第三大的数
时间复杂度O(n), 空间复杂度O(1) - 对列表进行排序, 选择最后三个数或者最后一个数和开头两个数的最大乘积即可
时间复杂度O(nlgn), 空间复杂度O(1)
代码:
C++:
class Solution
{
public:
int maximumProduct(vector<int>& nums)
{
int max1 = -1000, max2 = -1000, max3 = -1000, min1 = 1000, min2 = 1000;
for (auto 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 max(max1 * max2 * max3, min1 * min2 * max1);
}
};
Java:
class Solution {
public int maximumProduct(int[] nums) {
int max1 = -1000, max2 = -1000, max3 = -1000, min1 = 1000, min2 = 1000;
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, min1 * min2 * max1);
}
}
Python:
class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])