Maximum Points in an Archery Competition LeetCode solution in Java

1
class Solution {
2
int bobPoint = 0;
3
int[] maxbob = new int[12];
4

5
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
6
int[] bob = new int[12];
7
calculate(aliceArrows, bob, 11, numArrows, 0); // Start with max point that is 11
8
return maxbob;
9
}
10

11
public void calculate(int[] alice, int[] bob, int index, int remainArr, int point) {
12
if (index < 0 || remainArr <= 0) {
13
if (remainArr > 0) bob[0] += remainArr;
14
if (point > bobPoint) { // Update the max points and result output
15
bobPoint = point;
16
maxbob = bob.clone();
17
}
18
return;
19
}
20
// part 1: assign 1 more arrow than alice
21
if (remainArr >= alice[index] + 1) {
22
bob[index] = alice[index] + 1;
23
calculate(alice, bob, index - 1, remainArr - (alice[index] + 1), point + index);
24
bob[index] = 0;
25
}
26
// part 2: assign no arrow and move to next point
27
calculate(alice, bob, index - 1, remainArr, point);
28
bob[index] = 0;
29
}
30
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0