문제
링크텍스트
풀이
import heapq
def solution(people, limit):
n = len( people )
if n == 1:
return 1
elif n == 2:
return 1 if sum(people) <= limit else 2
heapq.heapify(people)
p = []
for i in range(n):
heapq.heappush(p,-people[i])
cnt = 0
while len(p)+len(people) > n:
cnt += 1
if people[0]-heapq.heappop(p) <= limit:
heapq.heappop(people)
return cnt
for i in range(4):
print(f'test case: {i}')
if i == 0:
people, limit, r = [70, 50, 80, 50], 100, 3
elif i == 1:
people, limit, r = [70, 80, 50], 100, 3
elif i == 2:
people, limit, r = [70], 100, 1
elif i == 3:
people, limit, r = [50,25,25,70], 100, 2
a = solution(people, limit)
print(a)
print(r)
결과