BS-12. Koko Eating Bananas
Problem Link: bit.ly/3CmCKVI
Notes/C++/Java/Python codes: takeuforward.org/binary-searc...
We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
Full Course: bit.ly/tufA2ZYt
You can follow me across social media, all my handles are below:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
0:00 Introduction of Course
Пікірлер: 232
I see Koko is on a high carb diet😀😅
@prernagupta7729
9 ай бұрын
😂
@SreeCharan-dx7oc
3 ай бұрын
🤣🤣
@dumpster-jackson
24 күн бұрын
Gay
@omkarshendge5438
16 күн бұрын
lmao but tbh koko is a monkey i think thats why he is eating bananas!
@kumarvansh007
5 күн бұрын
@@dumpster-jackson wtf
Great tip! Whenever there is a possible range of answers then we can apply binary search.. Thanks a lot Striver!
for those who are solving in leetcode the TotalH should be long long and also the CalculatetotalHours should also be long long type otherwise it will throw an error ..... hope it is helpful
@socify4410
7 ай бұрын
it was throwing run time error earlier. i thought i must be missing some edge case . after this change it got submitted. thanks to you buddy
@tanishagarwal5992
7 ай бұрын
Why to calculate total hours just check if it any instance the req time crosses h... Return false
@user-sm7zo5zd9t
4 ай бұрын
When I was submitting my code, I encountered the same problem, Thanks buddy.
@tusharkhatri2921
4 ай бұрын
Bro on LeetCode not working after 121 test cases.
@ranjeet_sohanpal
3 ай бұрын
@@tusharkhatri2921 you can make the condition in the calculation of hour .When the time taken exceeds h, you immediately return that val and prevent the overflow
Thanks Striver, always struggled to understand the reason behind the answer being always the low value, now it's clear as crystal, all thanks to you❤❤
I have earlier solved the question but intuition of yours is truly amazing... Great Content 🔥🔥
never thought we could even solve this by binary search mind-blowing
thanks to your intuition I could do this question on my own using the same approach that you explained in this video without any help! kudos
I remember doing this question earlier but the way you taught I am confident I can now easily solve problems like these in interviews with a clear and intuitive explanation. Understood, thanks!
@keshav9541
2 ай бұрын
same
The wait is finally over.... Thanks a ton striver❤
give this man the best teacher award in the world
@30sunique78
4 ай бұрын
In DSA sure is this best For development check chai aur code once
Thankyou you so much finally able to understand the approach and logics for solving DSA!!
I have no doubts after watching ur videos.
Understood! Wonderful explanation as always, thank you very very much for your effort!!
I just saw the question explanation and did the brute force and binary search approach all by myself just because of your previous videos explanation. Thank you so much for this wonderful and guided course.
@kumarshivam3661
Жыл бұрын
Bro please post the solution of code
@thenikhildaiya.
Жыл бұрын
@@kumarshivam3661 ** Brute Force : class Solution { public: int minEatingSpeed(vector& piles, int h) { int maxHours = -1 , k = 0 ; for(int i = 0 ; i if(piles[i] > maxHours) maxHours = piles[i] ; for(int i = 1 ; i
@nostalgiccringeallhailchel3881
Жыл бұрын
how far have you progressed in 3 weeks bro pls update
@thenikhildaiya.
Жыл бұрын
@@nostalgiccringeallhailchel3881 I did aggressive cows at last. Not good progress at all because of college exams🥲 Just revising old linked list questions I did in past.
@nostalgiccringeallhailchel3881
Жыл бұрын
@@thenikhildaiya. Which topic is aggressive cows in?
this was asked in an interview I came up with this solution without solving this before.. :)
understood Thank you striver for such an amazing explanation..
logic is same as finding first occurance THANK YOU
use long long to calculateTotalHours instead of int to avoid overflow
Thanks so much!! I was able to solve it without watching the video !!
finally found when to return the low and when to return the value high
Understood Please do upload videos consistently 😊
Understood, Thanks striver for this amazing video.
You are just incredible ❤️🎉🎉
Thank you striver....Understood everything🙂...keep up the good work
@padmavatishah9922
4 ай бұрын
fr?
Great video! Just wanted to add one thing, in the question's Constraints it's given that n
@sukhii0220
Ай бұрын
just include the overflow condition in finding the hrs in the loop only if totalH >H break;
Really amazing explanation 👏
Could you also please explain.Find K close elements with binary search.I've seen in some of the binary search approaches.Few of the solutions are with right=mid or left=mid.
Great explanation Striver
One doubt: what if h = 5 and piles = [30,11,23,4,20]here if we take k greater than 30 then also we'll get the answer. Then how can we apply that brute force where we are taking range of k to be [1, greatest_element] in that array?
Understood Very Well!
Someone please give a advice like how can u build a logic, how to revice sometimes I just go all blank even in some easy question what should I do ??.
hey striver, I feel that there is some problem with the leetcode question. for the testcase [805306368, 805306368, 805306368] and h = 1000000000, the expected answer is k=3 when we use an ans variable to store the desired value of mid, the code gives output = 1 which is incorrect, however, when we simply return the low, the testcase passes. I am not able to understand what is happening here. If this is a case of an overflow, I have already substitued long long. Pls help.
@thegame587
Жыл бұрын
bool check(vector& piles,int h,int k){ long long ans=0; for(int i=0;ih)return false; } //cout
@aniketgupta8064
Жыл бұрын
@@thegame587 Thanks a lot buddy for replying, it's working now. I had figured the overflow problem :)
@jayant-baid
Жыл бұрын
@@aniketgupta8064 you can also make the func dataype, totalH datatype to long, this can also works
@aniketgupta8064
Жыл бұрын
@@jayant-baid thanks 👍
@MYMIND252
9 ай бұрын
@@jayant-baid thank you bro i stuck in this problem for more than hour and you save me from it 😊😊
Should we not consider the + O(n) time to find the max element in the time complexity? Though i does not add to the over all complexity since we take the high value, still asking
very helpful explaination
can we do this by finding sum of elements in array and getting the ceil value by dividing it with number of hours which takes o(N) time complexity which is better than binary search.
If anyone was getting runtime error of integer overflow do change the datatype of totalhours to double or long long
Should we not add the O(n) to time complexity to find the max element in the array
9:54 at this while calculating time complexity of linear approach func will run for n times .but inside it ceil function will be used jiski time complexity log n hoti h so vo consider nahi krenge?
Why does it not work if I take the minimum eating speed as the minimum of the array ?
🔴Leetcode Solution:- class Solution { private: int maxi(vector arr){ int ans = INT_MIN; for(int i=0; i
@striver thanks for explanation
why this solution still falls in binary search. where we create extra memory for lowest pile 1 to highest pile 11 to get maximum pile per hour?
Hi Raj, Both the BF and Optimal solutions will have an additional TC of O(n) to find the maximum element from the array.
Hey striver, i am getting 1 test case wrong in this question, don't know why i still tried with your own code , but it is not working, nearly 3 problems have been in this way, any one please respond....
Really Amazing👏👏👏
Best explanation 🔥
Koko for real is gonna shit the whole next day ! AWESOME VID STRIVER
Best explaination😍
Understood, thank you.
ceil can be replaced with x / a + ( (x % a) != 0 ? 1 : 0)
how can we come to know that the given question is bs on answers.
Good explanation understood
Nice explanation ❤
nice explaination
was waiting for this
Sir plz make videos on linked list and string part from A to Z dsa sheet
while finding maximum element in an unsorted array it takes O(n) time so ultimately the time complexity of this question is O(n).
@prthmsh2930
6 ай бұрын
O(n*log(max) >>> O(n) :)
why do you return low, you didn't explained that part
To reduce runtime one can use instead of ceil ans += (piles[i]+k-1)/k;
Finding max will add O(n) to the time complexity.won't it?
Hi striver, i am experience developer approx 5 years, my query is how to get calls from recruiters
Understood!
how to tackle case of overflow in total hours calculated even if we use long long to calculate for total hours required it is giving wa?
@abhishek_dagar
11 ай бұрын
use long long toatl hours instead of int
Thank You
By what time, will the whole A2Z course will be completed?
Was Waiting for the same
Dada tumi best❤❤❤
Understood!!
Kb tk playlist complete hogi?
Understood✅🔥🔥
Understood ❤
understood 😇
Rather than using ceil we can use "sum = sum + (v[i] - 1)/ mid + 1;" It's more optimal
@shikharjoshi2998
8 ай бұрын
what's the logic behind the (v[i]-1) ??
@HemanthD-bf4he
4 ай бұрын
Can you explain the logic?
how to quickly understand that low is pointing to answer or high?
@abhishek_dagar
11 ай бұрын
dry run by taking 1 example
Thank you bhaiya
Can anyone explain why we have use double while using ceil function?
@abhishek_dagar
11 ай бұрын
#include using namespace std; int main() { int ans = ceil(4/(double)3); cout
understoood!!!
why low started from 1
but if the array is sorted then we don't need to find max of array .. its just the last element of the array .. i guess
Understood
understood
Giving wrong ans in this test case can anyone help
Understood!!!!!
public int findHours(int[] arr, int bananaCount) { int totalHrs = 0; for (int i = 0; i totalHrs += Math.ceil((double) arr[i] / bananaCount); } return totalHrs; } Cast to double, because ceil here needs floating-point number.
Following this series. Really helpfull 💪💪
UNDERSTOOD
Understood sir 🫡🤍
Understood:)
great
understand
sir even after submitting the exactly same code as yours...its getting wrong on 10th test case
@abhishekshinde5375
8 күн бұрын
add this condition to prevent overflow while calculating totalHours required to eat for the current rate if(timeReq > h){ break; } or use long long datatype every where
Why mid can't be our ans , why low
understood :)
i was doing this wrong for three hours and what i was doing wrong was only not applying double🙂
What to do if the totalH value exceeds int limits ?
@vipinshukla5546
Жыл бұрын
use long long instead of using int
@lucario404
Жыл бұрын
Actually I was looking for this.....when will the totalH value exceed int limit, can you explain me?
@111rhishishranjan2
Жыл бұрын
@@lucario404 #include #include #include class Solution { public: int minEatingSpeed(vector& piles, int h) { int s = 1; sort(piles.begin(), piles.end()); int ans = 0; int n = piles.size() - 1; int e = piles[n]; while (s
Thanks++
koko needs to stop eating those bananas
16:50 Low opposite polarity
spent whole day in this problem still could not do it
why i feel like i am watching these videos again