We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def pop_from(dict, key):
val = heapq.heappop(dict[key])
if len(dict[key]) == 0:
del dict[key]
return val
def push_into(dict, key, value):
if key not in dict:
dict[key] = []
heapq.heapify(dict[key])
heapq.heappush(dict[key], value)
t = int(input())
for _ in range(t):
A = list(map(int, input().split()))
n = A[0]
if n ==0:
print(0)
else:
A = sorted(A[1:])
teams = {}
for i in range(n):
if A[i] - 1 in teams:
size = pop_from(teams, A[i] - 1)
push_into(teams, A[i], size + 1)
else:
push_into(teams, A[i], 1)
min_size = 999999
for each in teams.values():
min_pot = min(each)
if min_pot < min_size:
min_size = min_pot
if min_size != 999999:
print(min_size)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Team Formation
You are viewing a single comment's thread. Return to all comments →
import heapq
def pop_from(dict, key): val = heapq.heappop(dict[key]) if len(dict[key]) == 0: del dict[key] return val
def push_into(dict, key, value): if key not in dict: dict[key] = [] heapq.heapify(dict[key]) heapq.heappush(dict[key], value)
t = int(input()) for _ in range(t): A = list(map(int, input().split())) n = A[0] if n ==0: print(0) else: A = sorted(A[1:]) teams = {} for i in range(n): if A[i] - 1 in teams: size = pop_from(teams, A[i] - 1) push_into(teams, A[i], size + 1) else: push_into(teams, A[i], 1)