Solving Leetcode Interviews in Seconds with AI: Koko Eating Bananas
Introduction
In this blog post, we will explore how to solve the LeetCode problem "875" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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. 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. Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return. Return the minimum integer k such that she can eat all the bananas within h hours. Example 1: Input: piles = [3,6,7,11], h = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], h = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], h = 6 Output: 23 Constraints: 1 <= piles.length <= 104 piles.length <= h <= 109 1 <= piles[i] <= 109
Explanation
Here's a solution to the bananas problem:
- Binary Search: The core idea is to use binary search to find the minimum eating speed
k. The search space is between 1 (the minimum possible speed) and the maximum number of bananas in a pile (the maximum possible speed needed to finish withinhhours). isPossibleHelper Function: This function checks if Koko can eat all the bananas withinhhours given a specific eating speedk. It calculates the total time needed for all piles and compares it withh.Optimization: The
isPossiblefunction utilizes ceiling division ((pile + k - 1) // k) to efficiently compute the number of hours needed for each pile. This avoids floating-point arithmetic and provides an integer result directly.Time Complexity: O(N log M), where N is the number of piles and M is the maximum number of bananas in a pile.
- Space Complexity: O(1)
Code
def minEatingSpeed(piles: list[int], h: int) -> int:
"""
Finds the minimum eating speed k such that Koko can eat all bananas within h hours.
Args:
piles: A list of integers representing the number of bananas in each pile.
h: The maximum number of hours Koko has to eat all the bananas.
Returns:
The minimum integer k such that Koko can eat all the bananas within h hours.
"""
def isPossible(k: int) -> bool:
"""
Checks if Koko can eat all bananas within h hours given an eating speed k.
Args:
k: The eating speed.
Returns:
True if Koko can eat all bananas within h hours, False otherwise.
"""
hours = 0
for pile in piles:
hours += (pile + k - 1) // k # Equivalent to math.ceil(pile / k) but avoids floating-point
return hours <= h
left, right = 1, max(piles) # Define the search space
ans = right # Initialize with the maximum possible value
while left <= right:
mid = (left + right) // 2
if isPossible(mid):
ans = mid
right = mid - 1 # Try to find a smaller k
else:
left = mid + 1 # Need a larger k
return ans