Fill Rooms with Their Nearest Gate Distances

Quick Overview

Fill Rooms with Their Nearest Gate Distances. Modify rooms in place. A cell is -1 for a wall, 0 for a gate, or 2147483647 for an empty room. Replace each reachable empty room by its shortest horizontal/vert

Fill Rooms with Their Nearest Gate Distances

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Modify rooms in place. A cell is -1 for a wall, 0 for a gate, or 2147483647 for an empty room. Replace each reachable empty room by its shortest horizontal/vertical distance to a gate. Unreachable rooms keep 2147483647; gates and walls remain unchanged. Preserve the void matrix-mutation interface. ```cpp void wallsAndGates(vector<vector<int>>& rooms); ``` ### Examples ```text rooms = [[0,2147483647,-1],[2147483647,2147483647,-1]] -> [[0,1,-1],[1,2,-1]] rooms = [[2147483647,-1,0]] -> [[2147483647,-1,0]] ``` ### Constraints - Both dimensions are between 1 and 250. - Initial entries are only -1, 0 or 2147483647. - Finite nearest-gate distances are smaller than 2147483647.

Overview: Fill Rooms with Their Nearest Gate Distances. Modify rooms in place. A cell is -1 for a wall, 0 for a gate, or 2147483647 for an empty room. Replace each reachable empty room by its shortest horizontal/vert

Amazon logo
Amazon
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Modify rooms in place. A cell is -1 for a wall, 0 for a gate, or 2147483647 for an empty room. Replace each reachable empty room by its shortest horizontal/vertical distance to a gate. Unreachable rooms keep 2147483647; gates and walls remain unchanged. Preserve the void matrix-mutation interface.

void wallsAndGates(vector<vector<int>>& rooms);

Examples

rooms = [[0,2147483647,-1],[2147483647,2147483647,-1]] -> [[0,1,-1],[1,2,-1]]
rooms = [[2147483647,-1,0]] -> [[2147483647,-1,0]]

Constraints

  • Both dimensions are between 1 and 250.
  • Initial entries are only -1, 0 or 2147483647.
  • Finite nearest-gate distances are smaller than 2147483647.