25. Podium Finish
A gaming tournament gives every player a score. Players who tie on a score share the same place, so the podium is decided by distinct scores: first place is the highest distinct score, second place is the next highest, and third place is the one after that.
Write a method thirdMax that takes the array nums of scores and returns the score that takes third place. If there are fewer than three distinct scores, there is no third place, so return the highest score instead.
Example 1
- Input:
- nums = [3,2,1]
- Output:
- 1
- Explanation:
The three distinct scores are 3, 2 and 1, so third place is 1.
Example 2
- Input:
- nums = [1,2]
- Output:
- 2
- Explanation:
Only two distinct scores exist, so the answer is the highest score, 2.
Constraints
1 ≤ nums.length ≤ 1000
-109 ≤ nums[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n log n)
- Space
- O(n)