Master Data Structures & Algorithms For FREE at AlgoMap.io!
@moein78082 ай бұрын
I follow neetcode , but not every explanation of his understandable for me , but your is defferent. Your explanation is awesome. Great Greatt !!!!
@shrayanmandal20042 ай бұрын
same thoughts
@Fastforward937j6 ай бұрын
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_SPS3 ай бұрын
h = Counter(nums) return max(h, key = h.get)
@artemborysenko6135 ай бұрын
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-fs9fm5 ай бұрын
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.
@mbe91766 ай бұрын
This is an implementation of Kadane's Algorithm right?
@GregHogg6 ай бұрын
Nope!
@bence7125 ай бұрын
Boyer-Moore majority vote algorithm
@gabrielk22957 ай бұрын
Weird but it seems to work :)
@Erlisch13377 ай бұрын
no idea how
@GregHogg7 ай бұрын
Yeah this one is not so intuitive
@SeltonMR7 ай бұрын
The second solution is boyer-moore voting algorithm
@gabrielk22957 ай бұрын
@@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
@GregHogg7 ай бұрын
@@gabrielk2295 Haha love the reference. Glad you enjoyed it!
@mamineamin18115 ай бұрын
why did you give it a Difficulty score of 8 in algomap
@microscorpi0n5 ай бұрын
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.
@brianwkinyua4 ай бұрын
@@microscorpi0n Thank you for sharing the name of the algorithm human. 🙂
@hamids4550Ай бұрын
wtf lol why is this working, and who the heck came up with this confusing but smart solution
@vidhansaini92965 ай бұрын
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
@kira81037 ай бұрын
Cool
@Blafasel37 ай бұрын
Calling it "count" in the second example is misleading. Its more like a weight or something like that. :D