This question evaluates understanding of algorithms and data structures for selecting distinct top-k values and enforcing ordering constraints. It is commonly asked in coding and algorithms interviews to assess practical application skills in handling duplicates, ordering, and performance optimization, reflecting a practical algorithmic application rather than purely conceptual reasoning.
Given an integer array nums and an integer k, return the k largest distinct values from nums.
Requirements
k
distinct values, return
all distinct values
in descending order.
Input
nums
: array of integers (may contain duplicates)
k
: positive integer
Output
Example
nums = [5, 2, 2, 9, 1, 9, 7], k = 3
→ output
[9, 7, 5]
Constraints (typical)
1 ≤ len(nums) ≤ 10^5
1 ≤ k ≤ len(nums)
Follow-up
O(n log k)
using a heap).