/**
* Problem: LeetCode 2069 - Walking Robot Simulation II
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Robot {
private final int[][] coords;
private final String[] dirs;
private final int perimeter;
private int idx;
private boolean moved;
public Robot(int width, int height) {
perimeter = 2 * (width + height) - 4;
coords = new int[perimeter][2];
dirs = new String[perimeter];
idx = 0;
moved = false;
int index = 0;
// Bottom row: (0, 0) to (width - 1, 0)
for (int i = 0; i < width; i++) {
coords[index] = new int[]{i, 0};
dirs[index] = "East";
index++;
}
// Right col: (width - 1, 1) to (width - 1, height - 1)
for (int j = 1; j < height; j++) {
coords[index] = new int[]{width - 1, j};
dirs[index] = "North";
index++;
}
// Top row: (width - 2, height - 1) down to (0, height - 1)
for (int i = width - 2; i >= 0; i--) {
coords[index] = new int[]{i, height - 1};
dirs[index] = "West";
index++;
}
// Left col: (0, height - 2) down to (0, 1)
for (int j = height - 2; j > 0; j--) {
coords[index] = new int[]{0, j};
dirs[index] = "South";
index++;
}
// Arriving at origin after movement faces South
dirs[0] = "South";
}
public void step(int num) {
moved = true;
idx = (idx + num) % perimeter;
}
public int[] getPos() {
return coords[idx];
}
public String getDir() {
if (!moved) {
return "East";
}
return dirs[idx];
}
}
"""
Problem: LeetCode 2069 - Walking Robot Simulation II
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Robot:
"""
Simulates a robot moving along the perimeter of a grid.
Time Complexity:
- __init__: O(W + H)
- step: O(1)
- getPos: O(1)
- getDir: O(1)
Space Complexity: O(W + H)
"""
def __init__(self, width: int, height: int):
self.perimeter = 2 * (width + height) - 4
self.coords = []
self.dirs = []
self.idx = 0
self.moved = False
# Bottom row
for i in range(width):
self.coords.append([i, 0])
self.dirs.append("East")
# Right column
for j in range(1, height):
self.coords.append([width - 1, j])
self.dirs.append("North")
# Top row
for i in range(width - 2, -1, -1):
self.coords.append([i, height - 1])
self.dirs.append("West")
# Left column
for j in range(height - 2, 0, -1):
self.coords.append([0, j])
self.dirs.append("South")
self.dirs[0] = "South"
def step(self, num: int) -> None:
self.moved = True
self.idx = (self.idx + num) % self.perimeter
def getPos(self) -> List[int]:
return self.coords[self.idx]
def getDir(self) -> str:
if not self.moved:
return "East"
return self.dirs[self.idx]
/**
* Problem: LeetCode 2069 - Walking Robot Simulation II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <string>
using namespace std;
class Robot {
private:
vector<vector<int>> coords;
vector<string> dirs;
int perimeter;
int idx;
bool moved;
public:
Robot(int width, int height) {
perimeter = 2 * (width + height) - 4;
coords.reserve(perimeter);
dirs.reserve(perimeter);
idx = 0;
moved = false;
// Bottom row
for (int i = 0; i < width; ++i) {
coords.push_back({i, 0});
dirs.push_back("East");
}
// Right col
for (int j = 1; j < height; ++j) {
coords.push_back({width - 1, j});
dirs.push_back("North");
}
// Top row
for (int i = width - 2; i >= 0; --i) {
coords.push_back({i, height - 1});
dirs.push_back("West");
}
// Left col
for (int j = height - 2; j > 0; --j) {
coords.push_back({0, j});
dirs.push_back("South");
}
dirs[0] = "South";
}
void step(int num) {
moved = true;
idx = (idx + num) % perimeter;
}
vector<int> getPos() {
return coords[idx];
}
string getDir() {
if (!moved) return "East";
return dirs[idx];
}
};
/**
* Problem: LeetCode 2069 - Walking Robot Simulation II
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var Robot = function(width, height) {
this.perimeter = 2 * (width + height) - 4;
this.coords = [];
this.dirs = [];
this.idx = 0;
this.moved = false;
// Bottom row
for (let i = 0; i < width; i++) {
this.coords.push([i, 0]);
this.dirs.push("East");
}
// Right col
for (let j = 1; j < height; j++) {
this.coords.push([width - 1, j]);
this.dirs.push("North");
}
// Top row
for (let i = width - 2; i >= 0; i--) {
this.coords.push([i, height - 1]);
this.dirs.push("West");
}
// Left col
for (let j = height - 2; j > 0; j--) {
this.coords.push([0, j]);
this.dirs.push("South");
}
this.dirs[0] = "South";
};
/**
* @param {number} num
* @return {void}
*/
Robot.prototype.step = function(num) {
this.moved = true;
this.idx = (this.idx + num) % this.perimeter;
};
/**
* @return {number[]}
*/
Robot.prototype.getPos = function() {
return this.coords[this.idx];
};
/**
* @return {string}
*/
Robot.prototype.getDir = function() {
if (!this.moved) return "East";
return this.dirs[this.idx];
};