356. Line Reflection - cocoder39/coco39_LC GitHub Wiki
356. Line Reflection
first loop finds the min
and max
coordinate x. then the mid is guaranteed to be min + max / 2
, sorting is not necessary
time complexity is O(n)
bool isReflected(vector<pair<int, int>>& points) {
unordered_map<int, unordered_set<int>> map;
int left = INT_MAX, right = INT_MIN;
for (int i = 0; i < points.size(); i++) {
map[points[i].first].insert(points[i].second);
left = min(left, points[i].first);
right = max(right, points[i].first);
}
long sum = (long)left + (long)right;
for (int i = 0; i < points.size(); i++) {
auto it = map.find(sum - points[i].first);
if (it == map.end() || (it->second).find(points[i].second) == (it->second).end()) {
return false;
}
}
return true;
}