23ms C++ with Explanation

  • 0

    We first find a possible reflection line by finding two furthest points on X axis, minx and maxx. mid = (min+max)/2. X = mid is the line. How do we verify it is a valid reflection line for all points, we put all points into a set. Any point A in the set has to satisfy:
    A is on X = mid, we dont care about its y value.
    OR There exists another point (B) on the other side of the reflection line such that (A.x + B.x)/2 = mid and A.y = B.y.

    class Solution {
    bool isReflected(vector<pair<int, int>>& points) {
    int midx = 0, minx = INT_MAX, maxx = INT_MIN;
    set<pair<int, int>> myMap;

        for(auto p : points){
            minx = min(minx, p.first);
            maxx = max(maxx, p.first);
        midx = minx + maxx;
        for(auto m : myMap){
            auto it = myMap.find(make_pair(midx - m.first, m.second));
            // If a point is not on the line, and it doesn't have a symmetric point on the other side
            // return false.
            if(midx - m.first != 0 && it == myMap.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.