Skip to content

Commit 15abaa8

Browse files
committed
Runtime: 3079 ms (Top 11.52%) | Memory: 74 MB (Top 23.64%)
1 parent cdfca59 commit 15abaa8

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

scripts/algorithms/R/Race Car/Race Car.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
// Runtime: 3079 ms (Top 11.52%) | Memory: 74 MB (Top 23.64%)
2+
13
var racecar = function(target) {
24
let queue = [[0, 1, 0]];
35
let visited = new Set(['0,1']);
4-
6+
57
while(queue.length > 0) {
68
const [pos, speed, distance] = queue.shift();
7-
9+
810
if(pos === target) return distance;
9-
11+
1012
const posA = pos + speed;
1113
const speedA = speed * 2;
1214
const keyA = posA + ',' + speedA;
13-
15+
1416
const posR = pos;
1517
const speedR = speed > 0 ? -1 : 1;
1618
const keyR = posR + ',' + speedR;
17-
18-
19+
1920
if(!visited.has(keyA) && posA >= 0 && posA <= 2*target) {
2021
visited.add(keyA);
2122
queue.push([posA, speedA, distance + 1]);
2223
}
23-
24+
2425
if(!visited.has(keyR) && posR >= 0 && posR <= 2*target) {
2526
visited.add(keyR);
2627
queue.push([posR, speedR, distance + 1]);

0 commit comments

Comments
 (0)