Kth Smallest Distinct Value
Look only at the distinct values in nums, counting each value once however many times it appears. Return the k-th smallest of them, where k = 1 means the smallest.
k never exceeds the number of distinct values. In [4, 4, 1, 7] the distinct values are 1, 4 and 7, so k = 2 gives 4.
Example 1
nums [4,4,1,7]
k 2
returns 4
The distinct values in order are 1, 4, 7; the 2nd is 4.
Example 2
nums [5,3,5,3,9]
k 3
returns 9
The distinct values are 3, 5, 9; the 3rd is 9.
Constraints
1 <= len(nums) <= 100-1000 <= nums[i] <= 10001 <= k <= number of distinct values in nums
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Sorting and searching problems
- Maximum Product of Two ElementsBeginner
- Median of the ListBeginner
- Names in OrderBeginner
- Shortest Words FirstBeginner
- Sort by Last DigitBeginner
- Strictly Increasing CheckBeginner
- Top Three ValuesBeginner
- Where It LandsBeginner