← Home

1041. Robot Bounded In Circle

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
104

This is one of those problems where the clean idea matters more than the amount of code. For 1041. Robot Bounded In Circle, the solution in this repository is mainly a two pointers solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are isRobotBounded.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Robot Bounded In Circle.
02//Memory Usage: 6.3 MB, less than 19.49% of C++ online submissions for Robot Bounded In Circle.
03class Solution {
04public:
05    bool isRobotBounded(string instructions) {
06        vector<int> pos = {0, 0};
07        vector<vector<int>> dirs = {{0,1}, {-1,0}, {0,-1}, {1,0}};
08        int dirId = 0;
09        
10        for(int i = 0; i < 4; ++i){
11            for(char c : instructions){
12                switch(c){
13                    case 'G':
14                        pos[0] += dirs[dirId][0];
15                        pos[1] += dirs[dirId][1];
16                        break;
17                    case 'L':
18                        dirId = (dirId+1)%4;
19                        break;
20                    case 'R':
21                        dirId = (dirId+4-1)%4;
22                        break;
23                }
24            }
25            if(pos[0] == 0 && pos[1] == 0){
26                return true;
27            }
28        }
29        
30        return false;
31    }
32};
33
34//https://leetcode.com/problems/robot-bounded-in-circle/discuss/290856/JavaC%2B%2BPython-Let-Chopper-Help-Explain
35//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Robot Bounded In Circle.
36//Memory Usage: 6.2 MB, less than 51.92% of C++ online submissions for Robot Bounded In Circle.
37class Solution {
38public:
39    bool isRobotBounded(string instructions) {
40        vector<int> pos = {0, 0};
41        vector<vector<int>> dirs = {{0,1}, {-1,0}, {0,-1}, {1,0}};
42        int dirId = 0;
43        
44        for(char c : instructions){
45            switch(c){
46                case 'G':
47                    pos[0] += dirs[dirId][0];
48                    pos[1] += dirs[dirId][1];
49                    break;
50                case 'L':
51                    dirId = (dirId+1)%4;
52                    break;
53                case 'R':
54                    dirId = (dirId+4-1)%4;
55                    break;
56            }
57        }
58        
59        /*if after a sequence of instruction, 
60        the robot doesn't face north,
61        then it will go to original point after four sequences of instruction
62        */
63        return (pos[0] == 0 && pos[1] == 0) || (dirId != 0);
64    }
65};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.