Skip to content

Commit

Permalink
Merge pull request #204 from Dhandeep10/875-KEB
Browse files Browse the repository at this point in the history
875. Koko Eating Bananas Issue #203
  • Loading branch information
iamdestinychild authored Oct 6, 2023
2 parents 7a265f1 + 58466cf commit 4c7341f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
17 changes: 17 additions & 0 deletions LeetCode/Koko Eating Bananas/C++/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Approach

1. The minEatingSpeed function takes the list of piles and the target number of hours as input. It initializes two variables, low and high, for binary search. low is initialized to 1, which represents the minimum possible eating speed, and high is initialized to the maximum number of bananas in any pile.
2. The binary search is performed in the while loop as long as low is less than or equal to high. The idea behind binary search is to narrow down the range of possible eating speeds where we can achieve the desired target hours.
3. In each iteration of the binary search, it calculates the middle value mid between low and high.
4. It then calls the getHoursToEatAll function with the current mid value, which calculates the total hours needed to eat all the piles at the given eating speed. This function iterates through each pile, calculates the hours required to eat that pile at the given speed, and accumulates the total hours.
5. If the calculated hoursToEatAll is less than or equal to the target hours, it means that we can eat all the piles at this speed, so we update the ans variable to the current mid value (potential minimum eating speed). Then, we narrow down the search range by setting high to mid - 1 to explore lower eating speeds.
6. If hoursToEatAll is greater than the target hours, it means that we need to increase the eating speed, so we set low to mid + 1 to explore higher eating speeds.
7. The binary search continues until low is no longer less than or equal to high. At this point, ans will contain the minimum eating speed that allows us to eat all the piles within or equal to the target hours, and it is returned as the result.


## Intuition

- The binary search approach leverages the fact that eating speed is a parameter that can be binary searched to find the optimal solution efficiently.
- By starting with a wide range of possible eating speeds (low to high) and iteratively narrowing it down, the algorithm efficiently finds the minimum eating speed that meets the target hours requirement.
- The getHoursToEatAll function calculates the total hours needed to eat all piles at a given eating speed. This is crucial for the binary search to make decisions about whether to search for faster or slower eating speeds.
- Overall, the binary search algorithm helps optimize the eating speed to minimize the time taken to eat all the bananas within the specified target hours.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions LeetCode/Koko Eating Bananas/C++/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
long long getHoursToEatAll(vector<int>&piles, int bananasPerHour)
{
long long totalHours = 0;
for (int i = 0; i < piles.size(); i++)
{
int hoursToEatPile = ceil(piles[i] / (double)bananasPerHour);
totalHours += hoursToEatPile;
}
return totalHours;
}

int minEatingSpeed(vector<int>& piles, int targetHours)
{
int low = 1, high = *(max_element(piles.begin(), piles.end()));
int ans = -1;

while(low <= high)
{
int mid = low + (high - low) / 2;
long long hoursToEatAll = getHoursToEatAll(piles, mid);

if (hoursToEatAll <= targetHours)
{
ans = mid;
high = mid - 1;
}
else low = mid + 1;
}
return ans;
}
};
33 changes: 33 additions & 0 deletions LeetCode/Koko Eating Bananas/PROBLEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 875. Koko Eating Bananas

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.


## Examples
```
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

https://leetcode.com/problems/koko-eating-bananas/description/?envType=study-plan-v2&envId=leetcode-75

0 comments on commit 4c7341f

Please sign in to comment.