BS-12. Koko Eating Bananas

  Рет қаралды 187,583

take U forward

take U forward

Күн бұрын

Пікірлер: 284
@abhaysinghrathore3192
@abhaysinghrathore3192 Жыл бұрын
give this man the best teacher award in the world
@30sunique78
@30sunique78 8 ай бұрын
In DSA sure is this best For development check chai aur code once
@vm1662
@vm1662 Жыл бұрын
Great tip! Whenever there is a possible range of answers then we can apply binary search.. Thanks a lot Striver!
@LokeshPandey-m1m
@LokeshPandey-m1m Жыл бұрын
never thought we could even solve this by binary search mind-blowing
@sauravchandra10
@sauravchandra10 Жыл бұрын
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
@keshav9541 6 ай бұрын
same
@pradyumnmulegaon385
@pradyumnmulegaon385 11 ай бұрын
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
@socify4410 11 ай бұрын
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
@tanishagarwal5992 11 ай бұрын
Why to calculate total hours just check if it any instance the req time crosses h... Return false
@ArunsinghParihar-j3j
@ArunsinghParihar-j3j 9 ай бұрын
When I was submitting my code, I encountered the same problem, Thanks buddy.
@tusharkhatri2921
@tusharkhatri2921 8 ай бұрын
Bro on LeetCode not working after 121 test cases.
@ranjeet_sohanpal
@ranjeet_sohanpal 8 ай бұрын
@@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
@mathguy198
@mathguy198 Жыл бұрын
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❤❤
@--Blood--Prince--
@--Blood--Prince-- Жыл бұрын
I see Koko is on a high carb diet😀😅
@prernagupta7729
@prernagupta7729 Жыл бұрын
😂
@SreeCharan-dx7oc
@SreeCharan-dx7oc 8 ай бұрын
🤣🤣
@dumpster-jackson
@dumpster-jackson 5 ай бұрын
Gay
@omkarshendge5438
@omkarshendge5438 4 ай бұрын
lmao but tbh koko is a monkey i think thats why he is eating bananas!
@kumarvansh007
@kumarvansh007 4 ай бұрын
@@dumpster-jackson wtf
@visheshagrawal8676
@visheshagrawal8676 Жыл бұрын
I have earlier solved the question but intuition of yours is truly amazing... Great Content 🔥🔥
@thenikhildaiya.
@thenikhildaiya. Жыл бұрын
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
@kumarshivam3661 Жыл бұрын
Bro please post the solution of code
@thenikhildaiya.
@thenikhildaiya. Жыл бұрын
@@kumarshivam3661 ** Brute Force : class Solution { public: int minEatingSpeed(vector& piles, int h) { int maxHours = -1 , k = 0 ; for(int i = 0 ; i < piles.size() ; i++) if(piles[i] > maxHours) maxHours = piles[i] ; for(int i = 1 ; i
@nostalgiccringeallhailchel3881
@nostalgiccringeallhailchel3881 Жыл бұрын
how far have you progressed in 3 weeks bro pls update
@thenikhildaiya.
@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
@nostalgiccringeallhailchel3881 Жыл бұрын
@@thenikhildaiya. Which topic is aggressive cows in?
@akashdutta1620
@akashdutta1620 11 ай бұрын
this was asked in an interview I came up with this solution without solving this before.. :)
@lshuarckyma
@lshuarckyma Ай бұрын
STRIVERS TIP always find out the range ie low and high for applying the binary search bcz it could even work for larger ranges but will be efficient for the lowest high or the highest low. TIP 2 the pointers high and low always end up at opposite polarity of the condition ie for ex here POINTER low from being the speed in which eating in time isnt possible end up to possible minimum so without using the ANS directly use the POINTERS
@CrazyHunk14
@CrazyHunk14 9 ай бұрын
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
@SuvradipDasPhotographyOfficial
@SuvradipDasPhotographyOfficial Жыл бұрын
The wait is finally over.... Thanks a ton striver❤
@impalash_ag
@impalash_ag 4 ай бұрын
Hi Raj, Both the BF and Optimal solutions will have an additional TC of O(n) to find the maximum element from the array.
@ArdentMusicLover
@ArdentMusicLover 2 күн бұрын
Thank you, Striver. Loved the intuition on "binary search on answers" and on how to pick low or high as final answer. One quick suggestion: The python code provided in the solution is INCORRECT because ceil computation needs to be float.
@ritiksoni5255
@ritiksoni5255 7 ай бұрын
Thankyou you so much finally able to understand the approach and logics for solving DSA!!
@shashwatkumar6965
@shashwatkumar6965 9 ай бұрын
Great video! Just wanted to add one thing, in the question's Constraints it's given that n
@sukhii0220
@sukhii0220 5 ай бұрын
just include the overflow condition in finding the hrs in the loop only if totalH >H break;
@Dipanshutripathi2407
@Dipanshutripathi2407 Жыл бұрын
I have no doubts after watching ur videos.
@Bigg_boss_trolls
@Bigg_boss_trolls 11 күн бұрын
Thanks a lot Striver!
@Amanali-rl9hw
@Amanali-rl9hw Жыл бұрын
finally found when to return the low and when to return the value high
@knowthrvdo
@knowthrvdo 9 ай бұрын
logic is same as finding first occurance THANK YOU
@varun1017
@varun1017 9 ай бұрын
use long long to calculateTotalHours instead of int to avoid overflow
@ayushgoel01
@ayushgoel01 Жыл бұрын
🔴Leetcode Solution:- class Solution { private: int maxi(vector arr){ int ans = INT_MIN; for(int i=0; i
@Manishgupta200
@Manishgupta200 Жыл бұрын
Rather than using ceil we can use "sum = sum + (v[i] - 1)/ mid + 1;" It's more optimal
@shikharjoshi2998
@shikharjoshi2998 Жыл бұрын
what's the logic behind the (v[i]-1) ??
@HemanthD-bf4he
@HemanthD-bf4he 8 ай бұрын
Can you explain the logic?
@cinime
@cinime Жыл бұрын
Understood! Wonderful explanation as always, thank you very very much for your effort!!
@visheshagrawal8676
@visheshagrawal8676 Жыл бұрын
If anyone was getting runtime error of integer overflow do change the datatype of totalhours to double or long long
@AtulKumar-c4x7l
@AtulKumar-c4x7l Жыл бұрын
understood Thank you striver for such an amazing explanation..
@tejanshsachdeva487
@tejanshsachdeva487 Жыл бұрын
The typecasted leet-code code: #include #include #include class Solution { private: int maxEl(vector& piles) { int maxi = INT_MIN; for (int i = 0; i < piles.size(); i++) { maxi = max(maxi, piles[i]); } return maxi; } long long totEl(vector& piles, int mid) { long long totH = 0; int n = piles.size(); for (int i = 0; i < n; i++) { totH += (piles[i] + mid - 1) / mid; // Updated calculation } return totH; } public: int minEatingSpeed(vector& piles, int h) { int low = 1; long long high = maxEl(piles); int ans = INT_MAX; while (low
@Jus-chill
@Jus-chill Жыл бұрын
Understood Please do upload videos consistently 😊
@debanjanghosal618
@debanjanghosal618 2 ай бұрын
I solved this problem but I cant get over 1 doubt? Why are we setting low to 1? Won't it be better if we set low as the minimum number of bananas among all the piles?
@rahulreddyambati9065
@rahulreddyambati9065 3 ай бұрын
A typical De-Shaw intern OA question
@aniketgupta8064
@aniketgupta8064 Жыл бұрын
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
@thegame587 Жыл бұрын
bool check(vector& piles,int h,int k){ long long ans=0; for(int i=0;ih)return false; } //cout
@aniketgupta8064
@aniketgupta8064 Жыл бұрын
@@thegame587 Thanks a lot buddy for replying, it's working now. I had figured the overflow problem :)
@jayant-baid
@jayant-baid Жыл бұрын
@@aniketgupta8064 you can also make the func dataype, totalH datatype to long, this can also works
@aniketgupta8064
@aniketgupta8064 Жыл бұрын
@@jayant-baid thanks 👍
@MYMIND252
@MYMIND252 Жыл бұрын
@@jayant-baid thank you bro i stuck in this problem for more than hour and you save me from it 😊😊
@joeljacob4685
@joeljacob4685 Жыл бұрын
Thanks so much!! I was able to solve it without watching the video !!
@DikshaNarang-q9l
@DikshaNarang-q9l Жыл бұрын
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
@prthmsh2930 10 ай бұрын
O(n*log(max) >>> O(n) :)
@rushidesai2836
@rushidesai2836 6 ай бұрын
public int findHours(int[] arr, int bananaCount) { int totalHrs = 0; for (int i = 0; i < arr.length; i++) { totalHrs += Math.ceil((double) arr[i] / bananaCount); } return totalHrs; } Cast to double, because ceil here needs floating-point number.
@hareshnayak7302
@hareshnayak7302 8 ай бұрын
Understood, Thanks striver for this amazing video.
@Shikaslad-s5f
@Shikaslad-s5f 6 ай бұрын
To reduce runtime one can use instead of ceil ans += (piles[i]+k-1)/k;
@Surya1prakash
@Surya1prakash Жыл бұрын
Really amazing explanation 👏
@manavsingh5919
@manavsingh5919 Жыл бұрын
Thank you striver....Understood everything🙂...keep up the good work
@padmavatishah9922
@padmavatishah9922 8 ай бұрын
fr?
@abdussalam-4836
@abdussalam-4836 4 ай бұрын
why i feel like i am watching these videos again
@tanujaSangwan
@tanujaSangwan 2 ай бұрын
Solved this without watching the video myself
@AyushSharma-sd1ny
@AyushSharma-sd1ny Жыл бұрын
Great explanation Striver
@abhaythakur2597
@abhaythakur2597 Жыл бұрын
very helpful explaination
@RaviRavi-kt9gt
@RaviRavi-kt9gt Жыл бұрын
You are just incredible ❤️🎉🎉
@aritramallik8173
@aritramallik8173 Жыл бұрын
Dada tumi best❤❤❤
@yashendrabadal4776
@yashendrabadal4776 5 ай бұрын
Koko for real is gonna shit the whole next day ! AWESOME VID STRIVER
@AnmolGupta-oj4lm
@AnmolGupta-oj4lm Жыл бұрын
Understood Very Well!
@arvindokram2906
@arvindokram2906 3 ай бұрын
Got asked this question today!
@jhanavikumari274
@jhanavikumari274 4 ай бұрын
it is actually similar to finding the smallest divisor than threshold question literally
@Thescienceworld652
@Thescienceworld652 Жыл бұрын
i was doing this wrong for three hours and what i was doing wrong was only not applying double🙂
@pradipkumarmukhi
@pradipkumarmukhi Ай бұрын
Understood Man You damn it!!!
@sateeshkumar1634
@sateeshkumar1634 9 ай бұрын
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....
@aaranyaksantra9933
@aaranyaksantra9933 5 ай бұрын
nice explaination
@harshitjaiswal9439
@harshitjaiswal9439 Жыл бұрын
Understood!
@ShivamMaurya-fq6ws
@ShivamMaurya-fq6ws Жыл бұрын
@striver thanks for explanation
@A_Myth963
@A_Myth963 8 ай бұрын
For people doing on leetcode and getting error after testcase 120 ...just use long long hrtaken and also return long long.. Here's the code if someone needs it class Solution { public: long long hrtaken(vector pi, int h) { long long hr = 0; for (auto i : pi) { hr += ceil((double)i/(double)h); } return hr; } int minEatingSpeed(vector& piles, int h) { int maxt = -1; int m = -1; for (auto i : piles) m = max(m, i); int low = 1, high = m; while (low
@srinivasareddychalla-i2o
@srinivasareddychalla-i2o Жыл бұрын
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.
@culeforever5408
@culeforever5408 Жыл бұрын
understood 😇
@happylearning6476
@happylearning6476 11 ай бұрын
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
@umangkumar2005
@umangkumar2005 9 ай бұрын
little explanation solve the whole question #include using namespace std; bool fun(vector & v,int h ,int &ans, int mid){ int sum=0; for(int i=0;ih){ return true; } } ans=mid; return false; } int minimumRateToEatBananas(vector v, int h) { int ma=INT_MIN; for(int i=0;i
@AdityaYadav-qf9qc
@AdityaYadav-qf9qc Жыл бұрын
Best explaination😍
@pritivarse2129
@pritivarse2129 Жыл бұрын
Sir plz make videos on linked list and string part from A to Z dsa sheet
@_CodeLifeChronicles_
@_CodeLifeChronicles_ 2 ай бұрын
i got a q same like this in uber where hours were same but here it is a car and we need to find min kwperh so that the cars can get charged . but right now i am in my third year and gave uber in the beginning of the year and i didnt have any idea abt this. thank go i am on a strict plan of action now hope i will be able to solve q next time in OA
@ayushjaiswal7298
@ayushjaiswal7298 Жыл бұрын
Best explanation 🔥
@tanishkarawat5266
@tanishkarawat5266 Жыл бұрын
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?
@akshaykeswani3601
@akshaykeswani3601 Жыл бұрын
Really Amazing👏👏👏
@akashgite905
@akashgite905 4 ай бұрын
koko needs to stop eating those bananas
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Good explanation understood
@NazeerBashaShaik
@NazeerBashaShaik 7 ай бұрын
Understood, thank you.
@AnilKumar-mg8sj
@AnilKumar-mg8sj 2 ай бұрын
Thanks!
@hitmanop4078
@hitmanop4078 5 ай бұрын
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 ??.
@neelnsoni1313
@neelnsoni1313 Жыл бұрын
Thank You
@chethanprabhu4475
@chethanprabhu4475 4 ай бұрын
Should we not add the O(n) to time complexity to find the max element in the array
@debhagaming
@debhagaming Жыл бұрын
Nice explanation ❤
@yashraj5898
@yashraj5898 19 күн бұрын
to clear all the test cases . class Solution { public: int findmaxelement(vector& piles){ int maximum = INT_MIN; int n = piles.size(); for (int i=0; i
@gnanaphanideep6398
@gnanaphanideep6398 19 күн бұрын
Understood😁
@oyeesharme
@oyeesharme 3 ай бұрын
understood bhaiya
@YourCodeVerse
@YourCodeVerse 11 ай бұрын
Understood✅🔥🔥
@frostyfreezemovies
@frostyfreezemovies Жыл бұрын
I am using javascript😇
@Learnprogramming-q7f
@Learnprogramming-q7f 9 ай бұрын
Thank you bhaiya
@debanjan10
@debanjan10 Жыл бұрын
@DeadPoolx1712
@DeadPoolx1712 3 ай бұрын
UNDERSTOOD;
@Yahsaswi
@Yahsaswi Жыл бұрын
was waiting for this
@Aryan-j7i2b
@Aryan-j7i2b 22 күн бұрын
understood!!!
@ravisingla9032
@ravisingla9032 Жыл бұрын
Was Waiting for the same
@VishalGupta-xw2rp
@VishalGupta-xw2rp Жыл бұрын
16:50 Low opposite polarity
@sumitdon2004
@sumitdon2004 4 ай бұрын
striver is best
@harshilpatel3205
@harshilpatel3205 9 ай бұрын
Understood sir 🫡🤍
@lakshyabhati2262
@lakshyabhati2262 9 ай бұрын
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
@per.seus._
@per.seus._ Жыл бұрын
understood
@farzadkhan3813
@farzadkhan3813 Жыл бұрын
Understood!!
@amarsharma8582
@amarsharma8582 10 ай бұрын
**IMPORTANT** For CEIL VALUE you can use (a+b-1)/b this works well. example: arr = 2 3 6 8 mid = some value X then ceil value = (arr[i] + mid - 1)/mid
@senseiAree
@senseiAree Жыл бұрын
Understood ❤
@girikgarg8
@girikgarg8 Жыл бұрын
Done
@shashankvashishtha4454
@shashankvashishtha4454 5 ай бұрын
understand
@AbhishekBhattacharjee-j2m
@AbhishekBhattacharjee-j2m Жыл бұрын
UNDERSTOOD
@amaankhan9845
@amaankhan9845 Жыл бұрын
Following this series. Really helpfull 💪💪
@ziggywiggys
@ziggywiggys 5 ай бұрын
Why does it not work if I take the minimum eating speed as the minimum of the array ?
@abhishek_dagar
@abhishek_dagar Жыл бұрын
Leetcode solution all test cases passed : class Solution { public: int findmaximum(vector &piles){ int n = piles.size(); int maxi = INT_MIN; for(int i =0; i
@vrandakansal5940
@vrandakansal5940 Жыл бұрын
Kb tk playlist complete hogi?
BS-13. Minimum days to make M bouquets | Binary Search
26:01
take U forward
Рет қаралды 153 М.
BS-11. Find the Nth root of an Integer
20:44
take U forward
Рет қаралды 130 М.
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН
Can You Find Hulk's True Love? Real vs Fake Girlfriend Challenge | Roblox 3D
00:24
Мама у нас строгая
00:20
VAVAN
Рет қаралды 11 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 124 М.
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 236 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 17 М.
Koko Eating Bananas - Binary Search - Leetcode 875 - Python
15:12
BS-17. Aggressive Cows | Binary Search Hard
26:44
take U forward
Рет қаралды 176 М.
Koko Eating Bananas - Leetcode 875 - Binary Search (Python)
13:34
BS-10. Finding Sqrt of a number using Binary Search
17:11
take U forward
Рет қаралды 145 М.
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН