C++ O(n) unordered_map solution


  • 0
    S
    class Solution {
    public:
        bool isReflected(vector<pair<int, int>>& points) {
            if(points.empty()) return true;
            int minx = INT_MAX;
            int maxx = INT_MIN;
            unordered_map<int, unordered_set<int> > dict;
            for(auto point : points){
                minx = min(minx, point.first);
                maxx = max(maxx, point.first);
                dict[point.first].insert(point.second);
            }
            int line = maxx + minx;
            
            for(auto it = dict.begin(); it != dict.end(); it++){
                int x = line - it->first;
                if(dict.find(x) == dict.end()) return false;
                for(auto jt = it->second.begin(); jt != it->second.end(); jt++){
                    int y = *jt;
                    if(dict[x].find(y) == dict[x].end()) return false;
                }
            }
            return true;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.