Skip to content

Commit 48f338a

Browse files
committed
Feat: add solutions
1 parent 535b905 commit 48f338a

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
| 652 | Find Duplicate Subtrees | [Ruby](./algorithms/ruby/0652-find-duplicate-subtrees.rb) | Medium |
4545
| 733 | Flood Fill | [Ruby](./algorithms/ruby/0733-flood-fill.rb) | Easy |
4646
| 783 | Minimum Distance Between BST Nodes | [Ruby](./algorithms/ruby/0783-minimum-distance-between-bst-nodes.rb) [Python3](./algorithms/python3/0783-minimum-distance-between-bst-nodes.py) | Easy |
47+
| 875 | Koko Eating Bananas | [Ruby](./algorithms/ruby/0875-koko-eating-bananas.rb) [Python3](./algorithms/python3/0875-koko-eating-bananas.py) | Medium |
4748
| 904 | Fruit Into Baskets | [Ruby](./algorithms/ruby/0904-fruit-into-baskets.rb) | Medium |
4849
| 912 | Sort an Array | [Ruby](./algorithms/ruby/0912-sort-an-array.rb) | Medium |
4950
| 953 | Verifying an Alien Dictionary | [Ruby](./algorithms/ruby/0953-verifying-an-alien-dictionary.rb) | Easy |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Problem: 875. Koko Eating Bananas
2+
# URL: https://leetcode.com/problems/koko-eating-bananas
3+
4+
'''
5+
6+
Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.
7+
8+
Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
9+
10+
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
11+
12+
Return the minimum integer k such that she can eat all the bananas within h hours.
13+
14+
Constraints:
15+
16+
* 1 <= piles.length <= 104
17+
* piles.length <= h <= 109
18+
* 1 <= piles[i] <= 109
19+
20+
'''
21+
import math
22+
class Solution:
23+
def minEatingSpeed(self, piles: list[int], h: int) -> int:
24+
left, right = 1, max(piles)
25+
26+
def speedEnough(speed):
27+
return sum(math.ceil(p / speed) for p in piles) <= h
28+
29+
while left < right:
30+
mid = (left + right) // 2
31+
if speedEnough(mid):
32+
right = mid
33+
else:
34+
left = mid + 1
35+
return left
36+
37+
# ********************#
38+
# TEST #
39+
# ********************#
40+
41+
import unittest
42+
43+
class TestminEatingSpeed(unittest.TestCase):
44+
def test_minEatingSpeed(self):
45+
self.assertEqual(Solution.minEatingSpeed(self, [3, 6, 7, 11], 8), 4)
46+
self.assertEqual(Solution.minEatingSpeed(self, [30, 11, 23, 4, 20], 5), 30)
47+
self.assertEqual(Solution.minEatingSpeed(self, [30, 11, 23, 4, 20], 6), 23)
48+
49+
if __name__ == '__main__':
50+
unittest.main()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
# Problem: 875. Koko Eating Bananas
4+
# URL: https://leetcode.com/problems/koko-eating-bananas
5+
6+
=begin
7+
8+
Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.
9+
10+
Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
11+
12+
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
13+
14+
Return the minimum integer k such that she can eat all the bananas within h hours.
15+
16+
Constraints:
17+
18+
* 1 <= piles.length <= 104
19+
* piles.length <= h <= 109
20+
* 1 <= piles[i] <= 109
21+
22+
=end
23+
24+
# @param {Integer[]} piles
25+
# @param {Integer} h
26+
# @return {Integer}
27+
def min_eating_speed(piles, h)
28+
(1..piles.max).bsearch do |x|
29+
piles.sum { (_1 + x - 1) / x } <= h
30+
end
31+
end
32+
33+
# ********************#
34+
# TEST #
35+
# ********************#
36+
37+
require "test/unit"
38+
class Test_min_eating_speed < Test::Unit::TestCase
39+
def test_
40+
assert_equal 4, min_eating_speed([3, 6, 7, 11], 8)
41+
assert_equal 30, min_eating_speed([30, 11, 23, 4, 20], 5)
42+
assert_equal 23, min_eating_speed([30, 11, 23, 4, 20], 6)
43+
end
44+
end

0 commit comments

Comments
 (0)