Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
Example 1:
Input: nums = [3,2,1] Output: 1 Explanation: The first distinct maximum is 3. The second distinct maximum is 2. The third distinct maximum is 1.
Example 2:
Input: nums = [1,2] Output: 2 Explanation: The first distinct maximum is 2. The second distinct maximum is 1. The third distinct maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: nums = [2,2,3,1] Output: 1 Explanation: The first distinct maximum is 3. The second distinct maximum is 2 (both 2's are counted together since they have the same value). The third distinct maximum is 1.
Constraints:
1 <= nums.length <= 104-231 <= nums[i] <= 231 - 1O(n) solution?Problem summary: Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,2,1]
[1,2]
[2,2,3,1]
kth-largest-element-in-an-array)neither-minimum-nor-maximum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #414: Third Maximum Number
class Solution {
public int thirdMax(int[] nums) {
long m1 = Long.MIN_VALUE;
long m2 = Long.MIN_VALUE;
long m3 = Long.MIN_VALUE;
for (int num : nums) {
if (num == m1 || num == m2 || num == m3) {
continue;
}
if (num > m1) {
m3 = m2;
m2 = m1;
m1 = num;
} else if (num > m2) {
m3 = m2;
m2 = num;
} else if (num > m3) {
m3 = num;
}
}
return (int) (m3 != Long.MIN_VALUE ? m3 : m1);
}
}
// Accepted solution for LeetCode #414: Third Maximum Number
func thirdMax(nums []int) int {
m1, m2, m3 := math.MinInt64, math.MinInt64, math.MinInt64
for _, num := range nums {
if num == m1 || num == m2 || num == m3 {
continue
}
if num > m1 {
m3, m2, m1 = m2, m1, num
} else if num > m2 {
m3, m2 = m2, num
} else if num > m3 {
m3 = num
}
}
if m3 != math.MinInt64 {
return m3
}
return m1
}
# Accepted solution for LeetCode #414: Third Maximum Number
class Solution:
def thirdMax(self, nums: List[int]) -> int:
m1 = m2 = m3 = -inf
for num in nums:
if num in [m1, m2, m3]:
continue
if num > m1:
m3, m2, m1 = m2, m1, num
elif num > m2:
m3, m2 = m2, num
elif num > m3:
m3 = num
return m3 if m3 != -inf else m1
// Accepted solution for LeetCode #414: Third Maximum Number
struct Solution;
use std::cmp::Ordering::*;
impl Solution {
fn third_max(nums: Vec<i32>) -> i32 {
let mut max1 = nums[0];
let mut m2: Option<i32> = None;
let mut m3: Option<i32> = None;
for x in nums {
match x.cmp(&max1) {
Greater => {
m3 = m2;
m2 = Some(max1);
max1 = x;
}
Less => {
if let Some(max2) = m2 {
match x.cmp(&max2) {
Greater => {
m3 = m2;
m2 = Some(x);
}
Less => {
if let Some(max3) = m3 {
if x > max3 {
m3 = Some(x);
}
} else {
m3 = Some(x);
}
}
Equal => {}
}
} else {
m2 = Some(x);
}
}
Equal => {}
}
}
if let Some(max3) = m3 {
max3
} else {
max1
}
}
}
#[test]
fn test() {
let nums = vec![3, 2, 1];
assert_eq!(Solution::third_max(nums), 1);
let nums = vec![1, 2];
assert_eq!(Solution::third_max(nums), 2);
let nums = vec![2, 2, 3, 1];
assert_eq!(Solution::third_max(nums), 1);
let nums = vec![1, 2, 2, 5, 3, 5];
assert_eq!(Solution::third_max(nums), 2);
}
// Accepted solution for LeetCode #414: Third Maximum Number
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #414: Third Maximum Number
// class Solution {
// public int thirdMax(int[] nums) {
// long m1 = Long.MIN_VALUE;
// long m2 = Long.MIN_VALUE;
// long m3 = Long.MIN_VALUE;
// for (int num : nums) {
// if (num == m1 || num == m2 || num == m3) {
// continue;
// }
// if (num > m1) {
// m3 = m2;
// m2 = m1;
// m1 = num;
// } else if (num > m2) {
// m3 = m2;
// m2 = num;
// } else if (num > m3) {
// m3 = num;
// }
// }
// return (int) (m3 != Long.MIN_VALUE ? m3 : m1);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.