Skip to content
BytePatterns

Longest Rising Subsequence

MediumDynamic Programming#bottom-up-dp#subsequence-dp~30m

Problem

Given a list of numbers, pick some of them while keeping their original order so that every picked number is strictly larger than the one picked before it. Return the largest number of values such a pick can contain. The values do not have to be next to each other in the list, and an empty list gives 0.

Examples

Input:  nums = [4, 1, 3, 2, 5, 3, 6]
Output: 4
Why:    1, 3, 5, 6 rises four times; 1, 2, 3, 6 does too
Input:  nums = [5, 5, 5]
Output: 1
Why:    equal values do not count as rising
Input:  nums = []
Output: 0
Why:    edge case, nothing to pick

Hints

0 / 3

Stuck on the idea rather than the code? Longest Increasing Subsequence covers it.