diff --git a/LeetCode/Koko Eating Bananas/C++/README.md b/LeetCode/Koko Eating Bananas/C++/README.md new file mode 100644 index 0000000..dc2de92 --- /dev/null +++ b/LeetCode/Koko Eating Bananas/C++/README.md @@ -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. \ No newline at end of file diff --git "a/LeetCode/Koko Eating Bananas/C++/Screenshot 2023-10-06 at 10.21.51\342\200\257PM.png" "b/LeetCode/Koko Eating Bananas/C++/Screenshot 2023-10-06 at 10.21.51\342\200\257PM.png" new file mode 100644 index 0000000..77a6043 Binary files /dev/null and "b/LeetCode/Koko Eating Bananas/C++/Screenshot 2023-10-06 at 10.21.51\342\200\257PM.png" differ diff --git a/LeetCode/Koko Eating Bananas/C++/solution.cpp b/LeetCode/Koko Eating Bananas/C++/solution.cpp new file mode 100644 index 0000000..0a1e80a --- /dev/null +++ b/LeetCode/Koko Eating Bananas/C++/solution.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + long long getHoursToEatAll(vector&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& 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; + } +}; \ No newline at end of file diff --git a/LeetCode/Koko Eating Bananas/PROBLEM.md b/LeetCode/Koko Eating Bananas/PROBLEM.md new file mode 100644 index 0000000..55cfe7a --- /dev/null +++ b/LeetCode/Koko Eating Bananas/PROBLEM.md @@ -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 \ No newline at end of file