Practice Snake in Matrix
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
There is a snake in an n x n matrix grid and can move in four possible directions. Each cell in the grid is identified by the position: grid[i][j] = (i * n) + j. The snake starts at cell 0 and follows a sequence of commands. You are given an integer n representing the size of the grid and an array of strings commands where each command[i] is either "UP", "RIGHT", "DOWN", and "LEFT". It's guaranteed that the snake will remain within the grid boundaries throughout its movement. Return the position of the final cell where the snake ends up after executing commands.
Source: Snake in Matrix on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
n = 2, commands = ["RIGHT","DOWN"]
3
# Example 2
n = 3, commands = ["DOWN","RIGHT","UP"]
1
Constraints
Related LeetCode Problems
Practice more problems similar to Snake in Matrix.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.