Learn DSA
Depth-First Search
Greedy Algorithms
Binary Search
Apple Harvest (Koko Eating Bananas)
medium
DESCRIPTION (credit Leetcode.com)
Bobby has an orchard of apple trees, and each tree has a certain number of apples on it.
Bobby wants to collect all the apples by the end of the day by collecting a fixed number of apples per hour. He can only harvest apples from one tree per hour - if he finishes collecting apples from a tree before the hour is up, he must wait until the next hour to move to the next tree.
- For example, if there are 3 apples on a tree and Bobby collects 1 apple per hour, it will take him 3 hours to finish collecting the apples on that tree.
- If he harvests 2 apples per hour, it will take him 2 hours to finish collecting all the apples on that tree (he waits until the hour is up even though he finishes early).
Write a function to determine the slowest rate of apples Bobby can harvest per hour to finish the job in at most 'h' hours. The input to the function is an array of integers representing the number of apples on each tree and an integer 'h' representing the number of hours Bobby has to finish the job within.
Example 1:
Input:
Output: 3
Explanation:
- 1 apple per hour: 3 hours for first tree, 6 hours the second tree, and 7 hours for third tree. This totals 16 hours, which is more than the 8 hours he has to finish the job. NOT VALID.
- 2 apples per hour: 2 + 3 + 4 = 9 hours, which is more than the 8 hours he has to finish the job. NOT VALID.
- 3 apples per hour: 1 + 2 + 3 = 6 hours, which is less than the 8 hours he has to finish the job. VALID.
- 4 apples per hour: 1 + 2 + 2 = 5 hours, which is less than the 8 hours he has to finish the job. VALID.
- 5 apples per hour: 1 + 2 + 2 = 5 hours, which is less than the 8 hours he has to finish the job. VALID.
Therefore, the minimum number of apples Bobby must harvest per hour to finish the job in 8 hours or less is 3.
Example 2:
Input:
Output: 25 (Bobby must harvest 25 apples per hour to finish in 5 hours or less)
Explanation
Brute-Force Approach
- The slowest rate at which can harvest apples is 1.
- The fastest rate at which can harvest apples is equal to the maximum number of apples in the orchard, as going any faster than this will not change the time taken to harvest all the apples.
- If that time is less than or equal to the given time limit, then we've found the slowest rate at which we can harvest all the apples in the given time limit, so we return this rate.
- If the time taken is greater than the given time limit, then we increase the rate by 1 and repeat.
Calculating Time Taken
def time_taken(rate):time = 0for i in range(len(apples)):time += (apples[i] + rate - 1) // ratereturn time
Brute-Force Solution
def min_rate(apples, h):def time_taken(rate):time = 0for i in range(len(apples)):time += (apples[i] + rate - 1) // ratereturn timerate = 1while time_taken(rate) > h:rate += 1return rate
Binary Search Approach
Reducing Search Space
- Set mid = (left + right) / 2 = 8 apples per hour.
- This is fast enough (T), so we set right = mid.
- Set mid = (right + left) / 2.
- This is still fast enough (T), so set right = mid.
Optimal Solution
class Solution:def minHarvestRate(self, apples, h):# Binary search on harvest rate: find minimum rate to finish in h hoursdef time_taken(rate):time = 0# Calculate total time needed at this harvest ratefor i in range(len(apples)):# Ceiling division: (apples[i] + rate - 1) // ratetime += (apples[i] + rate - 1) // ratereturn time# Binary search bounds: minimum rate = 1, maximum rate = max applesleft, right = 1, max(apples)# Binary search for minimum valid harvest ratewhile left < right:mid = (left + right) // 2if time_taken(mid) > h:# Rate too slow, need faster rateleft = mid + 1else:# Rate is sufficient, try slower rateright = midreturn left
Complexity Analysis
Login to track your progress
Your account is free and you can post anonymously if you choose.