Skip to content

Commit 150d700

Browse files
committed
add koko eating bananas
1 parent 5d57745 commit 150d700

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Diff for: src/koko_eating_bananas.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
import math
3+
4+
5+
class Solution:
6+
def minEatingSpeed(self, piles: List[int], h: int) -> int:
7+
left, right = 1, max(piles)
8+
res = right
9+
10+
while left <= right:
11+
k = (left + right) // 2
12+
hours = 0
13+
for p in piles:
14+
hours += math.ceil(p / k)
15+
16+
if hours <= h:
17+
res = min(res, k)
18+
right = k - 1
19+
else:
20+
left = k + 1
21+
return res

Diff for: tests/test_koko_eating_bananas.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from koko_eating_bananas import Solution
2+
3+
4+
testcase = [{"testcase": [3, 6, 7, 11], "h": 8, "result": 4}]
5+
6+
7+
def test_koko_eating_bananas():
8+
for tc in testcase:
9+
piles = tc["testcase"]
10+
h = tc["h"]
11+
ans = Solution().minEatingSpeed(piles, h)
12+
assert ans == tc["result"]

0 commit comments

Comments
 (0)