Tap here to switch tabs
Problem
Submissions

Problem: Walls and Gates

med
30 min
Try to solve the Walls and Gates problem.

Statement

You are given a rectangular grid rooms of size m by n where each cell represents one of the following:
a wall with value 1-1
a gate with value 00
an empty room with value 23112^{31} - 1

Update rooms in place so that each empty room contains the distance to its nearest gate, measured as the minimum number of moves between adjacent cells sharing a side. If an empty room cannot reach any gate, its value must remain 23112^{31} - 1. Walls and gates must remain unchanged.

Note: Modify rooms in place and do not return anything.

Constraints:

  • m == rooms.length

  • n == rooms[i].length

  • 11 \leq m, n 250\leq 250

  • rooms[i][j] is 1-1, 00, or 23112^{31} - 1

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Walls and Gates

med
30 min
Try to solve the Walls and Gates problem.

Statement

You are given a rectangular grid rooms of size m by n where each cell represents one of the following:
a wall with value 1-1
a gate with value 00
an empty room with value 23112^{31} - 1

Update rooms in place so that each empty room contains the distance to its nearest gate, measured as the minimum number of moves between adjacent cells sharing a side. If an empty room cannot reach any gate, its value must remain 23112^{31} - 1. Walls and gates must remain unchanged.

Note: Modify rooms in place and do not return anything.

Constraints:

  • m == rooms.length

  • n == rooms[i].length

  • 11 \leq m, n 250\leq 250

  • rooms[i][j] is 1-1, 00, or 23112^{31} - 1

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths