Second Highest Salary
Medium
Topics
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
[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.Input: nums = [100,200,300] Output: 200 Explanation: Three distinct salaries; the runner-up is 200.
Example 2
[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
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
- Carry two values, both unset to begin with: the highest seen and the second highest distinct.
- For each salary: if it beats the highest, the old highest becomes the second and this becomes the highest.
- Otherwise, if it is strictly below the highest and above the current second, it becomes the second.
- A salary equal to either is ignored. Return the second if it was ever set, and
-1otherwise.
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
-1here and asNULLin 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 secondimport java.util.*;
class Solution {
public int secondHighest(int[] nums) {
Integer highest = null, second = null;
for (int salary : nums) {
if (highest == null || salary > highest) {
if (highest != null) second = highest;
highest = salary;
} else if (salary < highest && (second == null || salary > second)) {
second = salary;
}
}
return second == null ? -1 : second;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int secondHighest(vector<int>& nums) {
bool haveHighest = false, haveSecond = false;
long long highest = 0, second = 0;
for (int salary : nums) {
if (!haveHighest || salary > highest) {
if (haveHighest) {
second = highest;
haveSecond = true;
}
highest = salary;
haveHighest = true;
} else if (salary < highest && (!haveSecond || salary > second)) {
second = salary;
haveSecond = true;
}
}
return haveSecond ? (int)second : -1;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var secondHighest = function(nums) {
let highest = null, second = null;
for (const salary of nums) {
if (highest === null || salary > highest) {
if (highest !== null) second = highest;
highest = salary;
} else if (salary < highest && (second === null || salary > second)) {
second = salary;
}
}
return second === null ? -1 : second;
};