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

# Longest Increasing Subsequence Python Solution

> Tested Python solution for LeetCode 300 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 300, Medium. Topics: Array, Binary Search, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/longest-increasing-subsequence/description/).

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

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

## Problem

Given an integer array `nums`, return the length of the longest **strictly increasing** **subsequence**.

### Examples

```
Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
```

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

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

### Constraints

* `1 <= nums.length <= 2500`
* `-10^4 <= nums[i] <= 10^4`

**Follow up:** Can you come up with an algorithm that runs in `O(n log(n))` time complexity?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def length_of_lis(self, nums: list[int]) -> int:
        """
        Binary Search + DP: tails[i] = smallest tail of all LIS of length i+1

        Example with middle replacement: [1,5,3,7,2,6,4,8]

        Step | num | tails        | Action
        -----|-----|--------------|------------------
          1  |  1  | [1]          | append
          2  |  5  | [1,5]        | append
          3  |  3  | [1,3]        | replace 5 (pos=1)
          4  |  7  | [1,3,7]      | append
          5  |  2  | [1,2,7]      | replace 3 (pos=1)
          6  |  6  | [1,2,6]      | replace 7 (pos=2)
          7  |  4  | [1,2,4]      | replace 6 (pos=2)
          8  |  8  | [1,2,4,8]    | append

        Result: len(tails) = 4
        """
        import bisect

        tails: list[int] = []

        for num in nums:
            pos = bisect.bisect_left(tails, num)
            if pos == len(tails):
                tails.append(num)
            else:
                tails[pos] = num

        return len(tails)
```

## Complexity

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

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
