1
/**
2
* @param {number} target
3
* @return {number}
4
*/
5
var racecar = function (target) {
6
const queue = [{ position: 0, speed: 1, sequence: "" }];
7

8
while (queue.length) {
9
let { position, speed, sequence } = queue.shift();
10

11
if (position === target) return sequence.length;
12

13
const newPosition = position + speed;
14

15
queue.push({
16
position: newPosition,
17
speed: speed * 2,
18
sequence: sequence + "A",
19
});
20

21
if (
22
(newPosition > target && speed > 0) ||
23
(newPosition < target && speed < 0)
24
) {
25
queue.push({
26
sequence: sequence + "R",
27
position,
28
speed: Math.sign(speed) === 1 ? -1 : 1,
29
});
30
}
31
}
32

33
return 0;
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0