commit a4d22b3edc2fef7248cb54af161eac2ed5e230b5
parent d6685394d4975bbc29528490dd2ce179f82ef813
Author: Andrew Laack <andrew@laack.co>
Date: Thu, 12 Jun 2025 20:56:20 -0500
Finished banana problem...
Diffstat:
1 file changed, 48 insertions(+), 0 deletions(-)
diff --git a/koko-eating-bananas/eating.py b/koko-eating-bananas/eating.py
@@ -0,0 +1,48 @@
+import math
+
+class Solution:
+
+ # IDEA:
+
+ # Brute force:
+ # Check all values for k, starting from 1 to m := max(piles)
+ # and return smallest value
+
+ # Optimized approach:
+ # we can use a binary search from 1 to m,
+ # checking at each step if k is sufficient
+ # to solve the problem.
+ # If k solves the problem, check lower values
+ # If it doesn't, check higher values.
+
+ def minEatingSpeed(self, piles: List[int], h: int) -> int:
+
+ m = max(piles)
+
+ lower = 1
+ upper = m
+
+ smallest_k = upper
+ while lower <= upper:
+
+ center = ((upper - lower) // 2) + lower
+
+ if possible(piles, h, center):
+ smallest_k = center
+ upper = center - 1
+ else:
+ lower = center + 1
+
+ return smallest_k
+
+# h = hours
+# k = bph
+def possible(piles, h, k):
+
+ total_hours = 0
+ for pile in piles:
+ total_hours += math.ceil(pile / k)
+ if total_hours > h:
+ return False
+
+ return True