C++ 19ms sort O(1) space solution


  • 0
    N

    Just need to sort to deal with duplicate points. If there are no duplicates, sort is not needed.

    bool isReflected(vector<pair<int, int>>& points) {
            int n = points.size();
            
            if (n <= 1) return true;
            
            int minx = INT_MAX, maxx = INT_MIN;
            for (int i = 0; i < n; i++)
            {
                minx = min(points[i].first, minx);
                maxx = max(points[i].first, maxx);
            }
            
            int midx = minx + maxx;
            
            
            std::sort(points.begin(), points.end());
            int yXor = 0;
            int xXor = 0;
            for (int i = 0; i < n; i++)
            {
                if (i >= 1 && points[i] == points[i - 1]) continue;
                
                if (2 * points[i].first != midx)
                {
                    xXor = xXor ^ ((2 * points[i].first < midx) ? (midx - points[i].first) : points[i].first);
                    yXor = yXor ^ points[i].second;
                }
            }
            
            return (xXor == 0 && yXor == 0);
        }
    

Log in to reply
 

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