Execution of All Suffix Instructions Staying in a Grid LeetCode solution in Python

1
class Solution:
2
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
3
result = []
4
for idx in range(len(s)):
5
count, row, col = 0, startPos[0], startPos[1]
6
while idx < len(s):
7
if s[idx] == "D":
8
row += 1
9
if row >= n:
10
break
11
count += 1
12
elif s[idx] == "U":
13
row -= 1
14
if row < 0:
15
break
16
count += 1
17
elif s[idx] == "R":
18
col += 1
19
if col >= n:
20
break
21
count += 1
22
else:
23
col -= 1
24
if col < 0:
25
break
26
count += 1
27
idx += 1
28
result.append(count)
29
return result

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0