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)
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!
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
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!❤
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 .
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❤
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.
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!❤❤
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.
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😇😇🤩🤩🤩.
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.
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.
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
@@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.
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.. 😍😍❤❤
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
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.
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
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
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.
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
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
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?
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?
Aggressive striver
😂😂😭
😂
😂😂😂
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)
but for this you need to traverse the array
@25:13 @rohitverma1057
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!
Series from Kunal are incomplete. Even after recent updates, its quite far from completion.
Neetcode is pretty good
@@albedo9617 yes
Exactly, Babbar hasn't covered topics in depth . Kunal of course hasn't completed the course
you are "The" best teacher In terms of explanation
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
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!❤
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 .
were you selected ?
@@kartikeyrana3736 yes , but no :). Moved to the next rounds but on hold due to hiring freeze!
this question is same as this!
I searched for aggressive cow solution and i got you THE BEST EXPLANATION
Just by looking at the comments, I wanted to give a try and got succeeded. Thanks Striver!!!
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❤
coded optimal in one go , without seeing video 😀 . THANKS A LOT STRIVER FOR EVERYTHING
After reading the problem statement, I didn't even understand it. It is crystal clear after watching your explanation! Thanks Striver :)
Clearly Understood Striver. Thanks for this awesome series.
Very good explanation Striver! Thanks a lot for these teachings. These are a great source of knowledge for tier-4 colleges too.
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.
Ya you can write but it wont make any difference in the time taken... The change in time taken is less than 1ms
The best explanation for Aggressive Cows problem!
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!❤❤
Understand everything
Min Of Max pattern super duper clear
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)
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.
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😇😇🤩🤩🤩.
Man, you're built different. You make everything understandable! Keep doing the same
Habibi issme thoda mushkil hua samjne me but dimak lagai toh samaj aagai ....tusssi ek number kaam karta hai ...acchi bideo banaata hai
Hanuman chalisa padhke iss lecture ko dekh samajh aa jayega
😂@@rintugayen2435
UNDERSTOOD...Thank You So Much for this wonderful video.............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
Best Explanation ...............
Best Course on youtube
Your explanation is so clear that even beginners can also understand easily.
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.
That's the main problem bro.
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.
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
@@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...
@@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.
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.. 😍😍❤❤
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
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.
Yes I find all medium problems quiet similar 😅 and I did it myself
5-10 were most difficult after that most were easy
The best explanation for aggressive cows problem🙂
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
Super Sir Understood Thankyou for this Lecture
I coded it on my own after you explained the approach
Understood! Super amazing explanation as always, thank you so so much for your continuous effort!!
Understood !!!!! Grateful of you, Striver🙏
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
Understood 👍
Another request, please please upload the strings playlist
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.
Understood...so simple explanation bhaiya💙
Loved the video. I was stuck in this problem. You have a way with explanations, thanks Striver.
someone give him a oscar for the best explanation
What a great explanation, thank you sir
Lecture successfully completed on 22nd October 2024🔥🔥
You are amazing Striver❤
Not gonna lie, Binary search made it look so simple.
Wonderful explaination! understood
Heyyy, Thanks for the polarity change explanation. I was shocked understanding the intution
understood
Thank you striver for such an amazing explanation
in the brute force approach the time complexity of the sorting is not included.
Is binary search that easy, or is it the sriver effect? 😊
yes it obviously strivers effect.
Definately his effect💯💯
woh sirf lagta hai, new problem try karo
@@varunaggarwal7126 true 😂
since we have to min of max distance we'll have to find divide the distances as equal as possible , then choose the minimum
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
Bht hi badiya padaya bhai, maja agya
clearly understood bro .awesome explanation!!!
explanation on top🔥🔥🔥🔥
understood, thanks for the perfect explanation
Just amazing content , I was unable to understand the question but now i am able to solve it. 💌💌💌🤟
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
This is a max(min) problem. This is because we are maximizing the minimum distance between any two cows.
Understood very good explanation than previous explanation
Understood,Thanks striver for this amazing video.
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
Taking high and low is logical and tricky to solve porblem in much more optimal way
Hello Striver,I am not able to solve the problems on my own what should i do?
yes yes, still watching and iked.. tysm
your dedication . god bless you
Today this problem was asked in Salesforce intern OA with the name of fighting children 😂 The joy of mapping the algorithm.
crystal clear sir
amazing explanation as always striver bhaiya
Thank you! Lots of gratitude 🌸
you are extradinary man!!💛💯
great explanation, easily understood...............
Thank u so much, u are very helpful
greeting from Egypt
Thank you Striver
superb explanation
almost did on my own. thanks striver
Similar Question on Leetcode : 1552. Magnetic Force Between Two Balls
Farthest would be (max - min) / (cows - 1)
Striver! What's next after this amazing BS Playlist?
best explanation and intuition
19:01 we are sorting as well. O(nlogn)
jai jinendra sir
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?
Understood !! Beautifully explained
great Explanation
luv u brother
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
amazing explanation thanks a lot .😊
In the canweplace func, is it okay to do if(cntcows==cows) return true, rather than >= ?
Can anyone help me understand why we need to sort the array of stalls in the first place? Thanks.
i am preparing for offcampus :) thank you for everything.
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?
Solve easy ones , once you have done around 50-60 easy questions then move to
Thank you soooooooo much 😁
can we do it using dp i tried it but wasn't getting the correct ans
understood 😇
Thank you so much sir🙏🙏🙏
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???
what about the time complexity of sorting ? we need to add it right ?
but for the first approach it is neccessary to sort ?
Understood Sir, Thank you
amazing question!