LeetCode 169. Majority Element - Interview Prep Ep 73 | Boyer-Moore majority vote algorithm

  Рет қаралды 12,837

Fisher Coder

Fisher Coder

Күн бұрын

Пікірлер: 60
@DaBigWang
@DaBigWang 8 күн бұрын
you're vids are great even today, ty fisher
@mikedepacina8588
@mikedepacina8588 11 ай бұрын
You're channel is underrated. I find your explanations more understandable and clearer than more famous channels😅
@FisherCoder
@FisherCoder 11 ай бұрын
Glad you like them!
@sugandhm2666
@sugandhm2666 3 жыл бұрын
Nicely explained steve. This channel is a goldmine for leetcode problems
@FisherCoder
@FisherCoder 3 жыл бұрын
Thanks and glad you found it helpful!
@nishant07kumar
@nishant07kumar 4 жыл бұрын
Like your approach for giving multiple solution of a problem and explanation of each problem.
@FisherCoder
@FisherCoder 4 жыл бұрын
Glad you found it helpful!
@victormiene520
@victormiene520 2 жыл бұрын
Bless you
@DecentProgrammer
@DecentProgrammer 2 жыл бұрын
Best content. Thanks for the video brother.
@FisherCoder
@FisherCoder 2 жыл бұрын
Glad you liked it!
@QQ-ps2vs
@QQ-ps2vs 3 жыл бұрын
The best explanation of 169 so far
@FisherCoder
@FisherCoder 3 жыл бұрын
Glad you found it helpful!
@KaisarAnvar
@KaisarAnvar 2 жыл бұрын
Subscribed, and I don't regret it. You're a genius....
@FisherCoder
@FisherCoder 2 жыл бұрын
Glad it was helpful!
@victormiene520
@victormiene520 2 жыл бұрын
Bless you!
@SK-yb7bx
@SK-yb7bx 2 жыл бұрын
Well explained. I understand how this works now. Subscribed.
@dinamohamed13600
@dinamohamed13600 Жыл бұрын
I Like your videos, you make things so easy. good job
@FisherCoder
@FisherCoder Жыл бұрын
Glad to hear that!
@thomasstephenson4043
@thomasstephenson4043 2 жыл бұрын
ok thats pretty cool. you are a good teacher. thank you
@FisherCoder
@FisherCoder 2 жыл бұрын
Glad it was helpful!
@VasheshJ
@VasheshJ 2 жыл бұрын
Thanks this video was helpful. Could you make a video on segment tree?
@sharanvkt
@sharanvkt 2 жыл бұрын
thanks a lottt for this easy yet best explaination. you saved my whole day😊
@FisherCoder
@FisherCoder 2 жыл бұрын
Glad it helped!
@pds7890
@pds7890 4 жыл бұрын
Thank you!! It didn't strike me that I can also sort and get middle element!! 😊 Video was helpful!
@FisherCoder
@FisherCoder 4 жыл бұрын
Glad it helped!
@okey1317
@okey1317 3 жыл бұрын
But sorting will add more complexity right? Also is it possible to use in built sorting methods in coding interview?
@pds7890
@pds7890 3 жыл бұрын
@@okey1317 Yes it does add more complexity. I think the purpose of exploring various ways to solve a problem is to help us solve new problems. 😁
@lilaiyang5745
@lilaiyang5745 3 жыл бұрын
Clear explanation, I understood it just by watching once. Appreciate 💪
@FisherCoder
@FisherCoder 3 жыл бұрын
Glad it helped!
@lxkp3233
@lxkp3233 4 жыл бұрын
nice explanation! thanks for sharing this video!
@FisherCoder
@FisherCoder 4 жыл бұрын
Glad it was helpful!
@haolicoder-5898
@haolicoder-5898 3 жыл бұрын
You are great!
@FisherCoder
@FisherCoder 3 жыл бұрын
Glad it's helpful!
@Sree-uv9bd
@Sree-uv9bd 4 жыл бұрын
Excellent explanation ! :) thank you
@FisherCoder
@FisherCoder 4 жыл бұрын
Glad it was helpful!
@jitinroy2246
@jitinroy2246 2 жыл бұрын
awesome
@chaitanyarao6045
@chaitanyarao6045 4 жыл бұрын
Hi There, Thanks for the explanation, it’s pretty clear and to the point. Would you mind explaining other areas/questions where this algorithm can be utilized.
@vijayap1929
@vijayap1929 3 жыл бұрын
Amazing!
@FisherCoder
@FisherCoder 3 жыл бұрын
Thank you! Cheers!
@justinkuang9423
@justinkuang9423 4 жыл бұрын
Was looking for a more optimal space solution, thanks!
@zfarahx
@zfarahx 6 ай бұрын
I don't think you've added enough #fishercode tags in your video.
@RajeebLochanChand-b6b
@RajeebLochanChand-b6b 11 ай бұрын
nice man
@FisherCoder
@FisherCoder 11 ай бұрын
Thanks!
@guo7580
@guo7580 2 жыл бұрын
Nice explanation. A minor question: why when counter == 0, candidate is reset to nums[i] while we do not increase counter from 0 to 1 (I mean do "counter++"). Based on your explanation, this should be the case.
@johnathondouglass
@johnathondouglass 2 жыл бұрын
I don't think that works because then you will exit the if statement and possibly increase the counter by 1 again, I think you would be double counting if you included the counter++ in your if statement?
@ultrma
@ultrma 2 жыл бұрын
The initinal value of counter should be set as 0. Otherwise this fails at [6,5,5]
@ahsanhabibabir5887
@ahsanhabibabir5887 4 жыл бұрын
thnx
@lephuphan9062
@lephuphan9062 3 жыл бұрын
what is the web browser he used?
@sysybaba420
@sysybaba420 Жыл бұрын
at 6 20, you said counter start from 0 but int counter = 1 initially. I am confused, do you mind explaining? Great video btw thanks!
@rufan89
@rufan89 Жыл бұрын
Initially, you are considering the first element of the array, nums[0], as the candidate for the majority element. Since you've already considered an instance of that element, you initialize the counter to 1.
@amitbhattacharya356
@amitbhattacharya356 2 жыл бұрын
Nice explanation, but for this [10,9,9,9,10] as input I am getting the wrong answer.
@amitbhattacharya356
@amitbhattacharya356 2 жыл бұрын
class Solution { public int majorityElement(int[] nums) { int count = 1; int candidate = nums[0]; for(int i=1; i
@timwong2396
@timwong2396 2 жыл бұрын
@@amitbhattacharya356 Why set the count = 1? if [a, b, a, b], then candidate b has count 1? initial: candidate = 10 , count 1 i = 1 : candidate = 10, count = 0 ( candidate 9 !== 10) i = 2 : candidate = 9, count = 1 ( candidate 9 === 9) i = 3: candidate = 9 , count = 2 ( candidate 9 === 9) i = 4 : candidate = 9 , count = 1 (candidate 10 !== 9 ) so finally return candidate = 9
@georgetsiklauri
@georgetsiklauri 4 жыл бұрын
Well.. what if we're not guaranteed that the majority exists? then this algorithm doesn't work. Imagine the {2, 2, 3, 3, 1} scenario. 3s would balance out the 2s, and finally, 1 becomes the candidate with count 1. Does that mean, that 1 is the majority because of the counter is positive? no; does even the comparison counter>arr.length/2 give me the correct boolean (whether there is a majority present)? no. Your approach works but in a very limited scope. Would have been better to explain those cases as well.
@andywang6928
@andywang6928 3 жыл бұрын
5/2 = 2. Which one here has a total number larger than 2?
@georgetsiklauri
@georgetsiklauri 4 жыл бұрын
Your approach doesn't work, as I've already mentioned this in a below comment. For the better example, consider {1, 2, 3}: These are the steps taken according to your explanation: Step1: candidate = 1; counter = 1; Step2: candidate = 1; counter = 0; Step3: candidate = 3; counter = 1; and 3 is not a majority element here.
@ArielVolovik
@ArielVolovik 4 жыл бұрын
This algorithm does one thing and one thing only. If a majority element exists, it will return it. This leetcode questions says that *the majority element does exist* - therefore, it works. If you instead need to also check if the majority element exists, then you would go about it differently.
@gargichaurasia4103
@gargichaurasia4103 3 жыл бұрын
@@ArielVolovik Yes, then we have the possibility that this candidate might be or might not be majority element. So in order to verify this we need to traverse the array and keep counting the no of occurences of this particular candidate element. If the frequency is more than N/2 than yes this candidate element is the majority element otherwise there exist no majority element. I this should work.
@kshitijsingh1990
@kshitijsingh1990 2 ай бұрын
In that case, you can write a verification function. private static int majorityElement(int[] nums) { int candidate = 0; int count = 0; for (int num : nums) { if (count == 0) { candidate = num; } if (num == candidate) { count++; } else { count--; } } // Verify if the candidate is the majority element if (isMajority(nums, candidate)) { return candidate; } else { // Handle the case when no majority element is found throw new IllegalArgumentException("No majority element found"); } } private static boolean isMajority(int[] nums, int candidate) { int count = 0; int n = nums.length; // Count how many times the candidate appears in the array for (int num : nums) { if (num == candidate) { count++; } } // Return true if the candidate appears more than n/2 times return count > n / 2; }
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 64 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 11 МЛН
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 9 МЛН
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 112 М.
LeetCode 104. Maximum Depth of Binary Tree - Interview Prep Ep 65
14:20
Boyer Moore Majority Vote Algorithm
5:52
AlgosWithMichael
Рет қаралды 20 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 123 М.
Majority Element II | Brute-Better-Optimal
26:58
take U forward
Рет қаралды 205 М.
Boyer-Moore Majority Vote Algorithm | Stop Motion
5:19
Fatima Nazir
Рет қаралды 389