Majority Element (LeetCode 169) | Full solution with 4 different methods | Interview Essential
HTML-код
- Опубликовано: 30 июл 2024
- A majority candidate is defined as more than 50%. So in a given array of integers you have different methods to find it. Watch the video to learn 4 ways how you can start and ultimately arrive at an efficient solution. This video provides a perfect approach to attack this problem step by step with animations and visuals.
Actual problem on LeetCode: leetcode.com/problems/majorit...
Chapters:
00:00 - Intro
00:51 - Problem Statement and Description
02:48 - Brute Force Solution
04:27 - Solution by Sorting
07:14 - Using a HashTable
09:35 - Moore’s Voting Algorithm O(1) space
12:14 - Dry-run of Code
13:47 - Final Thoughts
📚 Links to topics I talk about in the video:
Brute Force Algorithms: • Brute Force algorithms...
LeetCode Problems: • Leetcode Solutions
Other easy difficulty problems: • Easy Problems
Dynamic Programming: • Dynamic Programming ea...
Quick Sort: • Quick Sort super easy ...
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
Slick & straight! Thank you for breaking the complexity.
Keep making these videos bro, the quality is Amazing!
You've explained in a very simple way!!
You the one of the best here on RUclips, the way you teach, the way you keep every in a structured manner is super commendable. Subscribed.
thanks for the kind words
thank you, your dry run really helped
Great video, you explained it so well.
Thanks !!
Thank you. Very good explanation!
Thanks a lot sir ! Really helpful
Beautifully Explained
Bro, the quality of your content is exceptional. You deserve more subscribers. Thank you brother.
I wish that too 😄
this was very helpful 😀 Thank you
great solution as always. Thanks alot bhaiya please keep on making such amazing videos.
So nice of you
Thanks for this great explanation
Awesome explanation 😮😮
After watching 5 videos finally understood ... kaafi acchaa explanation...loved it
Thanks for liking
Amazing exaplanation
Amazing!
Next level & Awesome explaination with cutest smile. Thank you😊
that is so sweet of you
Amazing great explanation
amazing amazing!!!!!
Glad i found your channel
great explanation
pls make a video on peak element ... your videos are so helpful!!!!
Superb Explanation!
Thank you 🙂
Great video man! Loved this explanation, you are highly underrated. If it's possible can you do python solutions as well for future leetcode videos? Thanks again!
i would advice you to focus on the problem solving method, rather than the language. Trust me...languages will come and go...get your basics right first :)
LEGEND 🖤
Thanks!
Your teaching is superb. You have a new subscriber.
Thanks a lot 😊
super sir good explanation
SO CLASSY AND please make the playlist of data structures important questions too
playlist: ruclips.net/p/PLFdAYMIVJQHM8Kh5i8P2lGIbJXFPBelRI
it's tough to get an optimized solution 😔.... but I will try to reach it on my own 😊.
great video
Thanks
the second soln was impressive,
there's a small mistake in the dry run in moore's algo at last votes for majority 2 is 1,when majority reaches 3 the votes will be 0, so, majority will be updated in the next iteration so majority will be update as 1, please check, explanation is too good!!!
the dry-run is mainly to understand the simplicity of the code. The exact working code is available in the description too. Mostly you should understand the approach and how you are solving the problem :)
You're the best👍💯
You are!
better explanation than Striver .
Very Well Explained
Thank you so much 🙂
perfect teacher
perfect student :)
Why there are only three types of numbers? In array
you can have as many types.
Thanks man, good explaining, if i land a job, ill send you some money 😁
haha..thanks a bunch
the voting method returns 1 for me for array {1, 2, 2, 2, 3, 3, 1 } so is that logic correct?
I think after your logic, we need to check again in the array if the count of the majority element is greater than (n/2) to be considered as the majority.
In my case, the majority is returned as 1 but 1 is repeated 2 times which is not greater than the expected majority which is (>3). here we can suspect 2 could be the majority but it's not because it is not repeated more than 3 times.
Majority element means the element which occurs more than n/2 times. Your test case is invalid, as it does not have a majority element.
What you are talking about is the element occurring maximum number of times.
*Sir Please Complete top 150 interview Questions First from Leetcode 🔥*
there are some problems from that list that I have covered...adding new solutions every week :)
@@nikoo28 *Thankyou Sir Loved your teaching Very Clear & Upto the point*
@@nikoo28 thank you!
Can we do using 2 pointer
Give me a pseudo code for your approach
Can we get n/3 solution as well?
do you have a link to the problem?
What if there is no majority element? How to handle that?
then it will be an entirely different problem. What are you looking to find?
It gives error when nums=[6,5,5]
What error are you getting? I tried the case again and it gives 5 as the output
@@nikoo28 sir i got output as 6 in the same code
@@foodandjournieswithme8788 check your test case and code again. Here is the verified output: github.com/nikoo28/java-solutions/blob/master/src/test/java/leetcode/easy/MajorityElementTest.java
The mistake in the code is that the loop starts with i = 0, which causes the initial element to be counted twice. Specifically, when the loop starts, nums[0] is already assigned to the majority and the vote is set to 1. The loop then starts from i = 0, incrementing votes for the same element. The correct approach is to start the loop from i = 1.
for (int i = 1; i < nums.length; i++)
Wrong Code (Wrong understanding of mine)
What part do you think is wrong?
@@nikoo28 first condition vote==0
@@abhiguptamusic That condition is absolutely correct, whenever votes become =0 you need to update your majority candidate, and then increase the vote count. What error do you see in the condition? Did you try running the code?
@@nikoo28 the code is not working with another test cases
@@nikoo28 please try with these test case [1,1,2,3,4]