Longest Increasing Subsequence
1 approach, code in every language
Problem description
You are given an integer array. A subsequence is any sequence you can obtain by deleting zero or more elements without changing the relative order of the elements that remain.
Return the length of the longest subsequence whose values are strictly increasing from left to right.
Example 1
Input: nums = [10, 9, 2, 5, 3, 7, 101, 18]
Output: 4
One longest strictly increasing subsequence is [2, 3, 7, 101], which has length 4. Other choices such as [2, 5, 7, 18] also reach length 4.
Example 2
Input: nums = [7, 7, 7, 7, 7]
Output: 1
Every value is equal, so no pair is strictly increasing. The best you can do is pick a single element for a length of 1.
Constraints
1 <= nums.length <= 2500-10^4 <= nums[i] <= 10^4
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Longest Rising Anomaly Pick is our own problem on the technique behind Longest Increasing Subsequence: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free