-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMaximum Points in an Archery Competition.java
30 lines (30 loc) · 1.24 KB
/
Maximum Points in an Archery Competition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Runtime: 5 ms (Top 88.89%) | Memory: 40.7 MB (Top 100.00%)
class Solution {
int bobPoint = 0;
int[] maxbob = new int[12];
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
int[] bob = new int[12];
calculate(aliceArrows, bob, 11, numArrows, 0); //Start with max point that is 11
return maxbob;
}
public void calculate(int[] alice, int[] bob, int index, int remainArr, int point) {
if(index < 0 || remainArr <= 0) {
if(remainArr > 0)
bob[0] += remainArr;
if(point > bobPoint) { // Update the max points and result output
bobPoint = point;
maxbob = bob.clone();
}
return;
}
//part 1: assign 1 more arrow than alice
if(remainArr >= alice[index]+1) {
bob[index] = alice[index] + 1;
calculate(alice, bob, index-1, remainArr-(alice[index]+1), point + index);
bob[index] = 0;
}
//part 2: assign no arrow and move to next point
calculate(alice, bob, index-1, remainArr, point);
bob[index] = 0;
}
}