-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode-874.js
50 lines (44 loc) · 1.14 KB
/
Leetcode-874.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 874. Walking Robot Simulation
* https://leetcode.com/problems/walking-robot-simulation/
* @param {number[]} commands
* @param {number[][]} obstacles
* @return {number}
*/
/* 0 = north
* 1 = east
* 2 = south
* 3 = west
*/
const robotSim = (commands, obstacles) => {
let obstacle = {};
let x = 0;
let y = 0;
let max = 0;
let direction = 0;
for(let i = 0; i < obstacles.length; i++) {
obstacle[obstacles[i]] = true;
}
for(let i = 0; i < commands.length; i++) {
if(commands[i] == -1) {
direction = (direction + 1)% 4;
} else if(commands[i] == -2) {
direction = ((direction - 1) + 4) % 4;
} else {
while(commands[i]--) {
let previousX = x;
let previousY = y;
if(direction===0) y++;
if(direction===1) x++;
if(direction===2) y--;
if(direction===3) x--;
if(obstacle[x+','+y]) {
[x, y] = [previousX,previousY];
break;
}
}
}
max = Math.max(max, x**2 + y**2)
}
return max;
};