MediumArrayBinary SearchDynamic Programming

Longest Increasing Subsequence

LeetCode
1 approach, code in all languages

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
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now