Problem 250562 · easy · Level 02 Linear Data Structures

The Bronze-Medal Score

arrays · iteration · best-so-far

A diving contest hands out gold, silver and bronze to the three highest different scores in nums (a non-empty list of integers). Divers with equal scores share a step of the podium, so a repeated score fills only one step. Return the bronze-medal score. If fewer than three different scores were posted there is no bronze, and you return the gold score (the largest) instead.

Examples

Input:  nums = [8, 15, 4, 11]
Output: 8

Input:  nums = [5, 9]
Output: 9
Explanation: Only two different scores, so the gold score is returned.

Input:  nums = [7, 7, 10, 3, 3]
Output: 3
Explanation: The different scores are 10, 7, 3; the third highest is 3.

Constraints

  • Target: O(n) time, O(1) extra space (do not sort).

Goals

  • Maintain the top three distinct values in constant space
  • Skip duplicates so equal values do not occupy two slots
  • Fall back to the maximum when fewer than three distinct values exist
Starting Python…