Walking Robot Simulation II Solution in Java

Links to Other Language Solutions

C++ Solution |
Python Solution

Code Solution


class Robot {
  public Robot(int width, int height) {
    pos.add(new Pair<>(new int[] {0, 0}, "South"));
    for (int i = 1; i < width; ++i)
      pos.add(new Pair<>(new int[] {i, 0}, "East"));
    for (int j = 1; j < height; ++j)
      pos.add(new Pair<>(new int[] {width - 1, j}, "North"));
    for (int i = width - 2; i >= 0; --i)
      pos.add(new Pair<>(new int[] {i, height - 1}, "West"));
    for (int j = height - 2; j > 0; --j)
      pos.add(new Pair<>(new int[] {0, j}, "South"));
  }

  public void step(int num) {
    isOrigin = false;
    i = (i + num) % pos.size();
  }

  public int[] getPos() {
    return pos.get(i).getKey();
  }

  public String getDir() {
    return isOrigin ? "East" : pos.get(i).getValue();
  }

  private boolean isOrigin = true;
  private int i = 0;
  private List> pos = new ArrayList<>();
}

Problem Description

So, you think you can just send a robot on a leisurely stroll around a rectangular park without any consequences? Well, think again! The Walking Robot Simulation II problem on LeetCode is here to remind you that even robots need a plan. Imagine your robot is like that friend who always gets lost, even in a straight line. You need to help it navigate a rectangular grid, starting from the bottom-left corner, and make sure it doesn’t bump into walls or get stuck in a loop of confusion.

The robot can move in four directions: East, North, West, and South. It starts at the origin (0,0) and can only move along the perimeter of the rectangle defined by its width and height. Your job is to simulate its movement based on a series of steps and return its current position and direction.

Approach

The code initializes the robot’s position along the perimeter of the rectangle. It uses a list to store the possible positions and their corresponding directions. When the step method is called, it updates the robot’s position based on the number of steps taken, wrapping around if necessary. The getPos method retrieves the current coordinates, while getDir returns the current direction, ensuring that the robot knows which way it’s facing, even if it’s just staring at a wall.

Time and Space Complexity

Complexity Type Complexity
Time Complexity O(1) for the step, getPos, and getDir methods
Space Complexity O(n) where n is the perimeter of the rectangle

Real-World Example

Picture this: you’re at a theme park, and your friend (the robot) insists on walking the perimeter of the park instead of exploring the rides. You give them a map (the code) that tells them where to go, but they keep asking, “Am I facing the right way?” This simulation is just like that—keeping track of where your robot is and which way it’s facing, so it doesn’t end up in the cotton candy stand for the third time in a row!

Similar Problems

If you enjoyed this problem, you might also like these:

  • 2-Sum Solution in Java
  • 3-Sum Solution in Java
  • 4-Sum Solution in Java