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
Longest Increasing Subsequence ProblemInput: [10, 9, 2, 5, 3, 7, 101, 18]109253710118One possible LIS: [2, 3, 7, 101]Alternative LIS examples:• [2, 5, 7, 101] (length 4)• [2, 5, 7, 18] (length 4)• [2, 3, 7, 18] (length 4)Output: 4(Length of LIS)
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
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
87.5K Views
High Frequency
~25 min Avg. Time
1.8K 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