Skip to content

1637. Widest Vertical Area Between Two Points Containing No Points 👎

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  int maxWidthOfVerticalArea(vector<vector<int>>& points) {
    int ans = 0;
    vector<int> xs;

    for (const vector<int>& point : points) {
      const int x = point[0];
      xs.push_back(x);
    }

    ranges::sort(xs);

    for (int i = 1; i < xs.size(); ++i)
      ans = max(ans, xs[i] - xs[i - 1]);

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
  public int maxWidthOfVerticalArea(int[][] points) {
    int ans = 0;
    int[] xs = new int[points.length];

    for (int i = 0; i < points.length; ++i)
      xs[i] = points[i][0];

    Arrays.sort(xs);

    for (int i = 1; i < xs.length; ++i)
      ans = Math.max(ans, xs[i] - xs[i - 1]);

    return ans;
  }
}
1
2
3
4
class Solution:
  def maxWidthOfVerticalArea(self, points: list[list[int]]) -> int:
    xs = sorted([x for x, _ in points])
    return max(b - a for a, b in itertools.pairwise(xs))