Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Example 1:
Input: [3, 2, 1] Output: 1 Explanation: The third maximum is 1.
Example 2:
Input: [1, 2] Output: 2 Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: [2, 2, 3, 1] Output: 1 Explanation: Note that the third maximum here means the third maximum distinct number. Both numbers with value 2 are both considered as second maximum.
Solution:
The trick is to initialize three Integer variables with value null.
Therefore, we do not need to worry about Integer.MIN_VALUE in the input array.
When we go through the input array, we check each number and determine if we need to update the three variables.
Notice that we only care about unique numbers.
So if the number equals to either of the three variables, we simply pass it.
Code:
public class Solution { public int thirdMax(int[] nums) { Integer first = null; Integer second = null; Integer third = null; for (Integer num : nums) { if (num.equals(first) || num.equals(second) || num.equals(third)) { continue; } if (first == null || num > first) { third = second; second = first; first = num; } else if (second == null || num > second) { third = second; second = num; } else if (third == null || num > third) { third = num; } } return third == null ? first : third; } }