Created
June 2, 2019 22:28
-
-
Save otherpirate/134467c5be220e61878e1b24106ad250 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Complete the 'teamFormation' function below. | |
# | |
# The function is expected to return a LONG_INTEGER. | |
# The function accepts following parameters: | |
# 1. INTEGER_ARRAY score | |
# 2. INTEGER team | |
# 3. INTEGER m | |
# | |
from bisect import insort | |
from collections import defaultdict | |
def teamFormation(scores, team_size, size): | |
team_total = 0 | |
right_start_at = len(scores) - size | |
print(right_start_at, len(scores)/2) | |
if right_start_at < len(scores)/2: | |
right_start_at = size | |
magic = defaultdict(int) | |
values = [] | |
for value in scores[0:size]: | |
magic['{}_L'.format(value)] += 1 | |
insort(values, value) | |
for value in scores[right_start_at:]: | |
magic['{}_R'.format(value)] += 1 | |
insort(values, value) | |
scores = scores[size:right_start_at] | |
for _ in range(team_size): | |
max_value = values.pop(-1) | |
team_total += max_value | |
if len(scores) == 0: | |
continue | |
new_value = None | |
if magic['{}_L'.format(max_value)] > 0: | |
new_value = scores.pop(0) | |
magic['{}_L'.format(new_value)] += 1 | |
magic['{}_L'.format(max_value)] -= 1 | |
else: | |
new_value = scores.pop(-1) | |
magic['{}_R'.format(new_value)] += 1 | |
magic['{}_R'.format(max_value)] -= 1 | |
insort(values, new_value) | |
return team_total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment