Skip to main content
mediumBlind 75

Longest Increasing Subsequence

Find length of longest strictly increasing subsequence Example: [10,9,2,5,3,7,101,18] => 4

Longest Increasing Subsequence is a dynamic programming question: define the state cleanly and the transitions usually follow. Interviewers love it because it reveals your state-and-transition thinking and your edge-case discipline. Once you define the right state, the transitions usually write themselves.

Examples

You write

longest_increasing_subsequence(nums: List[int])

  1. Example 1

    Input
    [10,9,2,5,3,7,101,18]
    returns
    4
  2. Example 2

    Input
    [0,1,0,3,2,3]
    returns
    4

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Binary Search

I reach for it when

  • The input is sorted, or sorted and then rotated.
  • You want a boundary: the first or last place something is true.

Say this before you type. Say the yes-or-no question, and which side each answer throws away.

All 19 Binary Search problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Longest Increasing Subsequencemedium