Longest Increasing Subsequence - Problem

Given an integer array nums, you need to find the length of the longest strictly increasing subsequence.

A subsequence is a sequence that can be derived from the original array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of [0,3,1,6,2,2,7].

An increasing subsequence means each element is strictly greater than the previous one. Your goal is to find the maximum possible length of such a subsequence.

Example: In array [10,9,2,5,3,7,101,18], one possible longest increasing subsequence is [2,3,7,18] with length 4.

Input & Output

example_1.py — Basic Case
$ Input: [10,9,2,5,3,7,101,18]
Output: 4
💡 Note: The longest increasing subsequence is [2,3,7,101] or [2,3,7,18], both have length 4. There are multiple valid subsequences of the same maximum length.
example_2.py — All Decreasing
$ Input: [7,7,7,7,7,7,7]
Output: 1
💡 Note: All elements are equal, so the longest strictly increasing subsequence has length 1 (any single element).
example_3.py — Already Sorted
$ Input: [1,3,6,7,9,4,10,5,6]
Output: 6
💡 Note: The longest increasing subsequence is [1,3,4,5,6] or [1,3,6,7,9,10], both with length 6. We can pick elements in any order as long as they maintain increasing property.

Visualization

Tap to expand
Building the Longest Increasing StaircaseAvailable Blocks: 10, 9, 2, 5, 3, 7, 101, 18Step 1-3: Build base with smallest blocks2Step 4: Add next level23Step 5: Continue building237Final: Tallest staircase23718Height: 4🎯 Key StrategyAlways keep the smallest possible 'top block' for each heightThis maximizes your chances of adding more blocks laterBinary search helps find the right position quickly!
Understanding the Visualization
1
Start with First Block
Place the first block as the foundation of your staircase
2
Process Each Block
For each new block, decide whether to start a new staircase or extend an existing one
3
Make Optimal Choice
Always keep the smallest possible 'top block' for each staircase height to maximize future options
4
Find Maximum Height
The height of your tallest staircase is the answer
Key Takeaway
🎯 Key Insight: By maintaining the smallest tail for each possible length, we create maximum opportunities for future extensions while using binary search for efficiency.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

For each of n elements, we perform binary search on tails array which can be at most n elements long

n
2n
Linearithmic
Space Complexity
O(n)

Additional tails array that stores at most n elements in worst case

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 2500
  • -104 ≤ nums[i] ≤ 104
  • Follow up: Can you come up with an O(n log n) solution?
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 22
48.8K Views
High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen