Skip to content
MediumDpAI interview only

Longest Increasing Subsequence

Asked atgoogleamazonmetamicrosoftappleuber

01 · Problem

Given an integer array nums, return the length of the longest strictly increasing subsequence.

A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements.

02 · Examples

Example 01
Input
nums = [10,9,2,5,3,7,101,18]
Output
4

The longest increasing subsequence is [2,3,7,101] with length 4. Other valid LIS include [2,5,7,101] and [2,5,7,18].

Example 02
Input
nums = [0,1,0,3,2,3]
Output
4

One valid LIS is [0,1,2,3]. Note that elements don't need to be contiguous.

03 · Constraints

  • 011 <= nums.length <= 2500
  • 02−104 <= nums[i] <= 104

04 · Optimal complexity

Time
O(n log n)
Space
O(n)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.