Execution of All Suffix Instructions Staying in a Grid LeetCode solution in Python
1class Solution:2def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:3result = []4for idx in range(len(s)):5count, row, col = 0, startPos[0], startPos[1]6while idx < len(s):7if s[idx] == "D":8row += 19if row >= n:10break11count += 112elif s[idx] == "U":13row -= 114if row < 0:15break16count += 117elif s[idx] == "R":18col += 119if col >= n:20break21count += 122else:23col -= 124if col < 0:25break26count += 127idx += 128result.append(count)29return result