Datasets:
File size: 635 Bytes
93237a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | weights = [7, 2, 5, 10, 8]
def days_needed(capacity):
days, load = 1, 0
for weight in weights:
if weight > capacity:
return len(weights) + 1
if load + weight > capacity:
days += 1
load = 0
load += weight
return days
lo, hi = max(weights), sum(weights)
while lo < hi:
mid = (lo + hi) // 2
if days_needed(mid) <= 2:
hi = mid
else:
lo = mid + 1
assert days_needed(lo) <= 2 and days_needed(lo - 1) > 2
result = {"capacity": lo, "days_at_capacity": days_needed(lo)}
import json
print(json.dumps(result, ensure_ascii=False, sort_keys=True))
|