DEV Community

Cover image for 10 Patterns That Unlocked 80% of LeetCode for Me
ALI
ALI

Posted on

10 Patterns That Unlocked 80% of LeetCode for Me

LeetCode taught me one thing: if it looks hard, it’s probably a pattern you’ve seen before…or DP

When I first opened LeetCode, I thought I was a genius for solving “Two Sum.”
Three hours later, “Container With Most Water” had me rethinking all my life choices.
After grinding through hundreds of problems (and about the same number of mental breakdowns), I realized something:
LeetCode isn’t infinite — it just keeps repeating itself in clever ways.
Once I spotted the patterns, 80% of problems became… not easy, but at least less painful.

Here are the 10 that changed everything for me:

1. Sliding Window

If you see words like “subarray” or “substring,” this is probably your guy.
Two pointers walk into a bar, and one keeps moving until you forget why you started.

Examples:

  • Longest Substring Without Repeating Characters
  • Maximum Sum Subarray of Size K

2. Two Pointers

Usually sorted arrays, sometimes strings.
Move one pointer up, one down, and pray you don’t create an infinite loop.

Examples:

  • 3Sum
  • Remove Duplicates from Sorted Array

3. Fast and Slow Pointers

One pointer takes a stroll, the other runs like it’s late for a flight.
They somehow meet — usually where the bug is.

Examples:

  • Linked List Cycle
  • Find Middle of Linked List

4. Binary Search (on the answer)

Not just for sorted arrays — use it on the range of possible answers.
Once you get this, you’ll start seeing binary search in random places, like your grocery list.

Examples:

  • Koko Eating Bananas
  • Ship Packages Within D Days

5. Prefix Sum

When you’re done adding the same numbers a hundred times.
Compute once, use forever.

Examples:

  • Subarray Sum Equals K
  • Range Sum Query

6. HashMap Everything

If it moves, map it. If it doesn’t, still map it.
Half of LeetCode is just “figure out what to hash.”

Examples:

  • Two Sum
  • Group Anagrams

7. DFS / BFS

Grids, trees, graphs — choose your suffering.
DFS: dive deep.
BFS: stay shallow.
Either way, you’re getting lost.

Examples:

  • Number of Islands
  • Word Ladder

8. Dynamic Programming (a.k.a. organized pain)

It’s not scary once you realize it’s just recursion with memory.
Still confusing, but at least you know why it’s confusing.

Examples:

  • Climbing Stairs
  • House Robber

Longest Increasing Subsequence

9. Backtracking

When brute force is too obvious, but you still need all possible answers.
Think of it as recursion with regret management.

Examples:

  • N-Queens
  • Subsets
  • Combination Sum

10. Topological Sort

If the question sounds like “Course Schedule” or “Build Order,” congrats — you’re here.
It’s just dependency resolution with extra steps.

Examples:

  • Course Schedule
  • Alien Dictionary
  • The Real Trick

I stopped memorizing every solution.
Instead, I started asking, “Which pattern is this pretending to be?”

And suddenly, LeetCode stopped feeling like 2,000 unique problems —
and started feeling like 10 problems in disguise.

Still hate DP though. Some wounds never heal.


Top comments (0)