LeetCode 2120: Execution of All Suffix Instructions Staying in a Grid Solution

Master LeetCode problem 2120 (Execution of All Suffix Instructions Staying in a Grid), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2120. Execution of All Suffix Instructions Staying in a Grid

Problem Explanation

Explanation:

To solve this problem, we can simulate the movement of the robot based on the given instructions and check how many instructions it can execute starting from each position. We will keep track of the number of instructions executed until the robot moves off the grid or all instructions are executed.

We can iterate through each starting point and for each starting point, we iterate through the instructions in the given order. At each step, we update the position of the robot based on the instruction and check if the new position is within the grid. If it is within the grid, we increment the count of executed instructions. If the new position is outside the grid, we stop and record the count of instructions executed so far.

Time Complexity:

The time complexity of this solution is O(n^2 * m) where n is the size of the grid and m is the length of the instruction string.

Space Complexity:

The space complexity of this solution is O(1) as we are not using any extra space that grows with the input size.

:

Solution Code

class Solution {
    public int[] executionOfAllSuffixInstructions(int n, int[] startPos, String s) {
        int m = s.length();
        int[] answer = new int[m];
        
        for (int i = 0; i < m; i++) {
            int row = startPos[0];
            int col = startPos[1];
            int count = 0;
            
            for (int j = i; j < m; j++) {
                char instruction = s.charAt(j);
                
                if (instruction == 'L' && col > 0) {
                    col--;
                    count++;
                } else if (instruction == 'R' && col < n - 1) {
                    col++;
                    count++;
                } else if (instruction == 'U' && row > 0) {
                    row--;
                    count++;
                } else if (instruction == 'D' && row < n - 1) {
                    row++;
                    count++;
                } else {
                    break;
                }
            }
            
            answer[i] = count;
        }
        
        return answer;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2120 (Execution of All Suffix Instructions Staying in a Grid)?

This page provides optimized solutions for LeetCode problem 2120 (Execution of All Suffix Instructions Staying in a Grid) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2120 (Execution of All Suffix Instructions Staying in a Grid)?

The time complexity for LeetCode 2120 (Execution of All Suffix Instructions Staying in a Grid) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2120 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2120 in Java, C++, or Python.

Back to LeetCode Solutions