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 - 1Follow up: Can you find an O(n) solution?
program main
implicit none
integer, parameter :: n = 3 ! number of elements in the array
integer, dimension(n) :: nums = (/ 3, 2, 1 /) ! input array
integer :: max_val, sec_max_val, thrd_max_val ! variables to store the values
call get_distinct_max(nums, max_val, sec_max_val, thrd_max_val)
write (*, '(A, I0)') 'Maximum value: ', max_val
write (*, '(A, I0)') 'Second maximum value: ', sec_max_val
write (*, '(A, I0)') 'Third maximum value: ', thrd_max_val
contains
subroutine get_distinct_max(nums, max_val, sec_max_val, thrd_max_val)
implicit none
integer, intent(in) :: nums(:) ! input array
integer, intent(out) :: max_val, sec_max_val, thrd_max_val ! output values
integer :: i, j, k ! loop counters
! initialize variables
max_val = nums(1)
sec_max_val = max_val
thrd_max_val = max_val
! loop through the array and find the distinct maxima
do i = 2, size(nums)
if (nums(i) > max_val) then
thrd_max_val = sec_max_val
sec_max_val = max_val
max_val = nums(i)
else if (nums(i) > sec_max_val) then
thrd_max_val = sec_max_val
sec_max_val = nums(i)
end if
end do
! check if the third maximum exists
k = 0
do j = 1, size(nums)
if (nums(j) == sec_max_val) then
k = k + 1
end if
end do
if (k < 2) then
thrd_max_val = max_val
end if
end subroutine get_distinct_max
end program main
Maximum value: 3 Second maximum value: 3 Third maximum value: 3
module third_distinct_maximum
implicit none
private
public :: third_distinct_maximum
contains
function third_distinct_maximum(nums) result(third_max)
integer, intent(in) :: nums(:)
integer :: third_max, max_count, max_val, i
! Initialize variables
max_count = 0
max_val = -huge(max_val)
third_max = -huge(third_max)
! Loop through the array and find the maximum and maximum count
do i = 1, size(nums)
if (nums(i) > max_val) then
max_count = max_count + 1
if (max_count == 3) then
third_max = max_val
exit
end if
max_val = nums(i)
end if
end do
! If the third maximum does not exist, return the maximum
if (third_max == -huge(third_max)) then
third_max = max_val
end if
end function third_distinct_maximum
end module third_distinct_maximum
program test_third_distinct_maximum
use third_distinct_maximum
implicit none
integer :: nums(3)
! Test case 1:
nums = [3, 2, 1]
write (*,*) "Third distinct maximum of ", nums, " is ", third_distinct_maximum(nums)
! Test case 2:
nums = [1, 2]
write (*,*) "Third distinct maximum of ", nums, " is ", third_distinct_maximum(nums)
! Test case 3:
nums = [2, 2, 3, 1]
write (*,*) "Third distinct maximum of ", nums, " is ", third_distinct_maximum(nums)
end program test_third_distinct_maximum
temp.f95:4:36:
4 | public :: third_distinct_maximum
| 1
Error: PUBLIC attribute applied to MODULE third_distinct_maximum at (1)
temp.f95:8:35:
8 | function third_distinct_maximum(nums) result(third_max)
| 1
Error: MODULE attribute of ‘third_distinct_maximum’ conflicts with PROCEDURE attribute at (1)
temp.f95:9:38:
9 | integer, intent(in) :: nums(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:10:51:
10 | integer :: third_max, max_count, max_val, i
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:13:21:
13 | max_count = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:14:32:
14 | max_val = -huge(max_val)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:15:36:
15 | third_max = -huge(third_max)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:18:28:
18 | do i = 1, size(nums)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:19:39:
19 | if (nums(i) > max_val) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:20:41:
20 | max_count = max_count + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:21:40:
21 | if (max_count == 3) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:22:39:
22 | third_max = max_val
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:23:24:
23 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:24:19:
24 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:25:33:
25 | max_val = nums(i)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:26:15:
26 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:27:11:
27 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:30:47:
30 | if (third_max == -huge(third_max)) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:31:31:
31 | third_max = max_val
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:32:11:
32 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:34:7:
34 | end function third_distinct_maximum
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:38:9:
38 | use third_distinct_maximum
| 1
Fatal Error: Cannot open module file ‘third_distinct_maximum.mod’ for reading at (1): No such file or directory
compilation terminated.
def third_max(nums):
top3 = set()
for num in nums:
top3.add(num)
if len(top3) > 3:
top3.remove(min(top3))
return min(top3) if len(top3) == 3 else max(top3)
The algorithm to solve the problem is as follows:
1. Initialize an empty set called top3 to keep track of the top 3 distinct maximum numbers in the array.
2. Iterate through the input array, nums.
3. For each number in nums, insert it into top3.
4. If the size of top3 becomes greater than 3, remove the smallest number in top3.
5. If the size of top3 is 3, which means there are three distinct maximum numbers, return the smallest number in top3. Otherwise, return the largest number in top3.
#include <set>
int thirdMax(vector<int>& nums) {
set<int> top3;
for (int num : nums) {
top3.insert(num);
if (top3.size() > 3) {
top3.erase(top3.begin());
}
}
return top3.size() == 3 ? *top3.begin() : *top3.rbegin();
}
The algorithm to solve the problem is as follows:
1. Initialize an empty set called top3 to keep track of the top 3 distinct maximum numbers in the array.
2. Iterate through the input array, nums.
3. For each number in nums, insert it into top3.
4. If the size of top3 becomes greater than 3, remove the smallest number in top3.
5. If the size of top3 is 3, which means there are three distinct maximum numbers, return the smallest number in top3. Otherwise, return the largest number in top3.