bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 414. Third Maximum Number

414. Third Maximum Number

Leetcode Solutions

Approach: Pointers

  1. Initialize firstMax, secondMax, and thirdMax to None.
  2. Iterate through each number in the array.
    • If the number is already one of the maxes, continue to the next number.
    • If the number is greater than firstMax or firstMax is None, shift the maxes and set firstMax to the current number.
    • Else if the number is greater than secondMax or secondMax is None, shift the maxes and set secondMax to the current number.
    • Else if the number is greater than thirdMax or thirdMax is None, set thirdMax to the current number.
  3. If thirdMax is not None, return it as the third distinct maximum.
  4. If thirdMax is None, return firstMax as the maximum number.
UML Thumbnail

Approach: Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...