Description
Solutions
Rover Move
🤘 INTERN

A Mars rover is directed to move within a square matrix. It accepts a sequence of commands to move in any of the four directions from each cell: [UP, DOWN, LEFT or RIGHT]. The rover starts from cell 0. and may not move diagonally or outside of the boundary.

Each cell in the matrix has a position equal to:

(row * size) + column

where row and column are zero-indexed, size = row length of the matrix.

Return the final position of the rover after all moves.

Function Description

Complete the function roverMove in the editor.

roverMove has the following parameter(s):

  1. 1. int n: the size of the square matrix
  2. 2. String[] cmds: the commands

Returns

int: the label of the cell the rover occupies after executing all commands

Example 1:

Input:  n = 4, cmds = ["RIGHT", "UP", "DOWN", "LEFT", "DOWN", "DOWN"]
Output: 12
Explanation:
The function returns 12.
Constraints:
    • 2 ≤ n ≤ 20
    • 1 ≤ |cmds| ≤ 20
Thumbnail 0
Testcase

Result
Case 1

input:

output: