/177. Nth Highest Distinct Value in List

177. Nth Highest Distinct Value in List

Medium
Databases & SQL38.9% acceptance

Given a list of integer values and an integer k, return the k-th highest distinct value in the list. If there are fewer than k distinct values, return None.

Example 1

Input: values = [50, 20, 30, 50, 40], k = 3

Output: 30

Explanation: Distinct values are [50, 40, 30, 20]. The 3rd highest is 30.

Example 2

Input: values = [10, 10, 10], k = 2

Output: None

Explanation: Only one distinct value exists.

Example 3

Input: values = [5, 3, 8, 8, 2], k = 1

Output: 8

Explanation: Distinct values are [8, 5, 3, 2]. The highest is 8.

Constraints

  • 1 <= len(values) <= 10^4
  • All values are integers
  • 1 <= k <= len(values)
Python (current runtime)

Case 1

Input: values = [7, 7, 6, 5, 4], k = 4

Expected: 4

Case 2

Input: values = [100, 90, 90, 80], k = 2

Expected: 90

Case 3

Input: values = [1, 2, 3, 4, 5], k = 5

Expected: 1

Case 4

Input: values = [1, 2, 2, 1], k = 3

Expected: None

Case 5

Input: values = [9], k = 1

Expected: 9