Leetcode 169 - Majority Element JavaScript

preview_player
Показать описание
LeetCode 169 is commonly known as "Majority Element." This problem asks us to find the most commonly occurring element in an array of numbers. The initial problem statement gives us a significant hint: "The majority element is the element that appears more than ⌊n / 2⌋ times." In this video I'll show three solutions to this problem using a frequency counter, O(1) space complexity, and a '2-liner' solution to impress your friends.

Chapters:
0:00 Intro
1:44 Frequency Counter Solution
10:12 O(1) Space Complexity Solution
17:03 2 Liner Solution
19:03 Outro
Рекомендации по теме
Комментарии
Автор

Your second solution is actually O(n log n) because of the sorting step. Sorting the array is the part that takes the most time, and it’s what makes the solution not linear. Even though your loop after sorting is just O(n), the sorting dominates, so the overall complexity ends up being O(n log n). Without the sorting, it could be linear.

EpicTalesTube
join shbcf.ru