Given an integer array and k, return k distinct values with the highest frequencies. If values tie at the selection boundary, any valid choice is acceptable; output order is unrestricted.
### Constraints & Assumptions
Assume `0 <= k <= number of distinct values`. The source does not prescribe a tie-breaker, so do not invent one as a required output rule.
### Clarifying Questions to Ask
- Are ties at the kth frequency guaranteed absent, or may any tied values be selected?
- Must output be frequency-sorted, or is any order acceptable?
```hint Count before selecting
The selection candidates are distinct values paired with frequencies, not individual array positions.
```
### What a Strong Answer Covers
- A frequency map and a size-k selection structure.
- Correct treatment of duplicate occurrences and selection-boundary ties.
- Complexity in input length, distinct values, and k.
### Follow-up Questions
- How would frequency buckets yield linear expected work?
- What changes when the input is an unbounded stream with limited memory?
Overview: Find the k most frequent array values using counting and a bounded heap, preserving valid tie choices and comparing frequency-bucket alternatives.
Given an integer array and k, return k distinct values with the highest frequencies. If values tie at the selection boundary, any valid choice is acceptable; output order is unrestricted.
Constraints & Assumptions
Assume 0 <= k <= number of distinct values. The source does not prescribe a tie-breaker, so do not invent one as a required output rule.
Clarifying Questions to Ask Guidance
Are ties at the kth frequency guaranteed absent, or may any tied values be selected?
Must output be frequency-sorted, or is any order acceptable?
What a Strong Answer Covers Guidance
A frequency map and a size-k selection structure.
Correct treatment of duplicate occurrences and selection-boundary ties.
Complexity in input length, distinct values, and k.
Follow-up Questions Guidance
How would frequency buckets yield linear expected work?
What changes when the input is an unbounded stream with limited memory?