-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path356.cpp
More file actions
39 lines (36 loc) · 1.15 KB
/
356.cpp
File metadata and controls
39 lines (36 loc) · 1.15 KB
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
class Solution {
public:
double getMiddleLine(map<int, int>& xMap) {
int n = xMap.size();
if (n == 1) return xMap.begin()->first;
vector<pair<double, int>> xV;
for (auto& [num, cnt] : xMap) xV.push_back(make_pair(num, cnt));
int left = 1;
int right = n - 2;
double middle = (xV[0].first + xV[n - 1].first) / 2;
while (left <= right) {
double m = (xV[left].first + xV[right].first) / 2;
if (m != middle) return INT_MAX;
left++;
right--;
}
return middle;
}
bool isReflected(vector<vector<int>>& points) {
unordered_map<int, map<int, int>> mp;
for (auto& point : points) {
mp[point[1]][point[0]]++;
}
double line = INT_MAX;
for (auto& [y, xMap] : mp) {
if (line == INT_MAX) {
double middle = getMiddleLine(xMap);
if (middle == INT_MAX) return false;
line = middle;
continue;
}
if (line != getMiddleLine(xMap)) return false;
}
return true;
}
};