33 ms O(nlogn) 22 lines C++ solution with comments (testcases passed with >95% )


  • 0
    D
    class Solution {
        struct Order{
            bool operator()(pair<int,int> a,pair<int,int> b){
                return a.first<b.first;
            }
        };
    public:
        bool isReflected(vector<pair<int, int>>& points) {
            set<pair<int,int>,Order> s1{points.begin(),points.end()}; //Set to store the unique points in sorted order
            vector<pair<int,int> > points2{s1.begin(),s1.end()}; //Since set is an associative container, we need to store in the vector
            int i,j,n=points2.size();
            if(n<2) return true; //Only one point case
            double m3; //mid point on the x axis
            if(n%2==1) m3=points2[n/2].first;
            else m3=(points2[(n/2)-1].first + points2[n/2].first)/2.0;
            //m3 is the mid point so the equation is x=m3, for the line parallel to the yaxis, now we iterate through the list , one from the begining one from the end
            for(i=0,j=n-1;i<=j;i++,j--){
                if(points2[i].second!=points2[j].second or points2[i].first+2*(m3-points2[i].first)!=points2[j].first or  points2[j].first-2*(points2[j].first-m3)!=points2[i].first) return false;
            }
            return true;
        }
    };
    
    

Log in to reply
 

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