Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created March 17, 2025 01:52
Show Gist options
  • Select an option

  • Save unilecs/ebf505863c07ff797cf3c59a0b9aa014 to your computer and use it in GitHub Desktop.

Select an option

Save unilecs/ebf505863c07ff797cf3c59a0b9aa014 to your computer and use it in GitHub Desktop.
Задача: Оптимальное заполнение мешков (Python)
def maximumBags(capacity, rocks, additionalRocks):
# Вычитаем текущее количество камней из ёмкости
capacity = [c - r for c, r in zip(capacity, rocks)]
# Сортируем по возрастанию оставшегося места
capacity.sort()
result = 0
for space in capacity:
if space > 0 and additionalRocks == 0:
return result
if space == 0:
result += 1
elif space <= additionalRocks:
additionalRocks -= space
result += 1
return result
# Пример использования
capacity = [2, 3, 4, 5]
rocks = [1, 2, 4, 4]
additionalRocks = 2
print(maximumBags(capacity, rocks, additionalRocks))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment