Skip to content

Commit a9beba0

Browse files
authored
feat: add solutions for lc No.0874 (#5136)
1 parent 47a4bd8 commit a9beba0

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

solution/0800-0899/0874.Walking Robot Simulation/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,94 @@ impl Solution {
333333
}
334334
```
335335

336+
#### JavaScript
337+
338+
```js
339+
/**
340+
* @param {number[]} commands
341+
* @param {number[][]} obstacles
342+
* @return {number}
343+
*/
344+
var robotSim = function (commands, obstacles) {
345+
const dirs = [0, 1, 0, -1, 0];
346+
const s = new Set();
347+
348+
const f = (x, y) => x * 60010 + y;
349+
350+
for (const [x, y] of obstacles) {
351+
s.add(f(x, y));
352+
}
353+
354+
let x = 0,
355+
y = 0,
356+
k = 0;
357+
let ans = 0;
358+
359+
for (let c of commands) {
360+
if (c === -2) {
361+
k = (k + 3) % 4;
362+
} else if (c === -1) {
363+
k = (k + 1) % 4;
364+
} else {
365+
while (c-- > 0) {
366+
const nx = x + dirs[k];
367+
const ny = y + dirs[k + 1];
368+
if (s.has(f(nx, ny))) {
369+
break;
370+
}
371+
x = nx;
372+
y = ny;
373+
ans = Math.max(ans, x * x + y * y);
374+
}
375+
}
376+
}
377+
378+
return ans;
379+
};
380+
```
381+
382+
#### C#
383+
384+
```cs
385+
public class Solution {
386+
public int RobotSim(int[] commands, int[][] obstacles) {
387+
int[] dirs = {0, 1, 0, -1, 0};
388+
HashSet<int> s = new HashSet<int>();
389+
390+
int F(int x, int y) => x * 60010 + y;
391+
392+
foreach (var o in obstacles) {
393+
s.Add(F(o[0], o[1]));
394+
}
395+
396+
int x = 0, y = 0, k = 0;
397+
int ans = 0;
398+
399+
foreach (int c0 in commands) {
400+
int c = c0;
401+
if (c == -2) {
402+
k = (k + 3) % 4;
403+
} else if (c == -1) {
404+
k = (k + 1) % 4;
405+
} else {
406+
while (c-- > 0) {
407+
int nx = x + dirs[k];
408+
int ny = y + dirs[k + 1];
409+
if (s.Contains(F(nx, ny))) {
410+
break;
411+
}
412+
x = nx;
413+
y = ny;
414+
ans = Math.Max(ans, x * x + y * y);
415+
}
416+
}
417+
}
418+
419+
return ans;
420+
}
421+
}
422+
```
423+
336424
<!-- tabs:end -->
337425

338426
<!-- solution:end -->

solution/0800-0899/0874.Walking Robot Simulation/README_EN.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,94 @@ impl Solution {
345345
}
346346
```
347347

348+
#### JavaScript
349+
350+
```js
351+
/**
352+
* @param {number[]} commands
353+
* @param {number[][]} obstacles
354+
* @return {number}
355+
*/
356+
var robotSim = function (commands, obstacles) {
357+
const dirs = [0, 1, 0, -1, 0];
358+
const s = new Set();
359+
360+
const f = (x, y) => x * 60010 + y;
361+
362+
for (const [x, y] of obstacles) {
363+
s.add(f(x, y));
364+
}
365+
366+
let x = 0,
367+
y = 0,
368+
k = 0;
369+
let ans = 0;
370+
371+
for (let c of commands) {
372+
if (c === -2) {
373+
k = (k + 3) % 4;
374+
} else if (c === -1) {
375+
k = (k + 1) % 4;
376+
} else {
377+
while (c-- > 0) {
378+
const nx = x + dirs[k];
379+
const ny = y + dirs[k + 1];
380+
if (s.has(f(nx, ny))) {
381+
break;
382+
}
383+
x = nx;
384+
y = ny;
385+
ans = Math.max(ans, x * x + y * y);
386+
}
387+
}
388+
}
389+
390+
return ans;
391+
};
392+
```
393+
394+
#### C#
395+
396+
```cs
397+
public class Solution {
398+
public int RobotSim(int[] commands, int[][] obstacles) {
399+
int[] dirs = {0, 1, 0, -1, 0};
400+
HashSet<int> s = new HashSet<int>();
401+
402+
int F(int x, int y) => x * 60010 + y;
403+
404+
foreach (var o in obstacles) {
405+
s.Add(F(o[0], o[1]));
406+
}
407+
408+
int x = 0, y = 0, k = 0;
409+
int ans = 0;
410+
411+
foreach (int c0 in commands) {
412+
int c = c0;
413+
if (c == -2) {
414+
k = (k + 3) % 4;
415+
} else if (c == -1) {
416+
k = (k + 1) % 4;
417+
} else {
418+
while (c-- > 0) {
419+
int nx = x + dirs[k];
420+
int ny = y + dirs[k + 1];
421+
if (s.Contains(F(nx, ny))) {
422+
break;
423+
}
424+
x = nx;
425+
y = ny;
426+
ans = Math.Max(ans, x * x + y * y);
427+
}
428+
}
429+
}
430+
431+
return ans;
432+
}
433+
}
434+
```
435+
348436
<!-- tabs:end -->
349437

350438
<!-- solution:end -->
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Solution {
2+
public int RobotSim(int[] commands, int[][] obstacles) {
3+
int[] dirs = {0, 1, 0, -1, 0};
4+
HashSet<int> s = new HashSet<int>();
5+
6+
int F(int x, int y) => x * 60010 + y;
7+
8+
foreach (var o in obstacles) {
9+
s.Add(F(o[0], o[1]));
10+
}
11+
12+
int x = 0, y = 0, k = 0;
13+
int ans = 0;
14+
15+
foreach (int c0 in commands) {
16+
int c = c0;
17+
if (c == -2) {
18+
k = (k + 3) % 4;
19+
} else if (c == -1) {
20+
k = (k + 1) % 4;
21+
} else {
22+
while (c-- > 0) {
23+
int nx = x + dirs[k];
24+
int ny = y + dirs[k + 1];
25+
if (s.Contains(F(nx, ny))) {
26+
break;
27+
}
28+
x = nx;
29+
y = ny;
30+
ans = Math.Max(ans, x * x + y * y);
31+
}
32+
}
33+
}
34+
35+
return ans;
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[]} commands
3+
* @param {number[][]} obstacles
4+
* @return {number}
5+
*/
6+
var robotSim = function (commands, obstacles) {
7+
const dirs = [0, 1, 0, -1, 0];
8+
const s = new Set();
9+
10+
const f = (x, y) => x * 60010 + y;
11+
12+
for (const [x, y] of obstacles) {
13+
s.add(f(x, y));
14+
}
15+
16+
let x = 0,
17+
y = 0,
18+
k = 0;
19+
let ans = 0;
20+
21+
for (let c of commands) {
22+
if (c === -2) {
23+
k = (k + 3) % 4;
24+
} else if (c === -1) {
25+
k = (k + 1) % 4;
26+
} else {
27+
while (c-- > 0) {
28+
const nx = x + dirs[k];
29+
const ny = y + dirs[k + 1];
30+
if (s.has(f(nx, ny))) {
31+
break;
32+
}
33+
x = nx;
34+
y = ny;
35+
ans = Math.max(ans, x * x + y * y);
36+
}
37+
}
38+
}
39+
40+
return ans;
41+
};

0 commit comments

Comments
 (0)