From 25956cc25bee99789a810d21c985095cac1e2285 Mon Sep 17 00:00:00 2001 From: Johan Sandoval Date: Thu, 23 Jul 2026 13:56:14 -0500 Subject: [PATCH] Finish problem 1011-capacity-to-ship-packages-within-d-days --- .../solution.py | 23 +++++++++++++++++++ .../solution_test.py | 19 +++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 problems/1011-capacity-to-ship-packages-within-d-days/solution.py create mode 100644 problems/1011-capacity-to-ship-packages-within-d-days/solution_test.py diff --git a/problems/1011-capacity-to-ship-packages-within-d-days/solution.py b/problems/1011-capacity-to-ship-packages-within-d-days/solution.py new file mode 100644 index 0000000..555ef49 --- /dev/null +++ b/problems/1011-capacity-to-ship-packages-within-d-days/solution.py @@ -0,0 +1,23 @@ +def days_needed(weights: list[int], s: int) -> int: + days = 1 + capacity = s + for weight in weights: + if capacity - weight >= 0: + capacity -= weight + else: + capacity = s - weight + days += 1 + return days + + +class Solution: + def shipWithinDays(self, weights: list[int], days: int) -> int: + lo, hi = max(weights), sum(weights) + while lo < hi: + s = (lo + hi) // 2 + if days_needed(weights, s) > days: + lo = s + 1 + else: + hi = s + + return lo diff --git a/problems/1011-capacity-to-ship-packages-within-d-days/solution_test.py b/problems/1011-capacity-to-ship-packages-within-d-days/solution_test.py new file mode 100644 index 0000000..b24a6f5 --- /dev/null +++ b/problems/1011-capacity-to-ship-packages-within-d-days/solution_test.py @@ -0,0 +1,19 @@ +import pytest +from solution import Solution + + +@pytest.mark.parametrize( + "weights,days,expected", + [ + ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5, 15), + ([3, 2, 2, 4, 1, 4], 3, 6), + ([1, 2, 3, 1, 1], 4, 3), + ([1], 1, 1), + ([5, 5, 5, 5], 4, 5), + ([5, 5, 5, 5], 1, 20), + ([10, 50, 100, 100, 50, 10], 1, 320), + ([10, 50, 100, 100, 50, 10], 6, 100), + ], +) +def test_ship_within_days(weights, days, expected): + assert Solution().shipWithinDays(weights, days) == expected