BS-17. Aggressive Cows | Binary Search Hard

Поділитися
Вставка
  • Опубліковано 28 лис 2024

КОМЕНТАРІ • 271

  • @sus_tha_coder
    @sus_tha_coder Рік тому +362

    Aggressive striver

  • @rohitverma1057
    @rohitverma1057 10 місяців тому +20

    13:24 like he says that range can be optimized. The more optimal lower bound for search space will be the Minimum(adjacent difference). Reason: Let's take array [100, 200, 240, 400, 800]. In this 40 would be the lower bound and not starting from 1. Because if you want to place minimum 2 cows (like question says). You cannot attain difference any lower than this in sorted array which is 40 in this case i.e. Minimum(adjacent difference)

    • @abhaychoudhary6746
      @abhaychoudhary6746 3 місяці тому +1

      but for this you need to traverse the array

    • @Aryan_o7
      @Aryan_o7 2 місяці тому +1

      @25:13 @rohitverma1057

  • @technicalteam3298
    @technicalteam3298 Рік тому +128

    you are "The" best teacher In terms of explanation
    I have tried many UA-camrs like Codehelp, kunal kushwaha but your way of explaining things is much better as you focus on approach not only on coding the things without visualizing possibilities
    may god bless you for helping many Tier 3 students like me for providing this premium content just for free!

    • @anshulgoel1940
      @anshulgoel1940 Рік тому +2

      Series from Kunal are incomplete. Even after recent updates, its quite far from completion.

    • @albedo9617
      @albedo9617 6 місяців тому +4

      Neetcode is pretty good

    • @rickk3300
      @rickk3300 5 місяців тому +1

      @@albedo9617 yes

    • @Josuke217
      @Josuke217 4 місяці тому +1

      Exactly, Babbar hasn't covered topics in depth . Kunal of course hasn't completed the course

  • @Bigg_boss_trolls
    @Bigg_boss_trolls 8 днів тому +2

    you are "The" best teacher In terms of explanation

  • @himanshurane4847
    @himanshurane4847 Рік тому +24

    I solved this question just after you explained the question, and this is because you explained previous questions so well, and this is the power of your teaching

  • @341yes
    @341yes Рік тому +36

    This man is magic when comes to a certain set of problems!!!.... Simply, impressed by him! The explanation easily depicts how much hardwork he has put in! HandsOff to you sir!❤

  • @visase2036
    @visase2036 Рік тому +23

    I have been asked a similar question when i appeared for Google , the question was :
    The city is affected with Covid 19 and hospitals are filling up with patients.
    As a doctor , you need to make sure the patients are treated well. To do so You have some patients and beds for them to be allocated and the survival rate of the patients will be higher only if they are placed as far as possible.
    Given m beds and n patients maximize the minimum distance between any two patients to increase the survival rate.
    Ans : Same Binary Search approach .

    • @kartikeyrana3736
      @kartikeyrana3736 Рік тому

      were you selected ?

    • @visase2036
      @visase2036 Рік тому

      @@kartikeyrana3736 yes , but no :). Moved to the next rounds but on hold due to hiring freeze!

    • @munvut877
      @munvut877 11 місяців тому

      this question is same as this!

  • @AkshayGoel-of8ic
    @AkshayGoel-of8ic Рік тому +4

    I searched for aggressive cow solution and i got you THE BEST EXPLANATION

  • @manojkumar-hr7tj
    @manojkumar-hr7tj Місяць тому +2

    Just by looking at the comments, I wanted to give a try and got succeeded. Thanks Striver!!!

  • @harshpanwar1550
    @harshpanwar1550 Рік тому +18

    You win our hearts in every video, specially the hard problems by explaining them in such a beautiful and simple manner.
    Thank you for your enormous contributions toward the Coding Community❤

  • @devgarg4331
    @devgarg4331 9 місяців тому +5

    coded optimal in one go , without seeing video 😀 . THANKS A LOT STRIVER FOR EVERYTHING

  • @lavanya_m01
    @lavanya_m01 7 місяців тому +5

    After reading the problem statement, I didn't even understand it. It is crystal clear after watching your explanation! Thanks Striver :)

  • @elizabethr5161
    @elizabethr5161 Рік тому +12

    Clearly Understood Striver. Thanks for this awesome series.

  • @reshmah1497
    @reshmah1497 25 днів тому +2

    Very good explanation Striver! Thanks a lot for these teachings. These are a great source of knowledge for tier-4 colleges too.

  • @aniketwdubey
    @aniketwdubey Рік тому +3

    In the context of this problem, you could also start with low = 1 since it doesn't make sense to have two rooms at the exact same position. The only reason to start with 0 is to provide a clear lower bound for the binary search. The result wouldn't be affected either way since the binary search would quickly discard unfeasible distances, but starting from 1 would be more intuitive in this context.

    • @soumojjalsen1915
      @soumojjalsen1915 10 місяців тому

      Ya you can write but it wont make any difference in the time taken... The change in time taken is less than 1ms

  • @aaryanjavalekar7249
    @aaryanjavalekar7249 Рік тому +3

    The best explanation for Aggressive Cows problem!

  • @pradipkumarmukhi
    @pradipkumarmukhi Місяць тому

    Grateful beyond words, Striver! You transformed the Aggressive Cows problem from a daunting challenge to a clear solution. Your explanation not only sharpened my DSA skills but also boosted my confidence in problem-solving. Huge thanks for your patience and precision!❤❤

  • @ujjawalraj6096
    @ujjawalraj6096 Рік тому +4

    Understand everything
    Min Of Max pattern super duper clear

  • @52nevil36
    @52nevil36 Рік тому +4

    Genereally i never do any comment to video but the guys was insane.. this made DSA too easy for mr (I feel slowly slowly DSA is cup of my v tea)

  • @NonameNoname-f2t
    @NonameNoname-f2t 9 місяців тому

    I am getting crazy with every next video full of excitement and respect for you. How easy , clean , structured the problem and its solution . Best part when the complete story ends with code same as the story we listen, we read it too by the code.

  • @priyanshigusain6722
    @priyanshigusain6722 2 місяці тому +2

    i am really really really X 100000
    speechless
    he is am amazing teacher
    means he makes the problem look like it's nothing
    i have never met such simplified teacher
    you are serendipity encounter for me😇😇🤩🤩🤩.

  • @revanthchouhan3068
    @revanthchouhan3068 3 місяці тому

    Man, you're built different. You make everything understandable! Keep doing the same

  • @CodeMode9313
    @CodeMode9313 Рік тому +5

    Habibi issme thoda mushkil hua samjne me but dimak lagai toh samaj aagai ....tusssi ek number kaam karta hai ...acchi bideo banaata hai

    • @rintugayen2435
      @rintugayen2435 4 місяці тому +1

      Hanuman chalisa padhke iss lecture ko dekh samajh aa jayega

    • @RaunitJaiswal-s9v
      @RaunitJaiswal-s9v 2 місяці тому

      😂​@@rintugayen2435

  • @stith_pragya
    @stith_pragya 9 місяців тому +1

    UNDERSTOOD...Thank You So Much for this wonderful video.............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @Video-Notes
    @Video-Notes 6 місяців тому

    Best Explanation ...............
    Best Course on youtube

  • @venkatamurthy6022
    @venkatamurthy6022 Рік тому +1

    Your explanation is so clear that even beginners can also understand easily.

  • @rickk3300
    @rickk3300 Рік тому +21

    I solved this question without watching the video just because of the fact that I knew that binary search on answers is going to be applied here, but my question is that how the hell can someone come up with the binary search approach if he/she doesn't know that it is going to be solved using binary search. When I first read this question, the first thought that came to my mind was applying dp.

    • @utsavseth6573
      @utsavseth6573 Рік тому

      That's the main problem bro.

    • @bishalkundu7592
      @bishalkundu7592 Рік тому +1

      That's the thing which comes with practice. The more you practice, the more you will solve problems on different topics and you will be able to find patterns in it.

    • @ronakraj
      @ronakraj 8 місяців тому +1

      hey @techmaniac2002 now it has been 8 months since you said this and i think you might have practiced many questions since then, and so i wanted to know if you can now find the pattern and say that this question is going to be solved by this approach. or are you still struggling

    • @rickk3300
      @rickk3300 8 місяців тому

      @@ronakraj Nope, I haven't practiced many questions on DSA since I got placed in the month of August 🙂 I practically left DSA/CP since then...

    • @chetanraghavv
      @chetanraghavv 6 місяців тому +5

      @@ronakraj I think clear distinction between when to apply binary search or DP is that: whenever you observe that answer always lie between a fixed range, and you are getting an answer till a specific point and not getting any answer after a certain value (this is known as monotonic function) then for sure Binary Search can be applied. Whereas in DP, you will be asked to find the optimal answer (minimum or maximum, just like Binary Search) but the problem would be such that you HAVE TO explore all the answers and only after that determine optimal one, this is where recursion comes in which is used to explore all possibilities and finally overlapping subproblems in recursion are optimized using DP. But these things take time and practice.

  • @souvikbiswas284
    @souvikbiswas284 2 місяці тому

    I understood the question at 07:20 and coded the answer. The question is almost similar to "Find the smaller divisor". All thanks to you Striver, your explanation of the approach has helped me to come up with the answer on my own.. 😍😍❤❤

    • @souvikbiswas284
      @souvikbiswas284 2 місяці тому

      Here's the Solution:
      class Solution:
      def aggressiveCows(self, nums, k):
      nums.sort()
      n = len(nums)
      l = 0
      r = nums[-1] - nums[0]
      while l > 1)
      cows_placed = 1
      last_pos = nums[-1]
      for curr in range(n-2, -1, -1):
      if last_pos - nums[curr] >= distance:
      cows_placed += 1
      last_pos = nums[curr]
      if cows_placed >= k:
      break
      if cows_placed < k:
      r = mid - 1

      return r

  • @manitoshpaul6170
    @manitoshpaul6170 5 місяців тому +9

    Is it only me or any one else have also felt that the binary search medium problems are a bit doable rather than the easy problems... They seem to be hard. However, doesn't this problem seems to be a. 90% similar to minimum days to ship d packages or m bouquet flowers problem.

    • @graviton001
      @graviton001 3 місяці тому

      Yes I find all medium problems quiet similar 😅 and I did it myself

    • @Fe-ironman
      @Fe-ironman Місяць тому

      5-10 were most difficult after that most were easy

  • @farheenahmed8286
    @farheenahmed8286 5 місяців тому

    The best explanation for aggressive cows problem🙂

  • @sxd6259
    @sxd6259 Рік тому +3

    i think you should also optimise the check function a bit more by using lowerbound instead of linear search for the next cow stall index, great solution thought

  • @shaikkhizar8133
    @shaikkhizar8133 9 місяців тому +1

    Super Sir Understood Thankyou for this Lecture

  • @pushpendrasingh777
    @pushpendrasingh777 2 місяці тому

    I coded it on my own after you explained the approach

  • @cinime
    @cinime Рік тому +3

    Understood! Super amazing explanation as always, thank you so so much for your continuous effort!!

  • @simransaini1227
    @simransaini1227 4 місяці тому

    Understood !!!!! Grateful of you, Striver🙏

  • @PriyamJamwal
    @PriyamJamwal 4 місяці тому

    based on AP, to optimize further you can set
    high = ceil((double)(stall[n-1] - stall[0]) / (double)c)
    stall is array of sorted stalls, c is no. of cows

  • @srija0608
    @srija0608 День тому

    Understood 👍
    Another request, please please upload the strings playlist

  • @AK-nj1je
    @AK-nj1je 8 місяців тому

    You're seriously a legend 🔥🔥🔥 you made me consistent to dsa
    Thanks a lot❤
    Those who cannot find this question on leetcode, there's a question as 1552. Magnetic Force Between Two Balls which is as same as this you can do that.

  • @naveensingh596
    @naveensingh596 Рік тому

    Understood...so simple explanation bhaiya💙

  • @rohakdebnath8985
    @rohakdebnath8985 11 місяців тому

    Loved the video. I was stuck in this problem. You have a way with explanations, thanks Striver.

  • @ritwik121
    @ritwik121 Рік тому

    someone give him a oscar for the best explanation

  • @sushantguria8820
    @sushantguria8820 Місяць тому

    What a great explanation, thank you sir

  • @patilrajesh2500
    @patilrajesh2500 Місяць тому

    Lecture successfully completed on 22nd October 2024🔥🔥

  • @umairislam8715
    @umairislam8715 29 днів тому

    You are amazing Striver❤

  • @rishabh1S
    @rishabh1S Рік тому +1

    Not gonna lie, Binary search made it look so simple.

  • @samarthpai5359
    @samarthpai5359 26 днів тому

    Wonderful explaination! understood

  • @PrajwalCoding
    @PrajwalCoding 11 місяців тому

    Heyyy, Thanks for the polarity change explanation. I was shocked understanding the intution

  • @AtulKumar-c4x7l
    @AtulKumar-c4x7l Рік тому +1

    understood
    Thank you striver for such an amazing explanation

  • @MaheshPatil-of1zy
    @MaheshPatil-of1zy 3 місяці тому +2

    in the brute force approach the time complexity of the sorting is not included.

  • @sauravchandra10
    @sauravchandra10 Рік тому +29

    Is binary search that easy, or is it the sriver effect? 😊

  • @samsmith3961
    @samsmith3961 Рік тому +1

    since we have to min of max distance we'll have to find divide the distances as equal as possible , then choose the minimum

  • @arjunthakur5827
    @arjunthakur5827 8 місяців тому

    My approach sort the array, create an array arrd with difference between consecutive elemnts of the array, if k==2 asign first and last (max distance) if more in arrd, sort it pop until k, the last k poped gives min distance

  • @nihaal4699
    @nihaal4699 8 місяців тому

    Bht hi badiya padaya bhai, maja agya

  • @krishnasharma-rn3sd
    @krishnasharma-rn3sd 4 місяці тому

    clearly understood bro .awesome explanation!!!

  • @RobinSingh-nf4oo
    @RobinSingh-nf4oo 2 місяці тому

    explanation on top🔥🔥🔥🔥

  • @kaichang8186
    @kaichang8186 Місяць тому

    understood, thanks for the perfect explanation

  • @shashikumar2382
    @shashikumar2382 9 місяців тому

    Just amazing content , I was unable to understand the question but now i am able to solve it. 💌💌💌🤟

  • @Musicuvakavi1823
    @Musicuvakavi1823 7 місяців тому

    Understood well Sir ... Thank you so much Striver Sir for making such an amazing video... After understanding the problem, I was able to solve without seeing the code Thanks a lot Sir

  • @debanjanghosal618
    @debanjanghosal618 Місяць тому

    This is a max(min) problem. This is because we are maximizing the minimum distance between any two cows.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому

    Understood very good explanation than previous explanation

  • @hareshnayak7302
    @hareshnayak7302 8 місяців тому

    Understood,Thanks striver for this amazing video.

  • @darrienxavier5536
    @darrienxavier5536 3 місяці тому

    In the Brute Force approach return i-1 won't work in all cases , we could use a variable to keep track of the distance and then return it

  • @Manishgupta200
    @Manishgupta200 Рік тому

    Taking high and low is logical and tricky to solve porblem in much more optimal way

  • @alishashaikh3859
    @alishashaikh3859 Рік тому +3

    Hello Striver,I am not able to solve the problems on my own what should i do?

  • @samreenimam8608
    @samreenimam8608 10 місяців тому

    yes yes, still watching and iked.. tysm

  • @Sahilsharma-sk5vr
    @Sahilsharma-sk5vr 5 місяців тому

    your dedication . god bless you

  • @satwiksrivastava
    @satwiksrivastava 4 місяці тому

    Today this problem was asked in Salesforce intern OA with the name of fighting children 😂 The joy of mapping the algorithm.

  • @varun_immadisetti
    @varun_immadisetti Місяць тому

    crystal clear sir

  • @sauravkumarjha6162
    @sauravkumarjha6162 Рік тому

    amazing explanation as always striver bhaiya

  • @bhagyashreekhairnar683
    @bhagyashreekhairnar683 9 місяців тому

    Thank you! Lots of gratitude 🌸

  • @aruna5869
    @aruna5869 5 місяців тому

    you are extradinary man!!💛💯

  • @javabytharun
    @javabytharun 5 місяців тому

    great explanation, easily understood...............

  • @abdulrhmanmagdy7591
    @abdulrhmanmagdy7591 Рік тому

    Thank u so much, u are very helpful
    greeting from Egypt

  • @chetnaraghav9979
    @chetnaraghav9979 17 днів тому

    Thank you Striver

  • @shantipriya370
    @shantipriya370 11 місяців тому

    superb explanation

  • @DM2K6
    @DM2K6 4 місяці тому

    almost did on my own. thanks striver

  • @prathameshthorat9350
    @prathameshthorat9350 4 місяці тому

    Similar Question on Leetcode : 1552. Magnetic Force Between Two Balls

  • @rajveersingh2056
    @rajveersingh2056 Рік тому +1

    Farthest would be (max - min) / (cows - 1)

  • @usmankhatri6836
    @usmankhatri6836 Рік тому +1

    Striver! What's next after this amazing BS Playlist?

  • @ishangujarathi10
    @ishangujarathi10 Рік тому

    best explanation and intuition

  • @faizanahmed9304
    @faizanahmed9304 Рік тому

    19:01 we are sorting as well. O(nlogn)

  • @GauravJain-zo8gt
    @GauravJain-zo8gt 5 місяців тому

    jai jinendra sir

  • @VarshaSingh-hi2sb
    @VarshaSingh-hi2sb 3 місяці тому

    Why we are always placing the cow at first index only. There can be combinations where placing at first and all cows are placed then also places would be left to place more cows . So it is also possible that if we started from positon other than first it might have possible to place all the cows so why always position 1?

  • @joeljacob4685
    @joeljacob4685 Рік тому

    Understood !! Beautifully explained

  • @dhruvmalik8052
    @dhruvmalik8052 Рік тому

    great Explanation
    luv u brother

  • @ashfakahamed9266
    @ashfakahamed9266 Рік тому +1

    If "Find min or find max" is what you will use to identify Binary Search then eventually you will get stuck in the DP problems

  • @dipingrover1970
    @dipingrover1970 5 місяців тому

    amazing explanation thanks a lot .😊

  • @ayushagarwal5271
    @ayushagarwal5271 5 місяців тому

    In the canweplace func, is it okay to do if(cntcows==cows) return true, rather than >= ?

  • @amit2197kumar
    @amit2197kumar 4 місяці тому +1

    Can anyone help me understand why we need to sort the array of stalls in the first place? Thanks.

  • @crazymemes4080
    @crazymemes4080 3 місяці тому

    i am preparing for offcampus :) thank you for everything.

  • @hamzarehman7057
    @hamzarehman7057 Рік тому +2

    Sir I had a query. Since I'm learning DSA for the first time. Should I solve the easy questions of all topics and then move to their medium and hard questions. or study a topic like easy>medium>hard and then next topic?

    • @ganakkathuria3645
      @ganakkathuria3645 Рік тому +1

      Solve easy ones , once you have done around 50-60 easy questions then move to

  • @priyankaranawat8373
    @priyankaranawat8373 5 місяців тому +1

    Thank you soooooooo much 😁

  • @ishaanchandak4774
    @ishaanchandak4774 Рік тому +1

    can we do it using dp i tried it but wasn't getting the correct ans

  • @culeforever5408
    @culeforever5408 Рік тому +1

    understood 😇

  • @rockykumarverma980
    @rockykumarverma980 2 місяці тому

    Thank you so much sir🙏🙏🙏

  • @Unplugged_Aryan
    @Unplugged_Aryan Місяць тому

    i understand things here along with code but after some time span I forget the approach and needs revision. how do you guys tackle this???

  • @lucy2003-d8p
    @lucy2003-d8p 10 місяців тому

    what about the time complexity of sorting ? we need to add it right ?

  • @manishkaushik6526
    @manishkaushik6526 Рік тому +1

    but for the first approach it is neccessary to sort ?

  • @Shunya_Advait
    @Shunya_Advait Рік тому

    Understood Sir, Thank you

  • @MJBZG
    @MJBZG 4 місяці тому

    amazing question!