You're channel is underrated. I find your explanations more understandable and clearer than more famous channels😅
@FisherCoder11 ай бұрын
Glad you like them!
@sugandhm26663 жыл бұрын
Nicely explained steve. This channel is a goldmine for leetcode problems
@FisherCoder3 жыл бұрын
Thanks and glad you found it helpful!
@nishant07kumar4 жыл бұрын
Like your approach for giving multiple solution of a problem and explanation of each problem.
@FisherCoder4 жыл бұрын
Glad you found it helpful!
@victormiene5202 жыл бұрын
Bless you
@DecentProgrammer2 жыл бұрын
Best content. Thanks for the video brother.
@FisherCoder2 жыл бұрын
Glad you liked it!
@QQ-ps2vs3 жыл бұрын
The best explanation of 169 so far
@FisherCoder3 жыл бұрын
Glad you found it helpful!
@KaisarAnvar2 жыл бұрын
Subscribed, and I don't regret it. You're a genius....
@FisherCoder2 жыл бұрын
Glad it was helpful!
@victormiene5202 жыл бұрын
Bless you!
@SK-yb7bx2 жыл бұрын
Well explained. I understand how this works now. Subscribed.
@dinamohamed13600 Жыл бұрын
I Like your videos, you make things so easy. good job
@FisherCoder Жыл бұрын
Glad to hear that!
@thomasstephenson40432 жыл бұрын
ok thats pretty cool. you are a good teacher. thank you
@FisherCoder2 жыл бұрын
Glad it was helpful!
@VasheshJ2 жыл бұрын
Thanks this video was helpful. Could you make a video on segment tree?
@sharanvkt2 жыл бұрын
thanks a lottt for this easy yet best explaination. you saved my whole day😊
@FisherCoder2 жыл бұрын
Glad it helped!
@pds78904 жыл бұрын
Thank you!! It didn't strike me that I can also sort and get middle element!! 😊 Video was helpful!
@FisherCoder4 жыл бұрын
Glad it helped!
@okey13173 жыл бұрын
But sorting will add more complexity right? Also is it possible to use in built sorting methods in coding interview?
@pds78903 жыл бұрын
@@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. 😁
@lilaiyang57453 жыл бұрын
Clear explanation, I understood it just by watching once. Appreciate 💪
@FisherCoder3 жыл бұрын
Glad it helped!
@lxkp32334 жыл бұрын
nice explanation! thanks for sharing this video!
@FisherCoder4 жыл бұрын
Glad it was helpful!
@haolicoder-58983 жыл бұрын
You are great!
@FisherCoder3 жыл бұрын
Glad it's helpful!
@Sree-uv9bd4 жыл бұрын
Excellent explanation ! :) thank you
@FisherCoder4 жыл бұрын
Glad it was helpful!
@jitinroy22462 жыл бұрын
awesome
@chaitanyarao60454 жыл бұрын
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.
@vijayap19293 жыл бұрын
Amazing!
@FisherCoder3 жыл бұрын
Thank you! Cheers!
@justinkuang94234 жыл бұрын
Was looking for a more optimal space solution, thanks!
@zfarahx6 ай бұрын
I don't think you've added enough #fishercode tags in your video.
@RajeebLochanChand-b6b11 ай бұрын
nice man
@FisherCoder11 ай бұрын
Thanks!
@guo75802 жыл бұрын
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.
@johnathondouglass2 жыл бұрын
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?
@ultrma2 жыл бұрын
The initinal value of counter should be set as 0. Otherwise this fails at [6,5,5]
@ahsanhabibabir58874 жыл бұрын
thnx
@lephuphan90623 жыл бұрын
what is the web browser he used?
@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 Жыл бұрын
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.
@amitbhattacharya3562 жыл бұрын
Nice explanation, but for this [10,9,9,9,10] as input I am getting the wrong answer.
@amitbhattacharya3562 жыл бұрын
class Solution { public int majorityElement(int[] nums) { int count = 1; int candidate = nums[0]; for(int i=1; i
@timwong23962 жыл бұрын
@@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
@georgetsiklauri4 жыл бұрын
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.
@andywang69283 жыл бұрын
5/2 = 2. Which one here has a total number larger than 2?
@georgetsiklauri4 жыл бұрын
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.
@ArielVolovik4 жыл бұрын
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.
@gargichaurasia41033 жыл бұрын
@@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.
@kshitijsingh19902 ай бұрын
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; }