Second Highest Salary

Medium

Topics
ArraySorting

Given an array of salaries nums, return the second highest distinct salary, or -1 if there is no such salary.

Distinct is the operative word: in [300, 300, 200] the highest salary is 300 and the second highest is 200, because the duplicate 300 is the same salary, not a runner-up.

On LeetCode this is a SQL exercise returning NULL. Here it is an array and the missing case is reported as -1, which the judge can compare.

Example 1

100020013002
the highest salarythe answer
Of the 3 salaries in [100,200,300] there are 3 distinct values, and the second largest of those is the shaded 200 just below the dimmed maximum. The ranking runs over distinct salaries, so repeats collapse first and extra copies of the maximum can never become the answer.
The result is a single value, not a position.
Input:  nums = [100,200,300]
Output: 200
Explanation: Three distinct salaries; the runner-up is 200.

Example 2

1000
the highest salary
Every salary in [100] is the same value, which leaves no second place at all, so the answer is -1 rather than that one value reported again.
Input:  nums = [100]
Output: -1
Explanation: Only one salary, so there is no second.

Example 3

300030012002
the highest salarythe answer
Input:  nums = [300,300,200]
Output: 200
Explanation: The two 300s are one distinct salary, so 200 is second — not 300.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
Editorial — intuition, approach, complexity and pitfalls

Intuition

Sorting and taking the second-from-last element is the obvious move and it is wrong on example 3, where the last two entries are both 300. The duplicates have to be collapsed before the ranking means anything.

Sorting the distinct values works and costs more than necessary. The second highest can be found by carrying two running values through a single pass — the best seen and the best seen that is strictly smaller than it — updating them as each salary arrives.

The care needed is all in the word "strictly". A salary equal to the current best must not become the runner-up, and a salary equal to the current runner-up changes nothing. Getting those two comparisons right is the whole problem.

Approach

  1. Carry two values, both unset to begin with: the highest seen and the second highest distinct.
  2. For each salary: if it beats the highest, the old highest becomes the second and this becomes the highest.
  3. Otherwise, if it is strictly below the highest and above the current second, it becomes the second.
  4. A salary equal to either is ignored. Return the second if it was ever set, and -1 otherwise.

Walkthrough

nums = [300, 300, 200]

salary   highest   second   note
  300      300      unset   first value
  300      300      unset   equal to the highest, ignored
  200      300      200     below the highest, so it is second

answer 200

Sorting gives [200, 300, 300] and the second-from-last entry
is 300 -- the same salary as the highest, which is why the
duplicates have to go first.

Complexity

  • Time — O(n): one pass with constant work per salary. Sorting is O(n log n) for the same answer.
  • Space — O(1): two values.

Common Pitfalls

  • Taking the second-from-last element after sorting, which returns the highest again whenever it is duplicated.
  • Using <= instead of < when testing against the highest, so a duplicate of the highest becomes the runner-up.
  • Returning 0 or the highest when there is no second. The absence has to be reported, as -1 here and as NULL in the SQL original.
  • Assuming at least two salaries. A single-element array is valid input.

Full Solution — all languages

class Solution:
    def secondHighest(self, nums):
        highest = second = None
        for salary in nums:
            if highest is None or salary > highest:
                if highest is not None:
                    second = highest
                highest = salary
            elif salary < highest and (second is None or salary > second):
                second = salary
        return -1 if second is None else second
SensAI
Beta
Tap the mic to stop voice mode