Leetcode Daily Problem Day 21

Photo by Clark Tibbs on Unsplash

Leetcode Daily Problem Day 21

·

1 min read

Link Problem

Solution:

  • Observe that for any pair of points in the 2D plane to satisfy the condition, you need their x-axis to be as close as possible.

  • From the things I mentioned above, we can sort the array points based on the x-axis.

  • The conditions, which don't have any points without themselves in between, will be satisfied for each point in the array.

  • Return the result.

Code:

class Solution {
public:
    int maxWidthOfVerticalArea(vector<vector<int>>& points) {
        int res = 0;

        // sort the array based on x-axis
        sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) {
            return a[0] < b[0];
        });

        const int n = (int) points.size();

        // Find the differ between two points
        // Which is in turn the correct answer we want to have
        for(int i = 1; i < n; ++i) {
            res = max(res, points[i][0] - points[i - 1][0]);
        }

        // return the answer
        return res;
    }
};