Tap to unmute

Majority Element - Leetcode 169 - Hashmaps & Sets (Python)

  Рет қаралды 7,395

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 29
@GregHogg
@GregHogg 6 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@moein7808
@moein7808 2 ай бұрын
I follow neetcode , but not every explanation of his understandable for me , but your is defferent. Your explanation is awesome. Great Greatt !!!!
@shrayanmandal2004
@shrayanmandal2004 2 ай бұрын
same thoughts
@Fastforward937j
@Fastforward937j 6 ай бұрын
The majority elements count cant be reduced completely by any other elements count and finally the majority element will be having enough count to be in the ans variable even after checking all other elements
@Channel_SPS
@Channel_SPS 3 ай бұрын
h = Counter(nums) return max(h, key = h.get)
@artemborysenko613
@artemborysenko613 5 ай бұрын
Hi, thanks for the video. The solution is not working with such array [2,2,1,3,3,2,3,2,3,1,1,1,1,2,2], so I assume that the variant with HashSet is better in this case. Thanks.
@PedroBecker-fs9fm
@PedroBecker-fs9fm 5 ай бұрын
Being "n" the number of elements in the array, the problem affirms that one element will appear more than n / 2 times in the array, so your array isn't in the testcases. Greg's algorithym works only in that condition.
@mbe9176
@mbe9176 6 ай бұрын
This is an implementation of Kadane's Algorithm right?
@GregHogg
@GregHogg 6 ай бұрын
Nope!
@bence712
@bence712 5 ай бұрын
Boyer-Moore majority vote algorithm
@gabrielk2295
@gabrielk2295 7 ай бұрын
Weird but it seems to work :)
@Erlisch1337
@Erlisch1337 7 ай бұрын
no idea how
@GregHogg
@GregHogg 7 ай бұрын
Yeah this one is not so intuitive
@SeltonMR
@SeltonMR 7 ай бұрын
The second solution is boyer-moore voting algorithm
@gabrielk2295
@gabrielk2295 7 ай бұрын
@@GregHogg yes intuitively it means that you try to find one number that will be in majority comparing to the other numbers. If one number can "stay alive" (ah ah ah ah...) it means he is in majority. I did not know that Algo. Really nice! Thanks
@GregHogg
@GregHogg 7 ай бұрын
@@gabrielk2295 Haha love the reference. Glad you enjoyed it!
@mamineamin1811
@mamineamin1811 5 ай бұрын
why did you give it a Difficulty score of 8 in algomap
@microscorpi0n
@microscorpi0n 5 ай бұрын
The Dictionary (hashmap) was the trivial solution, however the second solution fulfills the problem's follow up restriction: "Could you solve the problem in linear time and in O(1) space?". Although 8 is difficult, I don't see how one could be expected to invent the Boyer-Moore majority vote algorithm in an interview without already having studied it.
@brianwkinyua
@brianwkinyua 4 ай бұрын
@@microscorpi0n Thank you for sharing the name of the algorithm human. 🙂
@hamids4550
@hamids4550 Ай бұрын
wtf lol why is this working, and who the heck came up with this confusing but smart solution
@vidhansaini9296
@vidhansaini9296 5 ай бұрын
count = collections.Counter(nums) majority_ele = max(count.values()) for n in nums: if count[n] == majority_ele: return n can we do this i did this and leetcode accepted the solution
@kira8103
@kira8103 7 ай бұрын
Cool
@Blafasel3
@Blafasel3 7 ай бұрын
Calling it "count" in the second example is misleading. Its more like a weight or something like that. :D
@GregHogg
@GregHogg 7 ай бұрын
Good point yeah
@hlubradio2318
@hlubradio2318 7 ай бұрын
I stopped at the first solution lol
@GregHogg
@GregHogg 7 ай бұрын
Haha fair but please understand the second one!
Majority Element - Leetcode 169 - Python
14:39
NeetCode
Рет қаралды 120 М.
Caleb Pressley Shows TSA How It’s Done
0:28
Barstool Sports
Рет қаралды 60 МЛН
Who is More Stupid? #tiktok #sigmagirl #funny
0:27
CRAZY GREAPA
Рет қаралды 10 МЛН
ML Was Hard Until I Learned These 5 Secrets!
13:11
Boris Meinardus
Рет қаралды 361 М.
Stacks & Queues - DSA Course in Python Lecture 5
14:58
Greg Hogg
Рет қаралды 18 М.
Majority Element II - Leetcode 229 - Python
14:34
NeetCodeIO
Рет қаралды 21 М.
Caleb Pressley Shows TSA How It’s Done
0:28
Barstool Sports
Рет қаралды 60 МЛН