Search⌘ K
AI Features

Solution: Third Maximum Number

Explore how to find the third distinct maximum element in an integer array by using a min heap and set to track unique elements. This lesson helps you understand maintaining the top three distinct numbers efficiently, handling duplicates, and returning the correct maximum value based on distinct counts.

Statement

Given an integer array nums, determine and return the third distinct maximum element in the array. If the array contains fewer than three distinct elements, return the maximum element instead.

Constraints:

  • 1<=1 <= nums.length <=103<= 10^3 ...