2022-03-27 20:52:13 +08:00
< p > Given an integer array < code > nums< / code > , return < em > the < strong > third distinct maximum< / strong > number in this array. If the third maximum does not exist, return the < strong > maximum< / strong > number< / em > .< / p >
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:52:13 +08:00
< pre >
< strong > Input:< / strong > nums = [3,2,1]
< strong > Output:< / strong > 1
< strong > Explanation:< / strong >
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:52:13 +08:00
< pre >
< strong > Input:< / strong > nums = [1,2]
< strong > Output:< / strong > 2
< strong > Explanation:< / strong >
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.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:52:13 +08:00
< pre >
< strong > Input:< / strong > nums = [2,2,3,1]
< strong > Output:< / strong > 1
< strong > Explanation:< / strong >
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.
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = nums.length < = 10< sup > 4< / sup > < / code > < / li >
< li > < code > -2< sup > 31< / sup > < = nums[i] < = 2< sup > 31< / sup > - 1< / code > < / li >
< / ul >
< p > < / p >
< strong > Follow up:< / strong > Can you find an < code > O(n)< / code > solution?