2. Runner-Up Score
EasyArray
A quiz show records the final score of every contestant. Contestants who tie on a score share the same place, so the runner-up score is the second-highest distinct score on the board.
Write a method secondLargest that takes the array nums of scores and returns the runner-up score. If the board has fewer than two distinct scores, return -1.
Example 1
- Input:
- nums = [40,85,70,85]
- Output:
- 70
- Explanation:
85 is the top score (held by two contestants), so the runner-up score is 70.
Example 2
- Input:
- nums = [50,50,50]
- Output:
- -1
- Explanation:
Every contestant has the same score, so there is no runner-up and the answer is -1.
Constraints
1 ≤ nums.length ≤ 104
0 ≤ 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)
- Space
- O(1)