Skip to content

Commit 8bbc5b8

Browse files
committed
solve problem Walking Robot Simulation
1 parent 266b303 commit 8bbc5b8

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ All solutions will be accepted!
218218
|427|[Construct Quad Tree](https://leetcode-cn.com/problems/construct-quad-tree/description/)|[java/py](./algorithms/ConstructQuadTree)|Easy|
219219
|458|[Poor Pigs](https://leetcode-cn.com/problems/poor-pigs/description/)|[java/py/js](./algorithms/PoorPigs)|Easy|
220220
|558|[Quad Tree Intersection](https://leetcode-cn.com/problems/quad-tree-intersection/description/)|[java/py](./algorithms/QuadTreeIntersection)|Easy|
221+
|874|[Walking Robot Simulation](https://leetcode-cn.com/problems/walking-robot-simulation/description/)|[java/py/js](./algorithms/WalkingRobotSimulation)|Easy|
221222

222223
# Database
223224
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Walking Robot Simulation
2+
This problem is easy to solve, here is the [solution](https://leetcode-cn.com/articles/walking-robot-simulation/)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int robotSim(int[] commands, int[][] obstacles) {
3+
int x = 0,
4+
y = 0,
5+
di = 0,
6+
res = 0;
7+
8+
int[] dx = {0, 1, 0, -1},
9+
dy = {1, 0, -1, 0};
10+
11+
Set<String> obstacleSet = new HashSet<String>();
12+
13+
for (int[] obstacle: obstacles) {
14+
obstacleSet.add(String.valueOf(obstacle[0]) + "X" + obstacle[1]);
15+
}
16+
17+
for (int cmd : commands) {
18+
if (cmd == -2) {
19+
di = (di - 1) % 4;
20+
if (di < 0) di = di + 4;
21+
} else if (cmd == -1) {
22+
di = (di + 1) % 4;
23+
} else {
24+
for (int i = 0; i < cmd; i++) {
25+
if (!obstacleSet.contains(String.valueOf(x + dx[di]) + "X" + String.valueOf(y + dy[di]))) {
26+
x += dx[di];
27+
y += dy[di];
28+
res = Math.max(res, x * x + y * y);
29+
} else {
30+
break;
31+
}
32+
}
33+
}
34+
}
35+
return res;
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} commands
3+
* @param {number[][]} obstacles
4+
* @return {number}
5+
*/
6+
var robotSim = function(commands, obstacles) {
7+
let x = y = di = res = 0,
8+
dx = [0, 1, 0, -1],
9+
dy = [1, 0, -1, 0]
10+
obstacleSet = new Set(obstacles.map(d => `${d[0]}X${d[1]}`))
11+
12+
commands.forEach(cmd => {
13+
if (cmd === -2) {
14+
di = (di - 1) % 4
15+
if (di < 0) di = di + 4
16+
} else if (cmd === -1) {
17+
di = (di + 1) % 4
18+
} else {
19+
for (let i = 0; i < cmd; i++) {
20+
if (!obstacleSet.has(`${x + dx[di]}X${y + dy[di]}`)) {
21+
x += dx[di]
22+
y += dy[di]
23+
res = Math.max(res, x * x + y * y)
24+
} else {
25+
break
26+
}
27+
}
28+
}
29+
})
30+
31+
return res
32+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution(object):
2+
def robotSim(self, commands, obstacles):
3+
"""
4+
:type commands: List[int]
5+
:type obstacles: List[List[int]]
6+
:rtype: int
7+
"""
8+
dx = [0, 1, 0, -1]
9+
dy = [1, 0, -1, 0]
10+
x = y = di = 0
11+
obstacleSet = set(map(tuple, obstacles))
12+
ans = 0
13+
14+
for cmd in commands:
15+
if cmd == -2: #left
16+
di = (di - 1) % 4
17+
elif cmd == -1: #right
18+
di = (di + 1) % 4
19+
else:
20+
for k in xrange(cmd):
21+
if (x + dx[di], y + dy[di]) not in obstacleSet:
22+
x += dx[di]
23+
y += dy[di]
24+
ans = max(ans, x * x + y * y)
25+
26+
return ans

0 commit comments

Comments
 (0)