Maximum Points in an Archery Competition LeetCode solution in Python

1
class Solution:
2
def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
3
max_score = [0, None]
4

5
def calc(i, remaining, score, arrows):
6
# Base case. Update max score.
7
if remaining == 0 or i == -1:
8
if score > max_score[0]:
9
max_score[0] = score
10
max_score[1] = arrows[:]
11
return
12

13
# Special handling for the last section. Use up all the arrows.
14
if i == 0:
15
arrows[i] = remaining
16
calc(i - 1, 0, score + i, arrows)
17
arrows[i] = 0
18
return
19

20
# Try to compete with Alice if there are enough arrows.
21
arrowsNeeded = aliceArrows[i] + 1
22
if remaining >= arrowsNeeded:
23
arrows[i] = arrowsNeeded
24
calc(i - 1, remaining - arrowsNeeded, score + i, arrows)
25
arrows[i] = 0
26

27
# Skip this section and go to the next section.
28
calc(i - 1, remaining, score, arrows)
29

30
# Kick off the recursion
31
calc(len(aliceArrows) - 1, numArrows, 0, [0 for _ in aliceArrows])
32
return max_score[1]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0