Koko Eating Bananas - (Leetcode - 875) - (Google, Microsoft) : Explanation ➕ Live Coding
Vložit
- čas přidán 6. 03. 2023
- This is the 11th Video on our Binary Search Playlist.
In this video we will try to solve a very easy but interesting Binary Search problem "Koko Eating Bananas" (Leetcode-875).
Problem Name : Koko Eating Bananas
Company Tags : Google, Microsoft
My solutions on Github : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/koko-ea...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Bhaiya bus waali problem samjhne ke baad yeh wala khud se kar paya mein thank you bhaiya 🥰
I feel like am I growing with you bro.
Solved because of your Binary Search playlist. You have come to us like a boon
Please continue the graph concept series also🙂
Yes, will be resuming that as well
Happy to solve it by myself because of yesterday's explanation, thank you soo much
bro best again that's all. even today i dont searched by name on youtube i just arrived to your channel.
Means a lot ❤️
Amazing explanation 🌹
Bhai huge respect aj holi k din bhi app videos bana rahe❤
Together we will grow ❤️💪
You can do the for loop in one line. 👍
actualHours += celi(x/mid)
OR
actualHours += (x + mid - 1) / mid;
OR
actualHours = 1 + ((x - 1) / mid); // if x != 0
binge watching this whole playlist. Loved it
Awesome!!!
same as yesterday only difference at 20:13
Indeed Yash 👍🏻💪
Bahut badhiya aapane samjhaya hai
Attempted after watching your approach -
def canEatInHours(self, piles, k, hours):
count = 0
for bananas in piles:
count+=ceil(bananas/k)
return count int:
low, high = 1, max(piles)
optimalMid = high
while low
Meri Jaan mjaaa aa gya yaarrr Explanation me.
Is concept ko shyd "Binary Search Over Answer" bolte hain, maine khi pdha tha pehle lkn tb smjh ni aaya tha.Aaj aapki video se smjh aaya.
Thanks Bawa and God bless you.
Bhaiya aap ko kaise pta ki koko she hi hai mazak kar raha.
But aapne aacha samajhaya question pure intutiton se line by line.
Depth concept hai or important bhi .Isliye aapse samjha mene.
😅 Thank you Vivek ❤️
Why are we using l < r rather than l
can we use ceil value instead of dividing then taking modules
please reply??
Thanks for the video. It is so good.
I just have one query : When we know that koko can eat mid number of Bananas, why are we setting our right pointer to mid and not 'mid-1'? I tried setting it to mid-1 but ofcourse it does not work. I know that in binary search we set mid-1 and mid+1, tried using same intuition here.
Can you please explain why we are not setting r to mid-1 and why it does not work.
I got the issue.
The issue is not with setting r to mid -1. The issue is that we are using 'int' to calculate the totalHoursRequiredToEatBanana. There is a test case - if you eat 1 banana so eventually will endup adding all the piles' element which will go out of 'int' limit and it will give 'true' because negatve value will always be less than h. So I used 'long' and it worked.
Test Case : {805306368,805306368,805306368}, k = 1000000000
Because here in problems based on “Binart Search On Answer” , we never skip mid value, because mid can also be a possible answer for our problem.
exactly , like yesterday's logic
Here is the javascript code for the above problem
var minEatingSpeed = function(piles, h) {
let l = 1;
let r = Math.max(...piles);
while(l < r){
let m = Math.floor(l + (r-l)/2);
let x = piles.reduce((acc,curr) => {
return acc + Math.ceil(curr/m);
},0);
if(x
konsa tha yesterday wla ? pls btado
EASY++;
how one will get to know ke it is binary search question, or it can be solved by two pointers or stack or it can be solved by BFS or dp? ..cause there are many questions where you need to find "minimum" ?
when a ceratain range gives u a vakid ans , and after a point it dont satisfy , we use BS.
bhaiya ye r==mid kyun kiya hai r=mid-1 kyun nhi kiya hm poore right half ko ignore kyun nhi kr de rhe?i always get confuse over here.
Because if you notice, that mid was a valid value (could eat all bananas in time
@@codestorywithMIK okay bhaiya i get it.Thanks a lot✌
bro ek question he maximum product subarray uski vedio he to link share karo nhi to please solve it
Noted.
Will soon upload this
Sir brute force ka code bhi smjaao
For brute force , you can start from maximum banana that can be eaten and then check for each time. If the value works fine, then decrement it and keep checking until you get the optimal one.
Code below :
class Solution {
public:
int minEatingSpeed(vector& piles, int h) {
int per_hour_banana = *max_element(begin(piles), end(piles));
while (per_hour_banana > 0) {
int count = 0;
for (int pile : piles) {
count += pile/per_hour_banana;
if(pile%per_hour_banana != 0)
count++;
if (count > h)
return per_hour_banana + 1;
}
per_hour_banana--;
}
return 0;
}
};
I was also able to solve this problem myself.😀😀
Java Code:
class Solution {
boolean canEatAllBananas(int[] piles, int speed, int target_hour){
int req_hour = 0;
for(int i = 0; i < piles.length; i++){
req_hour += Math.ceil(piles[i]*1.0/speed*1.0);
}
return req_hour
Awesome 😍🙏