LeetCode 1041: Robot Bounded In Circle Solution

Master LeetCode problem 1041 (Robot Bounded In Circle), 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.

1041. Robot Bounded In Circle

Problem Explanation

Explanation

To solve this problem, we need to simulate the movement of the robot based on the given instructions and check if the robot ends up at the initial position and facing the same direction after executing the instructions. If the robot ends up in the same place and facing the same direction after executing all the instructions, then it will be in a circle and will never leave it.

We can keep track of the direction the robot is facing and its coordinates. We iterate through the instructions and update the direction and coordinates accordingly. After iterating through all the instructions, if the robot is either back at the initial position or facing a direction other than north, then it will be in a circle.

Algorithm:

  1. Initialize variables for direction (north by default), x and y coordinates (0, 0).
  2. Iterate through the instructions:
    • If the instruction is "G", move the robot one step in the current direction.
    • If the instruction is "L" or "R", update the direction of the robot.
  3. After iterating through all instructions, if the robot is back at the initial position or not facing north, return true; otherwise, return false.

Time Complexity: O(N) where N is the length of the instructions string.

Space Complexity: O(1)

Solution Code

class Solution {
    public boolean isRobotBounded(String instructions) {
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int x = 0, y = 0;
        int dir = 0; // initially facing north

        for (char instruction : instructions.toCharArray()) {
            if (instruction == 'G') {
                x += directions[dir][0];
                y += directions[dir][1];
            } else if (instruction == 'L') {
                dir = (dir + 3) % 4;
            } else {
                dir = (dir + 1) % 4;
            }
        }

        return (x == 0 && y == 0) || dir != 0;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1041 (Robot Bounded In Circle)?

This page provides optimized solutions for LeetCode problem 1041 (Robot Bounded In Circle) 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 1041 (Robot Bounded In Circle)?

The time complexity for LeetCode 1041 (Robot Bounded In Circle) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1041 on DevExCode?

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

Back to LeetCode Solutions