Robot Navigation Around Lasers
Learn this problemProblem statement
A robot starts at the 1-indexed cell (curRow, curCol) of a numRows by numCols board. Each laser is given by its center cell. A cell is unsafe if its row or its column contains at least one laser.
Choose exactly one of the four cardinal directions and move the robot in a straight line. The robot may enter only safe cells and may not leave the board. Return the maximum number of steps it can take before its next step would enter an unsafe cell or leave the board. The starting cell is not counted as a step.
Function
maxSafeSteps(numRows: int, numCols: int, curRow: int, curCol: int, laserCoordinates: int[][]) → intExamples
Example 1
numRows = 6numCols = 7curRow = 4curCol = 3laserCoordinates = [[2,6],[6,5]]return = 2Rows 2 and 6 and columns 5 and 6 are unsafe. From (4,3), the robot can move two cells up, one cell down, one cell right, or two cells left. The maximum is 2.
Example 2
numRows = 4numCols = 5curRow = 2curCol = 3laserCoordinates = [[4,1]]return = 2Only row 4 and column 1 are unsafe. Moving right reaches columns 4 and 5, for two safe steps.
Constraints
1 <= numRows, numCols <= 200000.1 <= curRow <= numRowsand1 <= curCol <= numCols.0 <= laserCoordinates.length <= 200000.- Every laser coordinate is a valid board cell.
- The robot's starting cell is safe.