Third Maximum Number LT414

Posted taste-it-own-it-love-it

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Third Maximum Number LT414相关的知识,希望对你有一定的参考价值。

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).

Example 1:

Input: [3, 2, 1]

Output: 1

Explanation: The third maximum is 1.

 

Example 2:

Input: [1, 2]

Output: 2

Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

 Idea. Unique is the key, at first tried Integer.MIN_VALUE to initialise the array, more work to tell if the max3 is MIN_VALUE or not, use null instead

Time complexity: O(N)

Space complexity: O(1)

 1 class Solution {
 2     public int thirdMax(int[] nums) {
 3         Integer max1 = null;
 4         Integer max2 = null;
 5         Integer max3 = null;
 6         
 7         for(int num: nums) {  
 8             if(max1 == null || num > max1) {
 9                 max3 = max2;
10                 max2 = max1;
11                 max1 = num;
12             }
13             else if(num != max1 && (max2 == null || num > max2)) {
14                 max3 = max2;
15                 max2 = num;
16             }
17             else if((num != max1 && num != max2) && (max3 == null || (num > max3))) {
18                 max3 = num;
19             }
20         }
21         
22         if(max3 == null) {
23             return max1;
24         }
25         return max3;
26     }
27 }

Use Integer to wrap the array, to remove the duplicates in max1, max2 and max3, avoid the long if conidition

 1 class Solution {
 2     public int thirdMax(int[] nums) {
 3         Integer max1 = null;
 4         Integer max2 = null;
 5         Integer max3 = null;
 6         
 7         for(Integer num: nums) { 
 8             if(num.equals(max1) || num.equals(max2) || num.equals(max3)) {
 9                 continue;
10             }
11             
12             if(max1 == null || num > max1) {
13                 max3 = max2;
14                 max2 = max1;
15                 max1 = num;
16             }
17             else if(max2 == null || num > max2) {
18                 max3 = max2;
19                 max2 = num;
20             }
21             else if(max3 == null || num > max3) {
22                 max3 = num;
23             }
24         }
25         
26         if(max3 == null) {
27             return max1;
28         }
29         return max3;
30     }
31 }

Another idea, use Long.MIN_VALUE instead

 1 class Solution {
 2     public int thirdMax(int[] nums) {
 3         long max1 = Long.MIN_VALUE;
 4         long max2 = Long.MIN_VALUE;
 5         long max3 = Long.MIN_VALUE;
 6         
 7         for(int num: nums) {
 8             if(num > max1) {
 9                 max3 = max2;
10                 max2 = max1;
11                 max1 = num;
12             }
13             else if(num != max1 && num > max2) {
14                 max3 = max2;
15                 max2 = num;
16             }
17             else if((num != max1 && num != max2) && num > max3) {
18                 max3 = num;
19             }
20         }
21         
22         return max3 == Long.MIN_VALUE? (int)max1 : (int)max3;
23     }
24 }

 

 

Example 3:

Input: [2, 2, 3, 1]

Output: 1

Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.

以上是关于Third Maximum Number LT414的主要内容,如果未能解决你的问题,请参考以下文章

414. Third Maximum Number

414. Third Maximum Number

LeetCode Third Maximum Number

414. Third Maximum Number

Third Maximum Number

Third Maximum Number