Sorting · Easy
A dashboard needs a short list of the largest observed measurement values. Duplicate observations should not fill several places, and the output must be ordered from largest to smallest. Returning fewer items is valid when there are fewer distinct values.
Return at most k distinct input values in strictly decreasing numeric order.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[], k: integer }
Fields
- nums is an array of signed integers. Repeated values at different positions are separate entries.
- k is a non-negative integer maximum number of distinct values to return.
Output
Return value
- Return the largest min(k,u) values where u is the number of distinct input values.
- Return [] when k is zero or input is empty. Do not pad the result or repeat values.
Constraints
- 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
- All totals and counts fit exactly in a safe JSON integer.
- 0 <= k <= 100000.
Examples
Example 1
{
"k": 2,
"nums": [
5,
5,
1,
3
]
}Returns
[
5,
3
]Example 2
{
"k": 5,
"nums": [
-1,
-2,
-1
]
}Returns
[
-1,
-2
]Required complexity
O(n + u log(u + 1)) time and O(u) auxiliary space; more efficient exact selection is accepted