Skip to content

Commit 2824156

Browse files
authored
Create maximum-points-in-an-archery-competition.py
1 parent 0659527 commit 2824156

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Time: O(n * 2^n)
2+
# Space: O(n)
3+
4+
# bitmasks
5+
class Solution(object):
6+
def maximumBobPoints(self, numArrows, aliceArrows):
7+
"""
8+
:type numArrows: int
9+
:type aliceArrows: List[int]
10+
:rtype: List[int]
11+
"""
12+
def check(mask, numArrows):
13+
score = 0
14+
cnt = [0]*len(aliceArrows)
15+
i, base = 0, 1
16+
for k, a in enumerate(aliceArrows):
17+
if mask&1:
18+
need = a+1
19+
if need > numArrows:
20+
return 0, [0]*len(aliceArrows)
21+
numArrows -= need
22+
cnt[k] = need
23+
score += k
24+
mask >>= 1
25+
cnt[-1] += numArrows
26+
return score, cnt
27+
28+
result = [0]*len(aliceArrows)
29+
best = 0
30+
for mask in xrange(1, 2**len(aliceArrows)):
31+
score, cnt = check(mask, numArrows)
32+
if score > best:
33+
best = score
34+
result = cnt
35+
return result

0 commit comments

Comments
 (0)