All topics
8 patterns · 40 problems

Dynamic Programming

Cache overlapping subproblems and build optimal answers from optimal sub-answers.

01Pattern

Basic Dynamic Programming

Solve each subproblem once, reuse the answer.

This patternO(n)naïve O(2^n)

When to reach for it

  • You encounter a problem where recursive calls result in redundant computations, leading to inefficiency. The problem can be optimized by storing the results of previous computations in a data structure.

Spot it in the prompt

Look for recursive problems where subproblems are overlapping or repetitive.

Easy Bespoke animation

Climbing Stairs

LeetCode
Loading animation…

How to solve it

Ways to reach step n equal the sum of ways to reach n-1 and n-2, so accumulate the running total forward.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

02Pattern

Optimal Substructure

Combine optimal sub-answers into the optimum.

When to reach for it

  • You encounter a problem where an optimal solution can be constructed from optimal solutions of its subproblems.

Spot it in the prompt

Look for problems where the optimal solution to the problem can be formed by combining optimal solutions of its subproblems.

Medium

Maximum Subarray

LeetCode
Loading animation…

How to solve it

Animation: Optimal Substructure technique

Kadane's algorithm extends or restarts the running sum at each index and tracks the best sum seen so far.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

03Pattern

Interval/Range DP

Solve small ranges, expand to larger ones.

This patternO(n^3)

When to reach for it

  • You encounter a problem where you need to find optimal solutions for subintervals within a larger interval.

Spot it in the prompt

Look for problems where the optimal solution for a given interval can be derived from optimal solutions of its subintervals.

Medium

Minimum Falling Path Sum

LeetCode
Loading animation…

How to solve it

Animation: Interval/Range DP technique

For each cell add the minimum of the three cells above it, then return the smallest value in the last row.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

04Pattern

Knapsack Problems

Pick items to optimize value under capacity.

This patternO(n * W)

When to reach for it

  • You encounter a problem where you need to optimize the allocation of resources to maximize or minimize a value, subject to capacity constraints.

Spot it in the prompt

Look for problems where you need to select items from a set to maximize or minimize a value without exceeding capacity constraints.

Medium

Last Stone Weight II

LeetCode
Loading animation…

How to solve it

Animation: Knapsack Problems technique

Split the stones into two piles as equal as possible — a 0/1 knapsack toward half the total — and the answer is the leftover difference.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

05Pattern

Prefix Sums

Precompute cumulative sums for O(1) ranges.

This patternO(n)naïve O(n^2)

When to reach for it

  • You encounter a problem where you need to efficiently compute cumulative sums or counts over a range of elements.

Spot it in the prompt

Look for problems where you can preprocess the array to compute prefix sums or counts, enabling fast query operations.

Medium

Subarray Sum Equals K

LeetCode
Loading animation…

How to solve it

Animation: Prefix Sums technique

Track running prefix sums in a hash map and count how often the sum minus K has appeared before.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

06Pattern

Counting Problems

Count ways by summing state transitions.

When to reach for it

  • You encounter a problem where you need to count the number of ways to achieve a certain outcome, subject to certain constraints.

Spot it in the prompt

Look for problems where you can define states representing counts, and transitions between states correspond to valid outcomes.

Hard

Unique Paths III

LeetCode
Loading animation…

How to solve it

Animation: Counting Problems technique

Backtrack over the grid counting paths that visit every empty cell exactly once before reaching the end.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

07Pattern

Interval Partitioning

Sort by endpoint, then partition greedily.

When to reach for it

  • You encounter a problem where you need to divide a set of intervals into the minimum number of non-overlapping subsets.

Spot it in the prompt

Look for problems where you can sort the intervals by their endpoints and use dynamic programming to find the optimal partitioning.

Medium

Non-overlapping Intervals

LeetCode
Loading animation…

How to solve it

Animation: Interval Partitioning technique

Sort by end time and greedily keep each interval that starts after the last kept end, removing the rest.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

08Pattern

Probability & Expectations

Propagate probabilities across DP states.

When to reach for it

  • You encounter a problem where you need to compute the probability of certain events or the expected value of a random variable.

Spot it in the prompt

Look for problems where you can model the problem using probability distributions or expected values and use dynamic programming to compute them efficiently.

Medium

Knight Probability in Chessboard

LeetCode
Loading animation…

How to solve it

Animation: Probability & Expectations technique

DP over remaining moves and cell, spreading each cell's probability equally across its eight knight jumps.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

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