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

# Third Maximum Number Python Solution

> Tested Python solution for LeetCode 414 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 414, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/third-maximum-number/description/).

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

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

## Problem

You are given an integer array `nums`.

Return the third **distinct maximum** number in this array. If the third **maximum** does not exist, return the **maximum** number.

### Examples

```
Input: nums = [3,2,1]
Output: 1
```

**Explanation:** The first distinct maximum is 3. The second distinct maximum is 2. The third distinct maximum is 1.

```
Input: nums = [1,2]
Output: 2
```

**Explanation:** The first distinct maximum is 2. The second distinct maximum is 1. The third maximum does not exist, so the maximum (2) is returned instead.

```
Input: nums = [2,2,3,1]
Output: 1
```

**Explanation:** The first distinct maximum is 3. The second distinct maximum is 2 (both 2's are counted together since they have the same value). The third distinct maximum is 1.

### Constraints

* 1 \<= nums.length \<= 10^4
* -2^31 \<= nums\[i] \<= 2^31 - 1

**Follow up:** Can you find an `O(n)` solution?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def third_max(self, nums: list[int]) -> int:
        top: list[int] = []
        for num in nums:
            if num in top:
                continue
            top.append(num)
            top.sort(reverse=True)
            if len(top) > 3:
                top.pop()
        return top[2] if len(top) > 2 else top[0]
```

## Complexity

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

## Tags


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