Boyer Moore Majority Vote Algorithm

  Рет қаралды 20,418

AlgosWithMichael

AlgosWithMichael

Күн бұрын

Пікірлер: 62
@aliyazdizadeh
@aliyazdizadeh 10 ай бұрын
I was so confused since I could see many examples where this fails but there 2 important things here: 1. If there is no majority element it will return some wrong value 2. So there has to be a verification step at the end which is also O(n)
@chitalvision
@chitalvision Жыл бұрын
I have been through a lot of explanations for this problem. Yours has been the easiest to understand and follow through.
@Texa8
@Texa8 2 ай бұрын
Instead of 2, 1, 2, 2, 2, 1, 1, 3, 2 if the array was 2, 1, 2, 2, 2, 1, 1, 3, 3, it would give the majority element as 3... At least according to your definition, which to me seems incorrect
@mapl3man237
@mapl3man237 23 сағат бұрын
In this problem you assume that a majority element always exists, if there were only four 2s there wouldn't be a majority element.
@anujonthemove
@anujonthemove 2 жыл бұрын
A much better explanation than most others on YT.
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Hey thanks
@hhjdkdnchidnd
@hhjdkdnchidnd Жыл бұрын
Totally agree, really good presentation
@Aditya-jv9mp
@Aditya-jv9mp 4 жыл бұрын
I'll be patiently waiting for you too have 100k+ subscribers! I love your content. I'm part of the early notification squad!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
That is very nice of you, thank you for the encouragement!
@noodlespwn42
@noodlespwn42 3 жыл бұрын
Your explanation and code don't add up. Can you please explain why we can skip the element that takes the count back to 0 as a majority element candidate? It looks like that's what your solution is saying. Either that or can you provide a solution that actually goes inline with your algorithm explanation? Any help is appreciated.
@xnl3358
@xnl3358 3 жыл бұрын
for the example of 2, 1, 2, 2, 2, 1, 1, 3, 2... base on the code you write. after the first "1" at index 1. before, moving to index 2, the candidate is still 2, but the count will be 0 correct? it won't change to candidate 1, count 1
@julianguyen7200
@julianguyen7200 2 жыл бұрын
this
@saulgoodman6710
@saulgoodman6710 Жыл бұрын
It shouldnt matter since we're foccused on values which are majority in number not the one which are equal.
@araneuskyuro
@araneuskyuro 2 ай бұрын
Fantastic explanantion, thanks mate
@neobrute-ek1bf
@neobrute-ek1bf 28 күн бұрын
An addition to verify if the candidate is really the majority element: We go through the array a second time and only count how often the candidate element (we found in the first run) is in the list. When we count it over N/2 times, then it is really the majority element, if not then we know there is no majority element in this list. Since we only have two runs through the array, we have 2*N comparisons, means its still linear runtime O(n).
@AlgosWithMichael
@AlgosWithMichael 21 күн бұрын
Pro explanation
@mozabbbas
@mozabbbas 5 ай бұрын
I think you need to implement verification phase also By Reset count to 0. Traverse the array again to count the occurrences of the candidate.
@mozabbbas
@mozabbbas 5 ай бұрын
Consider the input array: [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8]. In this array, there is no majority element because no element appears more than ⌊11/2⌋ = 5 times. Your code might return 8 as the candidate, even though 8 does not appear more than half the time. This is reason you need to add verification phase
@captainmcduckyYT
@captainmcduckyYT 2 жыл бұрын
Thanks so much Michael
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
No problem
@meiantv9191
@meiantv9191 Жыл бұрын
in turkish we say that, "Explaining it as if you were telling it to an idiot. ". Thank you so much.
@DWEthiopia
@DWEthiopia 3 жыл бұрын
So I believe this algorithm ONLY works if it is given that each test case WILL HAVE GUARANTEED majority element. It wouldn't work if there wasn't a majority element. You clarified this in your video but just wanted to highlight how important that is.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Yep, thanks for highlighting the importance of that!
@ankitsingh-vg5uf
@ankitsingh-vg5uf 4 жыл бұрын
Hands down, one of the best channel
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you very much, that means alot
@heisenberg1844
@heisenberg1844 3 жыл бұрын
Another great video. Thanks a lot.
@AustinCS
@AustinCS 2 жыл бұрын
at 2:12 the order is messed up.
@manuj9570
@manuj9570 3 жыл бұрын
Your channel is so underrated !
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@englishonlyhere
@englishonlyhere 2 жыл бұрын
Thanks! I like very much your channel!
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Thank you!
@kingofgods898
@kingofgods898 3 жыл бұрын
Fucking brilliant. Great presentation as well. Good shit Muinos!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
I appreciate that!
@lostmeme9862
@lostmeme9862 2 жыл бұрын
I already caught the hare, how much more of this is out there.
@abhijit-sarkar
@abhijit-sarkar Жыл бұрын
Note that the comparisons are made between the current element and the candidate, _not_ between the current element and the previous element. If you miss this crucial point, you're going to get the algorithm wrong.
@pramodsharma4508
@pramodsharma4508 4 жыл бұрын
Great work...keep posting such videos...your channel will definitely grow..
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks a ton
@MayaBello
@MayaBello 4 жыл бұрын
you are incredible at explaining these concepts 😭👏🏿👏🏿👏🏿
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you very much!
@9392539968
@9392539968 3 жыл бұрын
some thing is wrong in this code if you have 1,3,3,3,4,4,2 you get output as 2 but it should be 3
@SasiKutti
@SasiKutti 2 жыл бұрын
Reason is, there is no majority element in 1,3,3,3,4,4,2. Since there are 7 elements in that array, a majority element should occur at least 4 times, but no element in the array occurs at least 4 times. As he mentions at 3:50, this algorithm works only if the array does have a majority element.
@kunal_chand
@kunal_chand 2 жыл бұрын
​@@SasiKutti Yes makes sense. And to detect the "no majority element case", we can iterate through the array again and check if the occurrence of the element (that we got at the end of the previous iteration) is greater than n/2.
@elpelicanojiji
@elpelicanojiji 11 ай бұрын
the java code is missing a final verification that the CANDIDATE COUNT is more than N/2, right? But still if the input would be for example: 1,2,1,2,1,2,1,2,1,2 the result would be 2 but the count would be 1, there is no way to assess its really a majority. Im confused, wouldnt it be more strict to keep a count of every element in a hash table?
@theghostwhowalk
@theghostwhowalk 4 жыл бұрын
Simple but can be tricky o(1) space in interview 😀
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yea, definitely a bit tricky
@sontran6673
@sontran6673 10 ай бұрын
If i have a array = [2,2,2,2,1,3,4,5,6]. That algorithm is correct ?
@jjkoh2495
@jjkoh2495 10 ай бұрын
No, because there is no majority element which is a given condition
@johntown4671
@johntown4671 9 ай бұрын
nice implementation but it does not work for a simple array of just [1,2]. The code should return None or Null because there is no majority. For this to work one needs to add code to re-count the candidate in the array and compare it to the floor of n/2 ( or n // 2 ) Then return the candidate if indeed the count > n // 2
@hariniravichandran4059
@hariniravichandran4059 4 жыл бұрын
Pls do some videos regarding Data structures and algorithms 😭 !!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yep many more to come
@kevinoliveira7194
@kevinoliveira7194 3 жыл бұрын
Hi, can you give me a hand if where this algorithm is usually applied?
@a4rdev
@a4rdev 10 ай бұрын
TY
@tathagatnegi5923
@tathagatnegi5923 4 жыл бұрын
Thanks👍
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem 👍
@sarthakmanojade4887
@sarthakmanojade4887 3 жыл бұрын
A small optimization could be to not update the candidate at the same index where count becomes zero, but on the next element. Just reduces a few operations that's all
@princerajput9302
@princerajput9302 3 жыл бұрын
Your teaching style is fabulous. Love from India.
@saichennu5288
@saichennu5288 4 жыл бұрын
Bro please upload some dp based problems
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Definitely man
@sameer9368
@sameer9368 2 жыл бұрын
You are the best...I am from India...plz post such coding video regularly
@NghiaTran-er5mp
@NghiaTran-er5mp 3 жыл бұрын
Your explain is not cover enough how this algo work. You just point out the easiest case without bring out the myth behind or some defense for all cases. Not convincing to me.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
The point of this video was not to go over a proof, but to understand the algorithm itself. A follow up video could be done though to elaborate on why this works in all cases, thanks for the feedback
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 112 М.
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 64 МЛН
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 28 МЛН
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 9 МЛН
Amazon Coding Interview Question - First Missing Positive (LeetCode)
20:47
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 269 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 166 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 453 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 620 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 123 М.
Boyer-Moore Majority Vote Algorithm | Stop Motion
5:19
Fatima Nazir
Рет қаралды 389
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 64 МЛН