286. Walls and Gates
Walls and Gates (LeetCode 286) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph BFS - Connected Components / Island Counting subpattern.
How to solve Walls and Gates
Walls and Gates is a BFS Connected Components problem, part of Graph Traversal Patterns (DFS & BFS). Use BFS for shortest path in unweighted graphs. Solve word ladder, rotting oranges, and matrix problems.
Work towards the solution in these steps:
- Think of each open cell as a vertex connected to its four non-wall neighbors.
- Starting a separate search from every empty room would repeat work; instead, begin from all gates together.
- Use a queue and process cells in increasing distance order so the first time an empty room is reached gives its nearest-gate distance.
The reference solution runs in O(m * n) time and O(m * n) space.
The Walls and Gates solution and editorial walks through the full approach with reference code.
Problem statement
Implement Solution.wallsAndGates(rooms). The parameter rooms is a rectangular grid, where each cell represents one room or barrier. Cells with value -1 are walls and cannot be entered, cells with value 0 are gates, and cells with value 2147483647 are empty rooms whose distances have not yet been filled.
Movement is allowed only between horizontally or vertically adjacent cells, and walls cannot be crossed. Modify rooms in place so that every empty room contains the minimum number of moves needed to reach any gate. An empty room with no possible route to a gate must remain 2147483647.
The method returns nothing; the required answer is the final contents of rooms after the in-place modification.
Example 1:
Input: rooms = [[2147483647,-1,0],[2147483647,2147483647,2147483647],[-1,2147483647,-1]] Output: [[4,-1,0],[3,2,1],[-1,3,-1]] Explanation: The gate at the top right reaches the open cells through the lower corridor. The wall cells are unchanged.
Example 2:
Input: rooms = [[2147483647,-1],[2147483647,2147483647]] Output: [[2147483647,-1],[2147483647,2147483647]] Explanation: There is no gate, so none of the empty rooms can receive a finite distance.
Example 3:
Input: rooms = [[0,2147483647,2147483647],[2147483647,-1,2147483647],[2147483647,2147483647,0]] Output: [[0,1,2],[1,-1,1],[2,1,0]] Explanation: Distances are measured from the nearer of the two gates, while the center wall remains -1.
Constraints:
- 1 <= rooms.length <= 250 - 1 <= rooms[0].length <= 250 - Every row in rooms has the same length as rooms[0].length - Each rooms[i][j] is one of -1, 0, or 2147483647 - The grid uses four-directional adjacency: up, down, left, and right
Topics and companies
Topics: Breadth-First Search, Matrix, Queue, Shortest Path.
How to practise Walls and Gates on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m * n) time and O(m * n) space.
Open Walls and Gates in the code editor, or read the Walls and Gates editorial for a worked solution with its approach and complexity analysis.
Where Walls and Gates sits in the DSA pattern sheet
- Graph Traversal Patterns (DFS & BFS) pattern guide
- Graph BFS - Connected Components / Island Counting subpattern
- The full DSA patterns sheet
Problems related to Walls and Gates
Other problems that use the same Graph BFS - Connected Components / Island Counting and Graph Traversal Patterns (DFS & BFS) techniques:
- 01 Matrix — Medium, same subpattern
- Rotting Oranges — Medium, same subpattern
- Shortest Path in Binary Matrix — Medium, same subpattern
- Word Ladder — Hard, same subpattern
- Accounts Merge — Medium, same pattern
- Alien Dictionary — Hard, same pattern
Walls and Gates FAQ
What is the LeetCode number of Walls and Gates?
Walls and Gates is LeetCode problem 286, rated Medium.
What pattern is used to solve Walls and Gates?
Walls and Gates is a BFS Connected Components problem. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph BFS - Connected Components / Island Counting subpattern.
What is the time complexity of Walls and Gates?
The reference solution runs in O(m * n) time and O(m * n) space.