# 628. Maximum Product of Three Numbers

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* 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:
The length of the given array will be in range [3,104] 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
******- 时间复杂度: O(NlgN)******- 空间复杂度: O(1)******

排好序，然后分了很多种情况，自己第一个版本，bug free

```python
class Solution(object):
    def maximumProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        if nums[-1] <= 0:
            return nums[-1] * nums[-2] * nums[-3]
        else:
            if nums[0] >= 0:
                return nums[-1] * nums[-2] * nums[-3]
            else:
                if nums[1] < 0:
                    return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])
                else:
                    return nums[-1] * nums[-2] * nums[-3]
```





> 思路 2
******- 时间复杂度: O(NlgN)******- 空间复杂度: O(1)******


后面发现根本不需要分那么多情况，上面所有情况合并其实就只有两种结果，要么nums[-1] * nums[-2] * nums[-3], 要么nums[0] * nums[1] * nums[-1]

beats 78.35%

```python
class Solution(object):
    def maximumProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])
```


> 思路 3
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******

用size为3的最小堆储存最大的3个数和size为2的最大堆储存最小的2个数

可以使得时间降到O(N)

beats 99.19%

```python
from heapq import *
class Solution(object):
    def maximumProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """    
        max_heap, min_heap = sorted(nums[:3]), [-i for i in sorted(nums[:3])[:2]]
        for num in nums[3:]:
            if num > max_heap[0]:
                heappop(max_heap)
                heappush(max_heap, num)
            if num < -min_heap[0]:
                heappop(min_heap)
                heappush(min_heap, -num)
        return max(max_heap[0] * max_heap[1] * max_heap[2], min_heap[0] * min_heap[1] * max(max_heap))
```

> 思路 4
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******

用更简单的函数, nlargest(k, nums)时间为O(N*lgk)，但是会稍微比思路3慢一点

beats 82.55%

```python
from heapq import *
class Solution(object):
    def maximumProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """    
        max_top3, min_top2 = heapq.nlargest(3, nums), heapq.nsmallest(2, nums)
        return max(max_top3[0] * max_top3[1] * max_top3[2], min_top2[0] * min_top2[1] * max_top3[0])
```

一行版本

```python
from heapq import *
class Solution(object):
    def maximumProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """    
        return max(nums) * max(x * y for x, y in [heapq.nsmallest(2, nums), heapq.nlargest(3, nums)[1:]])
```






























