From 13e85849b388e27ca5427d6ad30e8f708ad3d909 Mon Sep 17 00:00:00 2001 From: Johan Sandoval Date: Wed, 22 Jul 2026 21:48:12 -0500 Subject: [PATCH] Finish problem 0875-koko-eating-bananas --- problems/0875-koko-eating-bananas/solution.py | 14 ++++++++++++++ problems/0875-koko-eating-bananas/solution_test.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 problems/0875-koko-eating-bananas/solution.py create mode 100644 problems/0875-koko-eating-bananas/solution_test.py diff --git a/problems/0875-koko-eating-bananas/solution.py b/problems/0875-koko-eating-bananas/solution.py new file mode 100644 index 0000000..ba0c107 --- /dev/null +++ b/problems/0875-koko-eating-bananas/solution.py @@ -0,0 +1,14 @@ +def hours_needed(piles: list[int], k: int) -> int: + return sum(-(-p // k) for p in piles) + + +class Solution: + def minEatingSpeed(self, piles: list[int], h: int) -> int: + lo, hi = 1, max(piles) + while lo < hi: + k = (lo + hi) // 2 + if hours_needed(piles, k) <= h: + hi = k + else: + lo = k + 1 + return lo diff --git a/problems/0875-koko-eating-bananas/solution_test.py b/problems/0875-koko-eating-bananas/solution_test.py new file mode 100644 index 0000000..4a96f21 --- /dev/null +++ b/problems/0875-koko-eating-bananas/solution_test.py @@ -0,0 +1,14 @@ +import pytest +from solution import Solution + + +@pytest.mark.parametrize( + "piles, h, expected", + [ + ([3, 6, 7, 11], 8, 4), + ([30, 11, 23, 4, 20], 5, 30), + ([30, 11, 23, 4, 20], 6, 23), + ], +) +def test_min_eating_speed(piles, h, expected): + assert Solution().minEatingSpeed(piles, h) == expected