Longest Increasing Subsequence - Problem
Given an integer array nums, return the length of the longest strictly increasing subsequence.
A subsequence is a sequence that can be derived from an 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 the array [0,3,1,6,2,2,7].
Input & Output
Example 1 — Basic Case
$
Input:
nums = [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] or [2,5,7,101] or [2,5,7,18], all with length 4.
Example 2 — All Decreasing
$
Input:
nums = [0,1,0,3,2,3]
›
Output:
4
💡 Note:
The longest increasing subsequence is [0,1,2,3] with length 4.
Example 3 — All Same Elements
$
Input:
nums = [7,7,7,7,7,7,7]
›
Output:
1
💡 Note:
Since all elements are the same, the longest strictly increasing subsequence has length 1.
Constraints
- 1 ≤ nums.length ≤ 2500
- -104 ≤ nums[i] ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with elements in any order
2
Process
Find longest subsequence where each element is strictly greater than the previous
3
Output
Return the length of the longest increasing subsequence
Key Takeaway
🎯 Key Insight: Use binary search to maintain smallest tails for optimal O(n log n) solution
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code