File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 8 ms, faster than 77.41% of C++ online submissions for Third Maximum Number.
2
+ // Memory Usage: 9.1 MB, less than 76.92% of C++ online submissions for Third Maximum Number.
3
+
4
+ class Solution
5
+ {
6
+ public:
7
+ int thirdMax (vector<int >& nums)
8
+ {
9
+ int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;
10
+ bool found_INT_MIN = false ;
11
+
12
+ for (auto num : nums)
13
+ {
14
+ if (num == INT_MIN) found_INT_MIN = true ;
15
+ if (num == max1 || num == max2 || num <= max3) continue ;
16
+
17
+ if (num > max1)
18
+ {
19
+ max3 = max2;
20
+ max2 = max1;
21
+ max1 = num;
22
+ }
23
+ else if (num > max2)
24
+ {
25
+ max3 = max2;
26
+ max2 = num;
27
+ }
28
+ else
29
+ max3 = num;
30
+ }
31
+
32
+ if (max3 == INT_MIN)
33
+ {
34
+ if (found_INT_MIN && max2 != INT_MIN) return max3;
35
+ else return max1;
36
+ }
37
+ else
38
+ return max3;
39
+ }
40
+ };
You can’t perform that action at this time.
0 commit comments