3533. Snake in Matrix
My accepted Python solution to LeetCode problem 3533, Snake in Matrix, running in 1ms.
- Difficulty: Easy
- Python
- Runtime 1ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1ms, memory 17.2MB, accepted 2026-01-01.
class Solution:
def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
row, col = 0, 0
for cmd in commands:
if cmd == "UP":
row -= 1
elif cmd == "DOWN":
row += 1
elif cmd == "LEFT":
col -= 1
elif cmd == "RIGHT":
col += 1
return row * n + col