Timeline
Timeline
2025-09-29
init
Sorting
Title:
After sorting, just return the middle element.
1 |
|
A simple fact: If an array has more than half of its numbers the same, then deleting any two different numbers will still leave the new array with the same property. Based on this fact, a similar elimination idea arises:
Since the majority element must occupy more than half of the array size, even if all other elements come to ‘collide’ with it, in the end the majority element remains. cur represents the current candidate majority element, count represents the ‘net’ count of that majority element so far (for the candidate majority, increment on same, decrement on different, if count becomes 0, reset candidate majority and set count to 1).
1 | class Solution { |
LeetCode Hot 100 rewrite, starting from 0
1 |
|
