forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-points-in-an-archery-competition.py
More file actions
35 lines (33 loc) · 1.01 KB
/
maximum-points-in-an-archery-competition.py
File metadata and controls
35 lines (33 loc) · 1.01 KB
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
31
32
33
34
35
# Time: O(n * 2^n)
# Space: O(n)
# bitmasks
class Solution(object):
def maximumBobPoints(self, numArrows, aliceArrows):
"""
:type numArrows: int
:type aliceArrows: List[int]
:rtype: List[int]
"""
def check(mask, numArrows):
score = 0
cnt = [0]*len(aliceArrows)
i, base = 0, 1
for k, a in enumerate(aliceArrows):
if mask&1:
need = a+1
if need > numArrows:
return 0, [0]*len(aliceArrows)
numArrows -= need
cnt[k] = need
score += k
mask >>= 1
cnt[-1] += numArrows
return score, cnt
result = [0]*len(aliceArrows)
best = 0
for mask in xrange(1, 2**len(aliceArrows)):
score, cnt = check(mask, numArrows)
if score > best:
best = score
result = cnt
return result