Maximum Points in an Archery Competition LeetCode solution in JavaScript

1
var maximumBobPoints = function (numArrows, aliceArrows) {
2
let max = 0,
3
n = aliceArrows.length,
4
res;
5
backtrack(numArrows, 0, 0, Array(n).fill(0));
6
return res;
7

8
function backtrack(arrows, idx, points, bobArrows) {
9
if (idx === n || arrows === 0) {
10
let origVal = bobArrows[n - 1];
11
if (arrows > 0) bobArrows[n - 1] += arrows; // put extra arrows in any slot
12
if (points > max) {
13
max = points;
14
res = [...bobArrows];
15
}
16
bobArrows[n - 1] = origVal;
17
return;
18
}
19

20
backtrack(arrows, idx + 1, points, bobArrows); // don't use any arrows
21
if (aliceArrows[idx] + 1 <= arrows) {
22
// use aliceArrows[idx] + 1 arrows to gain idx points
23
bobArrows[idx] = aliceArrows[idx] + 1;
24
backtrack(
25
arrows - (aliceArrows[idx] + 1),
26
idx + 1,
27
points + idx,
28
bobArrows
29
);
30
bobArrows[idx] = 0;
31
}
32
}
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0