tutorialcup
Third Maximum Number Leetcode Solution
As the title says, the goal is to find the third maximum integer in a given array of integers. Note that we need to find the distinct third maximum integer in the array. We return the maximum integer in the array when it has no distinct third maximum integer.
Example
Array = {1 , 3 , 2 , -2 , -4 , -9}
1
Explanation
The first maximum is 3, second maximum is 2 and third maximum is 1. So, we print 1.
Array = {1 , 1 , 3}
3
Explanation
The first maximum is 3, second maximum is 1, but there is no third maximum because we consider both the 1s as the second maximum here.
Approach(Sorting)
We can sort the whole array and find the third distinct integer starting from its back. Note that we can't return Array simply as there can be duplicate entries in the array. If we do not find 3 distinct integers in the array, we return its last element as it is the maximum element. Follow the given algorithm for better understanding.
Algorithm
- Create a function thirdMax() returning the required integer
- thirdMax() returns the third distinct maximum integer in a given array- a
- Sort the array
- Initialize a variable, idx to store the last index of the array and distinctCount to count the distinct elements from the back
- while idx >= 0:
- Increment distinctCount
- Initialize i to iterate through indices having the same value as a
- while the elements before idx have the same value as a and i >= 0:
- Decrement i
- If i == -1, it means that we have
www.tutorialcup.com/leetcode-solutions/third-maximum-numb...
Third Maximum Number Leetcode Solution
As the title says, the goal is to find the third maximum integer in a given array of integers. Note that we need to find the distinct third maximum integer in the array. We return the maximum integer in the array when it has no distinct third maximum integer.
Example
Array = {1 , 3 , 2 , -2 , -4 , -9}
1
Explanation
The first maximum is 3, second maximum is 2 and third maximum is 1. So, we print 1.
Array = {1 , 1 , 3}
3
Explanation
The first maximum is 3, second maximum is 1, but there is no third maximum because we consider both the 1s as the second maximum here.
Approach(Sorting)
We can sort the whole array and find the third distinct integer starting from its back. Note that we can't return Array simply as there can be duplicate entries in the array. If we do not find 3 distinct integers in the array, we return its last element as it is the maximum element. Follow the given algorithm for better understanding.
Algorithm
- Create a function thirdMax() returning the required integer
- thirdMax() returns the third distinct maximum integer in a given array- a
- Sort the array
- Initialize a variable, idx to store the last index of the array and distinctCount to count the distinct elements from the back
- while idx >= 0:
- Increment distinctCount
- Initialize i to iterate through indices having the same value as a
- while the elements before idx have the same value as a and i >= 0:
- Decrement i
- If i == -1, it means that we have
www.tutorialcup.com/leetcode-solutions/third-maximum-numb...