Longest Increasing Subsequence

Medium#3001d array

You get an array of numbers. Pick some of them, keeping their order, so that each picked number is larger than the one before; the numbers need not stand side by side. How many can you pick at most? Our example: [4, 9, 5, 2, 6, 7, 3] — 4, for example 4, 5, 6, 7.

input
nums: an array of integers.
returns
The length of the longest strictly increasing subsequence of nums: numbers taken in their order, not necessarily next to each other, each greater than the one before.
constraints
  • 1 ≤ nums.length ≤ 1000
  • −10⁴ ≤ nums[i] ≤ 10⁴
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {number}
 */
function lengthOfLIS(nums) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more dynamic programming (dp) problems

Pattern “Dynamic programming (DP)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.