Koko Eating Bananas Leetcode 875. Binary Search | Full C++ Code in Comments | Brute force to Optimal
Vložit
- čas přidán 27. 07. 2024
- Timestamps:
0:00 Reading out question
1:51 Brute force example
6:42 Intuition to optimize
8:55 Time Complexity
9:50 Optimized example
13:24 Code
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
Check out our other playlists:
Dynamic Programming:
• Dynamic Programming
Trees:
• Trees
Heaps and Maps:
• Heaps and Maps
Arrays and Maths:
• Arrays and Maths
Bit Manipulation:
• Bit Manipulation
Greedy Algorithms:
• Greedy Algorithms
Sorting and Searching:
• Sorting and Searching
Strings:
• Strings
Linked Lists:
• Linked Lists
Stack and Queues:
• Stacks and Queues
Two Pointers:
• Two pointers
Graphs, BFS, DFS:
• Graphs, DFS, BFS
Backtracking:
• Backtracking
Non- DSA playlists:
Probability:
• Probability
SQL-Basic Join functions:
• SQL - Basic JOIN Funct...
SQL-Basic Aggregate functions:
• SQL-Basic Aggregate Fu... - Věda a technologie
Code in C++ -> Time Complexity -> O(log(m)*n) where m is max element in array
int minEatingSpeed(vector& piles, int h) {
int low = 1;
int high = *max_element(piles.begin(),piles.end());
int ans = 0;
while(low
if mid==0 then sum go to infinit
mam pls explain minimum limit of balls in a bag
thanks for the solution! You deserve way more subscribers for your content.
tysm alisha for such a wonderful explaination.
awesome explanations mam
🤩🤩
much love to your work Alisha, you made my day ❤, Thanks a lot!!
you are great source strength and motivation for us ...
thanks for the explaination
best explanation of this problem so far
U are awesome explainer
Best explanation
simply superb
Explanation is damn good
class Solution {
public:
int minEatingSpeed(vector& piles, int h) {
int low = 1;
int high =10000000000;
int ans = 0;
while (low
Solution didnt work
Thankful for your wonderful efforts which help me to understand this problem
gajab yaar , ek number ../// thank u so mmuchh
amazing explanation and don't stop uploading
Great explanations !! _/\_
class Solution {
public:
int minEatingSpeed(vector& piles, int h) {
int low = 1;
int high = *max_element(piles.begin(),piles.end());
int ans = 0;
while(low
You are returning long long int in integer return type function
exactly , in leetcote as well as in gfg , answer is not coming out right by this solution
int k=0;
int left=1, right= 10000000000;
while(left
@@ashishkulkarni2350 This works! Thank you so much!
However,
Can you please provide the explanation of the line:
count+= (piles[i]+k-1)/k;
Great explanation, thanks a lot miss. 🙏🫡