5 patterns · 23 problems

Binary Search

Halve the search space every step — on sorted arrays, and on answers, ranges, and allocations.

Each pattern below starts with when to reach for it and the clue in the prompt that gives it away. Then it runs on real problems: pick one to watch the technique step by step, open its full solution, or practise it on an original problem with graded tests.

03Pattern

Allocation Problems

Search the smallest max that a greedy split allows.

This patternO(n log(range))

When to reach for it

  • Involves dividing resources or items among multiple entities while optimizing a certain criterion.

Spot it in the prompt

Look for scenarios where resources need to be distributed among entities with certain constraints, and the goal is to optimize a certain criterion, such as minimizing the maximum or maximizing the minimum.

Medium

Koko Eating Bananas

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Allocation Problems technique

Binary search the eating speed; for each candidate rate sum the hours needed and shrink toward the smallest rate that still finishes in time.

Overnight Print Rate is our own problem on the same technique: a new statement, graded in your browser.

Practice · 4 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

04Pattern

Counting Occurrences

Find boundaries with binary search, then subtract indices.

This patternO(log n)naïve O(n)

When to reach for it

  • Involves counting the number of occurrences of a specific value or satisfying a certain condition.

Spot it in the prompt

Look for scenarios where you need to count occurrences or determine the frequency of certain elements/values in a sorted array or range.

Medium

Find First and Last Position of Element in Sorted Array

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Counting Occurrences technique

Run two binary searches for the leftmost and rightmost target indices; their difference gives the occurrence range.

Deviation Value Span is our own problem on the same technique: a new statement, graded in your browser.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

After the pattern

Now solve one you haven't seen.

Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.

1,194 problems · 15,062 graded tests · free