Рет қаралды 8,055
iPad PDF Notes - github.com/MAZ...
This is the 63rd Video on our Arrays (1-D & 2-D) Playlist.
In this video we will try to solve a very very famous array Problem - Majority Element I and Majority Element II (Leetcode-229 & Leetcode-169).
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : Majority Element I and Majority Element II
Company Tags : AMAZON | GOOGLE
My solutions on Github (C++ & JAVA) :
Majority Element - I github.com/MAZ...
Majority Element - II github.com/MAZ...
Leetcode Link :
Majority Element - I leetcode.com/p...
Majority Element - II leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZ...
Subscribe to my channel : / @codestorywithmik
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Approaches Summary :
Majority Element I - The algorithm iterates through the array, maintaining a count variable and a candidate variable. It initializes the count to 0 and the candidate to NULL. For each element in the array, it checks if the count is zero. If so, it sets the count to 1 and updates the candidate to the current element. If the current element is equal to the candidate, it increments the count; otherwise, it decrements the count. The algorithm does not explicitly check the frequency of the candidate, as it is guaranteed that a majority element exists. Finally, it returns the majority element found using this approach.
Majority Element II - It employs a variation of the Boyer-Moore Voting Algorithm to identify two potential majority elements (maj1 and maj2) and then validates their frequencies in a second pass. The algorithm initializes two candidates (maj1 and maj2) and their corresponding counts (count1 and count2) to NULL and 0, respectively. It also calculates the required frequency threshold (freq) based on the array size. The first loop iterates through the array, updating the counts and candidates based on the Boyer-Moore Voting Algorithm. In the second pass, the algorithm counts the occurrences of maj1 and maj2 in the array and checks if they meet the frequency criteria. The elements that satisfy the condition are added to the result vector, which is then returned. This approach ensures that the elements included in the result vector appear more than ⌊ n/3 ⌋ times in the given array.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook