You are given an m x n grid where each cell holds one of three characters: 'O' for a robot, 'E' for an empty space, and 'X' for a blocker. The grid boundary itself also acts as a blocker. You are also given a length-4 integer array distance = [left, top, bottom, right].
For each robot, compute its distance to the nearest blocker in each of the four cardinal directions. The "distance" is the number of steps from the robot to the blocker, counting the blocker (or boundary) as one step beyond the last walkable cell. Robots themselves are walkable — only 'X' and the boundary block.
Return the coordinates [row, col] of every robot whose four distances exactly match distance. Return the matches in row-major scan order (top to bottom, left to right within each row).
'O', compute four blocker distances.'O') are NOT blockers — keep walking past them.left = 1.1 <= m, n and the board has at least one cell.'O', 'E', 'X'.distance.length == 4, integers >= 1.Example 1:
` Input: board = [["O","E","E","E","X"], ["E","O","X","X","X"], ["E","E","E","E","E"], ["X","E","O","E","E"], ["X","E","X","E","X"]] distance = [2,2,4,1]
Output: [[1,1]] `
Example 2:
` Input: board = [["O","E","X","O","O"], ["E","O","X","O","X"], ["X","X","O","E","E"], ["E","O","E","O","E"], ["O","O","X","O","O"]] distance = [2,1,2,4]
Output: [[3,1]] `
Example 3:
` Input: board = [["O","X","O"], ["E","O","X"], ["O","X","O"]] distance = [1,1,1,1]
Output: [[0,2],[2,2]] `
Hint 1: One pass per direction For every robot, walk outward in each of the four cardinal directions until you hit either an
'X'or step off the grid. Count the number of steps taken — that is the blocker distance for that direction. Don't stop on'O'; only'X'and the boundary act as blockers.
Hint 2: Order matters Iterate the grid row-by-row, left-to-right. Append matches to your result list in that order. The expected outputs follow row-major scan order — even when more than one robot matches, the relative order of the returned coordinates should reflect their grid position.
Full Solution ` def findRobots(board, distance): m, n = len(board), len(board[0]) target_left, target_top, target_bottom, target_right = distance
def steps_to_blocker(r, c, dr, dc): steps = 0 while True: r, c = r + dr, c + dc steps += 1 if r < 0 or r >= m or c < 0 or c >= n: return steps if board[r][c] == 'X': return steps result = [] for r in range(m): for c in range(n): if board[r][c] != 'O': continue left = steps_to_blocker(r, c, 0, -1) top = steps_to_blocker(r, c, -1, 0) bottom = steps_to_blocker(r, c, 1, 0) right = steps_to_blocker(r, c, 0, 1) if (left, top, bottom, right) == (target_left, target_top, target_bottom, target_right): result.append([r, c]) return result`
Approach:
- Direction walker. Write a tiny helper that takes a starting cell and a step vector
(dr, dc), and returns the number of steps taken before hitting'X'or the edge. Each step counts — the blocker itself is the last counted step.- Row-major scan. Iterate
routermost,cinnermost. The first matching robot is the top-left match; appending in this order produces the required output ordering naturally.- Compare against the target tuple. Pack the four computed distances into a tuple and compare element-wise against the target.
Time Complexity:
O(m * n * (m + n))worst case — every robot walks at mostm + ncells across the four directions. For typical interview-sized grids this is fast enough.Space Complexity:
O(k)for the output list ofkmatches;O(1)auxiliary.Edge cases:
- Robots adjacent to a boundary correctly report
1for that direction.- Robots-on-robots: when walking past another robot, the helper keeps going, since
'O'is not a blocker.- A
1x1grid containing'O'has all four distances equal to1(every direction immediately hits the boundary).