# Maximum Product of Three Numbers

<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.

## Thoughts

最大的乘积要不就是三个正数, 要不就是俩负一正. 前者直接取最大的三个数, 后者取最小的两个数和最大的一个数.

## Code

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

## Analysis

O(nlgn)用于排序. 也可以O(N)来找多次最大或最小.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hao-fu-1.gitbook.io/oj/math/maximum-product-of-three-numbers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
