-
Notifications
You must be signed in to change notification settings - Fork 12
/
MaximumDistanceInArrays.cpp
77 lines (71 loc) · 2.59 KB
/
MaximumDistanceInArrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Solution {
public:
void swap(int& a, int& b){
int temp = a;
a = b;
b = temp;
}
int maxDistance(vector<vector<int>>& arrays) {
int maxi = INT_MIN, smaxi = INT_MIN, mini = INT_MAX, smini = INT_MAX, imaxi = -1, ismaxi = -1, imini = -1, ismini = -1;
for(int i = 0; i < arrays.size(); i++){
int len = arrays[i].size();
if(len){
if(maxi == INT_MIN){
maxi = arrays[i][len-1];
imaxi = i;
}
else if(smaxi == INT_MIN){
smaxi = arrays[i][len-1];
ismaxi = i;
if(maxi < smaxi){
swap(maxi, smaxi);
swap(imaxi, ismaxi);
}
}
else{
if(arrays[i][len-1] > maxi){
smaxi = maxi;
ismaxi = imaxi;
maxi = arrays[i][len-1];
imaxi = i;
}
else if(arrays[i][len-1] > smaxi && smaxi != INT_MIN){
smaxi = arrays[i][len-1];
ismaxi = i;
}
}
if(mini == INT_MAX){
mini = arrays[i][0];
imini = i;
}
else if(smini == INT_MAX){
smini = arrays[i][0];
ismini = i;
if(mini > smini){
swap(mini, smini);
swap(imini, ismini);
}
}
else{
if(arrays[i][0] < mini){
smini = mini;
ismini = imini;
mini = arrays[i][0];
imini = i;
}
else if(arrays[i][0] < smini && smini != INT_MAX){
smini = arrays[i][0];
ismini = i;
}
}
}
// cout << i << endl;
// cout << "maxi : " << maxi << ", mini : " << mini << ", smaxi : " << smaxi << ", smini : " << smini << endl;
// cout << "imaxi : " << imaxi << ", imini : " << imini << ", ismaxi : " << ismaxi << ", ismini : " << ismini << endl;
}
if(imaxi == imini){
return max(abs(maxi - smini), abs(smaxi - mini));
}
return abs(maxi - mini);
}
};