2120. Execution of All Suffix Instructions Staying in a Grid
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def executeInstructions(self, n, startPos, s):
        m = len(s)
        answer = [0] * m
        grid = [[0] * n for _ in range(n)]

        def is_valid(r, c):
            return 0 <= r < n and 0 <= c < n

        for i in range(m - 1, -1, -1):
            r, c = startPos
            count = 0
            for j in range(i, m):
                if s[j] == 'L': c -= 1
                elif s[j] == 'R': c += 1
                elif s[j] == 'U': r -= 1
                elif s[j] == 'D': r += 1
                if not is_valid(r, c):
                    break
                count += 1
            answer[i] = count

        return answer

```
['Python3']