CodingNodes / 1-D Dynamic Programming / Longest Increasing Subsequence System design Space play · ←→ step · R restart

Problem

Given an array nums, pick some of its numbers, keeping their original order, so that each picked number is strictly bigger than the one before it. The numbers do not need to sit next to each other.

Return the greatest number of numbers you can pick.

Constraints

  • 1 ≤ nums.length ≤ 10
  • -100 ≤ nums[i] ≤ 100

Examples: pick one to animate

Solutions

TimeO(n²) SpaceO(n)

Time: Each index is compared with every index before it.

Space: The dp array holds one value per index.

dp[i] is the longest increasing run that ends at index i. For each earlier index j with a smaller number, a run ending at j can be extended by nums[i], so try dp[j] + 1. The answer is the largest dp value.