Majority Element I | Majority Element II : Boyer-Moore | Made Simple | Leetcode - 229 and 169

codestorywithMIK
codestorywithMIK
6.5 هزار بار بازدید - 9 ماه پیش - iPad PDF Notes -
iPad PDF Notes - https://github.com/MAZHARMIK/Intervie...
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 https://github.com/MAZHARMIK/Intervie...
   Majority Element - II https://github.com/MAZHARMIK/Intervie...
Leetcode Link  :
   Majority Element - I https://leetcode.com/problems/majorit...
   Majority Element - II https://leetcode.com/problems/majorit...

My DP Concepts Playlist : Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : Graph Concepts & Qns - 1 : Graph will...
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Subscribe to my channel : @codestorywithmik
Instagram : Instagram: codestorywithmik
Facebook : Facebook: 100090524295846
Twitter : 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
9 ماه پیش در تاریخ 1402/07/13 منتشر شده است.
6,532 بـار بازدید شده
... بیشتر