Problem 377546 · medium · Level 03 Linear Management & Searching

Kth Largest Distinct Value

sorting · sets · indexing

Given a list of integers nums and a positive integer k, return the k-th largest distinct value. Duplicates count once: in [5, 5, 4] the 2nd largest distinct value is 4.

If there are fewer than k distinct values, return None.

Examples

Input:  nums = [12, 7, 19, 3, 15], k = 2
Output: 15
Input:  nums = [8, 11, 8, 2, 11, 6, 14, 6], k = 3
Output: 8
Explanation: The distinct values in descending order are 14, 11, 8, 6, 2.
Input:  nums = [1, 1], k = 2
Output: None

Constraints

  • 0 <= len(nums) <= 10**5, 1 <= k <= 10**5
  • Target complexity: O(n log n).

Goals

  • Remove duplicates before ranking values
  • Index into a descending sorted list safely
  • Return None when the request cannot be satisfied
Starting Python…