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.
Constraints
- 1 ≤ nums.length ≤ 2500
- -104 ≤ nums[i] ≤ 104
- Follow up: Can you come up with an O(n log n) solution?
Visualization
Tap to expand
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code