Laser Robot Safe Path
Learn this problemProblem statement
A special thank-you to the friend who kindly shared that this problem was seen again on August 4, 2026! (∩˃o˂∩)♡
Imagine a board of size numRows x numColumns with lasers placed on it. Their coordinates are given in the two-dimensional array laserCoordinates, where laserCoordinates[i] is a two-element array containing the one-based row and column coordinates of the center of the ith laser.
A laser centered at (row, column) destroys everything in row row and column column. In other words, each laser shoots in all non-diagonal directions until the border of the board.
A robot starts at the one-based coordinates (curRow, curColumn). It can move only in a straight line within the board: left, right, up, or down. Return the maximum number of cells the robot can safely move through in any one direction before being destroyed by a laser.
The initial cell is protected. Lasers cannot destroy the robot there even if that cell is within their destruction area.
A solution with time complexity no worse than O(numRows * numColumns * laserCoordinates.length) will fit within the execution time limit.
Function
laserRobotSafePath(numRows: int, numColumns: int, curRow: int, curColumn: int, laserCoordinates: int[][]) → intExamples
Example 1
numRows = 8numColumns = 8curRow = 5curColumn = 3laserCoordinates = [[1, 6], [2, 8]]return = 3On the 8 x 8 board, the two lasers are centered at (1, 6) and (2, 8). The longest safe path available to the robot contains 3 cells.
Constraints
8 <= numRows <= 208 <= numColumns <= 201 <= curRow <= numRows1 <= curColumn <= numColumns0 <= laserCoordinates.length <= 5laserCoordinates[i].length = 21 <= laserCoordinates[i][0] <= numRows1 <= laserCoordinates[i][1] <= numColumns- The robot starts at a different cell from all laser centers.