Smallest Axis-Aligned Box Around Scattered Points
Arrays & HashingAuto-graded · 18 test casesLast updated
Scan a list of integer 2D points once to find the extreme x and y values, then report the enclosing axis-aligned box as a corner plus width and height.
Enclosing Box for a Set of Points
You are given a non-empty list of points on a 2D plane. Each point is a pair (x, y) of integers.
Find the smallest rectangle whose sides are parallel to the coordinate axes and which contains every point (points lying on the boundary count as contained).
Describe that rectangle with three values:
- its lower-left corner, as the pair
(smallest x, smallest y)over all points, - its
width, the largest x minus the smallest x, - its
height, the largest y minus the smallest y.
Return these as (corner, width, height), where corner is a pair of two integers and width and height are integers.
Width and height are plain differences of coordinates, so do not add 1 to either. A rectangle may be degenerate: if all points share the same x, the width is 0; if they share the same y, the height is 0. A single point yields a corner equal to that point with width 0 and height 0.
Constraints
- The list contains between 1 and 10^6 points.
- Every coordinate is an integer in the range from -10^9 to 10^9.
- Duplicate points may appear.
Examples
Example 1
Input: bounding_box([[1, 1], [3, 4], [0, 2]])
Output: [[0, 1], 3, 3]
Example 2
Input: bounding_box([[5, 5]])
Output: [[5, 5], 0, 0]
Hints
Hint 1
Which points actually determine the rectangle? Only the ones holding the smallest and largest x, and the smallest and largest y.
Hint 2
Initialize all four extremes from the first point, then walk through the rest and widen each bound whenever a point falls outside it.
Hint 3
Check x and y updates independently for every point (separate `if` statements), since one point may improve both an x bound and a y bound. Width is max_x - min_x with no +1.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The rectangle is fully determined by four numbers: the smallest and largest x, and the smallest and largest y. Every other point lies inside the range these define and has no influence on the result.
Start all four extremes at the first point (the input is guaranteed non-empty). Then go through the points once. For each point, compare its x with the current minimum and maximum x, and its y with the current minimum and maximum y, updating whichever bound it exceeds. Finally, return the corner (min_x, min_y), the width max_x - min_x, and the height max_y - min_y.
Why it is correct
After handling the first k points, the four stored values are exactly the true extremes of those k points. This holds initially for k = 1, and each new point either leaves a bound unchanged or becomes the new bound, so it holds for k + 1 as well. At the end the extremes describe the whole set.
The rectangle [min_x, max_x] x [min_y, max_y] contains every point. It is also minimal: moving any side inward would exclude the point that attains that extreme. The four comparisons are independent if checks, so a point that sets both an x bound and a y bound updates both.
Edge cases fall out naturally: a single point gives width and height 0, negative coordinates need no special handling, and shared x or y values give a zero width or height. Duplicates never change a bound.
Complexity
- Time: O(n), one pass with four constant-time comparisons per point. This is optimal since any point could hold an extreme.
- Space: O(1) extra.
Reference solution
from typing import List, Tuple
def bounding_box(points: List[Tuple[int, int]]) -> Tuple[Tuple[int, int], int, int]:
min_x, min_y = points[0]
max_x, max_y = min_x, min_y
for p in points:
x, y = p[0], p[1]
if x < min_x:
min_x = x
if x > max_x:
max_x = x
if y < min_y:
min_y = y
if y > max_y:
max_y = y
return ((min_x, min_y), max_x - min_x, max_y - min_y)