#3248

Snake in Matrix

newbie · 155 · lc easy +14 · 82.4% accepted · 175 likes · top 94%

Description

A snake moves in an n x n grid. Cell (i, j) has index i * n + j. The snake starts at index 0 and follows commands ("UP", "DOWN", "LEFT", "RIGHT"). The snake always stays within the grid.

Return the index of the final cell.

Code

1
2
3