Advertisement

Open the Lock - LeetCode 752 Solution

Open the Lock - Complete Solution Guide

Open the Lock is LeetCode problem 752, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' . The wheels can rotate freely and wrap around: for example we can turn '9' to be '0' , or '0' to be '9' . Each move consists of turning one wheel one slot. The lock initially starts at '0000' , a string representing the state of the 4 wheels. You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop

Detailed Explanation

The problem asks us to find the minimum number of moves (turns of the wheels) required to transform a 4-digit lock from its initial state '0000' to a given 'target' state. We are given a list of 'deadends', which represent combinations that, if encountered, prevent the lock from being opened. The lock has 4 wheels, each with digits 0-9, and each move consists of turning one wheel one digit forward or backward (wrapping around, so 9 goes to 0 and 0 goes to 9). The target will not be in the deadends, and the target and deadends will only contain digits.

Solution Approach

The provided code utilizes Bidirectional Breadth-First Search (BFS) to find the shortest path from the initial state '0000' to the target state while avoiding deadends. Two queues are used, one starting from '0000' and the other starting from 'target'. At each step, the algorithm expands the queue with fewer elements to minimize the search space. The algorithm explores all possible neighbor states by incrementing or decrementing each digit. If a neighbor state is found in the visited states of the other queue, it means a path has been found, and the sum of the distances from both sides gives the result. If the target is not reachable without hitting a deadend, the function returns -1.

Step-by-Step Algorithm

  1. Step 1: Initialize a set of deadends for fast lookup.
  2. Step 2: Check if the initial state '0000' is a deadend. If so, return -1 because the lock cannot be opened.
  3. Step 3: Check if the target is '0000'. If so, return 0 because no moves are needed.
  4. Step 4: Initialize two queues, q1 starting at '0000' and q2 starting at the 'target'.
  5. Step 5: Initialize two hash maps (or sets), visited1 and visited2, to keep track of the visited states and their distances from their respective starting points.
  6. Step 6: While both queues are not empty, expand the smaller queue.
  7. Step 7: For each state in the current queue, generate all possible neighbor states by incrementing/decrementing each digit.
  8. Step 8: For each neighbor state, check if it has been visited by the other search. If so, a path has been found; return the combined distance.
  9. Step 9: If the neighbor state is not a deadend and has not been visited by the current search, add it to the queue and mark it as visited.
  10. Step 10: If both queues become empty without finding a path, it implies that the target is unreachable; return -1.

Key Insights

  • Insight 1: Breadth-First Search (BFS) is the optimal algorithm for finding the shortest path in an unweighted graph. Each lock state can be thought of as a node, and each possible move (turning a wheel) is an edge.
  • Insight 2: The lock states can be represented as strings and stored in a set or hash table to quickly check for deadends and visited states.
  • Insight 3: Bidirectional BFS can significantly improve performance by searching from both the start and the end simultaneously. This reduces the search space.

Complexity Analysis

Time Complexity: O(10000)

Space Complexity: O(10000)

Topics

This problem involves: Array, Hash Table, String, Breadth-First Search.

Companies

Asked at: ByteDance, CARS24, Citadel, Coupang, DE Shaw, Flipkart, J.P. Morgan, Zepto, eBay.