2Brute2Best
πŸ”₯0

Open the Lock

MediumΒ·LeetCode #752 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You have a lock with 4 wheels. Each wheel shows a digit from 0 to 9. One turn moves one wheel by one slot. A wheel wraps around: turning 9 up gives 0, and turning 0 down gives 9.

The lock starts at 0000. Some codes are dead ends. If the wheels show a dead end, the lock jams and you cannot go on from there. The target code opens the lock.

Return the fewest turns needed to get from 0000 to the target without ever showing a dead end. If the target cannot be reached, return -1. (If 0000 itself is a dead end, the answer is -1.)

Input

deadends, a list of 4-digit codes that jam the lock. target, the 4-digit code that opens it.

Output

The fewest number of turns, or -1 if the target cannot be reached.

Example 1
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
Output: 6

The route 0000 β†’ 1000 β†’ 1100 β†’ 1200 β†’ 1201 β†’ 1202 β†’ 0202 has 6 turns and avoids every dead end. No shorter route exists.

Example 2
Input: deadends = ["8888"], target = "0009"
Output: 1

Turning the last wheel down from 0 gives 9 in one turn.

Example 3
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
Output: -1

All 8 codes one turn away from 8888 are dead ends, so the target is walled off.

Constraints
  • β€’ There are between 1 and 500 dead ends.
  • β€’ Every code has exactly 4 digits, and uses only the digits 0 to 9.
  • β€’ The target is never one of the dead ends.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œfewest turns”
β†’ Each turn costs 1, so the question is a shortest-path question. BFS gives the fewest turns directly.
β€œ4-digit codes”
β†’ A code is a state. Its 8 neighbours are the codes one wheel turn away, worked out on the spot rather than stored.
β€œdead ends”
β†’ Forbidden states. They are marked as seen before the search, so BFS never enters them.
03

Approach Complexity Ladder

Brute Force β€” try every turn sequence up to a limitO(8^K)O(K + D)

Try every sequence of turns with a limit of 0, 1, 2, … until one reaches the target

Optimal β€” BFS from 0000, one round per turnO(D)O(D)

BFS from 0000; each code is pushed once, and the dead ends are never entered

Brute Force Intuition

Choose a limit. Try every sequence of at most that many turns from 0000, never stepping onto a dead end. If one sequence reaches the target, the limit is the answer. If none does, raise the limit by 1 and search again.

Recognition rule for this sub-pattern:

Nodes are states (lock codes, words, board squares). Generate neighbours on the fly.
Remember this Β· Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.