FAANG · LeetCode #347
Top K Frequent Elements — three-stage drill
1The ProblemMedium
Top K Frequent Elements
Return the k elements that appear most often.
Examples
[1,1,1,2,2,3], k=2→ [1,2]
constraintThe answer is unique for the given k.
2The PatternTIME O(n) · SPACE O(n)
Arrays & Hashing
Trade space for time: a hash map turns 'have I seen this?' into an O(1) lookup.
Problems using this pattern
3The SolutionRESULT → [1, 2]
Step through it
count: 1 → 1×
Step 1 / 7
Arrow keys to navigate · escape to reset