LeetCode 1637. Widest Vertical Area Between Two Points Containing No Points Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1637. Widest Vertical Area Between Two Points Containing No Points

Description

Given n points on a 2D plane where points[i] = [xi, yi], Return the widest vertical area between two points such that no points are inside the area.

A vertical area is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The widest vertical area is the one with the maximum width.

Note that points on the edge of a vertical area are not considered included in the area.

 

Example 1:

Input: points = [[8,7],[9,9],[7,4],[9,7]]
Output: 1
Explanation: Both the red and the blue area are optimal.

Example 2:

Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
Output: 3

 

Constraints:

  • n == points.length
  • 2 <= n <= 105
  • points[i].length == 2
  • 0 <= xi, yi <= 109

Solutions

Solution 1

PythonJavaC++GoTypeScriptJavaScript
class Solution: def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int: points.sort() return max(b[0] - a[0] for a, b in pairwise(points))(code-box)

Solution 2

PythonJavaC++GoTypeScriptJavaScript
class Solution: def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int: nums = [x for x, _ in points] n = len(nums) mi, mx = min(nums), max(nums) bucket_size = max(1, (mx - mi) // (n - 1)) bucket_count = (mx - mi) // bucket_size + 1 buckets = [[inf, -inf] for _ in range(bucket_count)] for x in nums: i = (x - mi) // bucket_size buckets[i][0] = min(buckets[i][0], x) buckets[i][1] = max(buckets[i][1], x) ans = 0 prev = inf for curmin, curmax in buckets: if curmin > curmax: continue ans = max(ans, curmin - prev) prev = curmax return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !