forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0414.cpp
More file actions
24 lines (24 loc) · 922 Bytes
/
0414.cpp
File metadata and controls
24 lines (24 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int thirdMax(vector<int>& arr) {
int max{INT_MIN}, sMax{INT_MIN}, tMax{INT_MIN};
for(int i{0}; i < int(arr.size()); i++){
if(arr[i] > max){
tMax = sMax;
sMax = max;
max = arr[i];
}
else if(arr[i] > sMax and arr[i] < max){
tMax = sMax;
sMax = arr[i];
}
else if(arr[i] > tMax and arr[i] < sMax)
tMax = arr[i];
}
//these three checks preserve space while maintaining O(n) time complexity.
//one could also simply use long int sentinels, but that would take extra space when it's needed for only 2 border cases.
if(int(arr.size()) <= 2 or (tMax == INT_MIN and std::find(arr.begin(), arr.end(), INT_MIN) == arr.end()) or tMax == sMax)
return max;
else return tMax;
}
};