> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Product of Three Numbers

> Tested Python solution for LeetCode 628 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 628, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/maximum-product-of-three-numbers/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 628   # by problem number
lcpy gen -s maximum_product_of_three_numbers   # by problem name
```

## Problem

You are given an integer array `nums`.

Find three numbers whose product is maximum and return the maximum product.

### Examples

```
Input: nums = [1,2,3]
Output: 6
Explanation: The only three numbers are 1, 2, and 3, so the maximum product is 1 * 2 * 3 = 6.
```

```
Input: nums = [1,2,3,4]
Output: 24
Explanation: The largest product comes from the three greatest numbers: 2 * 3 * 4 = 24.
```

```
Input: nums = [-1,-2,-3]
Output: -6
Explanation: The only three numbers are -1, -2, and -3, so the maximum product is (-1) * (-2) * (-3) = -6.
```

### Constraints

* `3 <= nums.length <= 10^4`
* `-1000 <= nums[i] <= 1000`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_product_of_three_numbers/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_product_of_three_numbers/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def maximum_product(self, nums: list[int]) -> int:
        max1 = max2 = max3 = -(10**18)
        min1 = min2 = 10**18
        for num in nums:
            if num > max1:
                max1, max2, max3 = num, max1, max2
            elif num > max2:
                max2, max3 = num, max2
            elif num > max3:
                max3 = num
            if num < min1:
                min1, min2 = num, min1
            elif num < min2:
                min2 = num
        return max(max1 * max2 * max3, max1 * min1 * min2)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.