Leetcode 356 Solution

This article provides solution to leetcode question 356 (line-reflection)

https://leetcode.com/problems/line-reflection

Solution

class Solution {
public:
    bool isReflected(vector<pair<int, int>>& points) {
        sort(points.begin(), points.end());
        int j = 1;
        for (int i = 1; i < points.size(); i++)
        {
            if (points[i].first == points[i - 1].first && points[i].second == points[i - 1].second)
                continue;

            points[j++] = points[i];
        }
        points.resize(j);
        sort(points.begin(), points.end());

        auto points2 = points;
        for (auto& point : points2)
            point.first = -point.first;
        sort(points2.begin(), points2.end());

        int n = points.size();
        double middle_val = 0;
        for (int i = 0; i < n; i++)
        {
            auto pt1 = points[i];
            auto pt2 = points2[i];

            pt2.first = -pt2.first;

            if (pt1.second != pt2.second)
                return false;

            if (i == 0)
                middle_val = (pt1.first + pt2.first) * 0.5;
            else if (middle_val != (pt1.first + pt2.first) * 0.5)
                return false;
        }

        return true;
    }
};