0%

maximum-product-of-three-numbers

Maximum Product of Three Numbers – LeetCode 628

Problem

Description

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

Answer

Original

Code

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

思路

直接排序然后针对负数情况进行处理。时间复杂度$O(nlog(n))$,空间复杂度$O(1)$。
耗时$44$ ms,排名$40.39\%$

Better

思路

还没看到更好的思路