Rank Point Pairs by Bounding Hyperrectangle Volume
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Enumerate point pairs in d dimensions, compute the volume of the box they span, drop flat boxes, and return the rest in a strict multi-key order.
You are given a list of n points in d-dimensional space. Point i (0-based) has the integer coordinates points[i].
Any two distinct indices i < j define an axis-aligned box whose opposite corners are points[i] and points[j]. On each axis, the side length of this box is the absolute difference between the two points' coordinates on that axis. The box's volume is the product of its d side lengths. When d = 2, the box is a rectangle and the volume is its area. Your solution must work for every d in the allowed range.
Find every pair whose box has a volume greater than zero, and report each one together with its volume.
Function
def rank_box_pairs(points: List[List[int]]) -> List[List[int]]:
Output format and rules
- Every reported entry is a list
[i, j, volume]withi < j, whereiandjare indices intopoints. - A pair with volume
0is not reported. This happens when the two points have the same coordinate on at least one axis, and it includes pairs of identical points. - Sort the result by
volumefrom largest to smallest. If volumes are equal, put the smallerifirst. Ifiis also equal, put the smallerjfirst. - If no pair qualifies, return an empty list.
Constraints
1 <= n <= 3002 <= d <= 6, and every point has exactlydcoordinates-100 <= points[i][k] <= 100- Every volume is at most
200^6 = 6.4 * 10^13
Examples
Example 1
Input: rank_box_pairs([[1, 1], [4, 3], [1, 5], [3, 0]])
Output: [[2, 3, 10], [0, 1, 6], [1, 2, 6], [1, 3, 3], [0, 3, 2]]
Example 2
Input: rank_box_pairs([[0, 0, 0], [2, 3, 1], [2, 1, 4]])
Output: [[0, 2, 8], [0, 1, 6]]
Example 3
Input: rank_box_pairs([[5, 5]])
Output: []
Hints
Hint 1
The output can contain every pair of points, so you need to look at all i < j pairs. With n at most 300 that is fine.
Hint 2
Do not hard-code x and y. Walk over the coordinates of both points together and multiply the absolute differences, stopping early if one difference is zero.
Hint 3
For the ordering, negate only the volume in the sort key, for example (-volume, i, j). Using reverse=True on the whole key would also flip the tie-breakers.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The answer may include every pair of points, so the output alone can have on the order of n^2 entries. That makes direct enumeration of all pairs the right approach, and the limits (n <= 300, d <= 6) keep it cheap.
- For each pair
i < j, walk over the coordinates of both points together. Multiply the absolute differences to get the volume. - If any coordinate is equal on some axis, the volume is
0. Stop multiplying immediately and skip the pair. - Collect
[i, j, volume]for the remaining pairs and sort by the key(-volume, i, j).
Why it is correct
- Every pair is visited once. Starting the inner loop at
i + 1covers each unordered pair exactly once and always givesi < j, which is the required output form. - Volume. The side length on axis
kis|p[k] - q[k]|, and the volume is the product of the sides. This works for any number of dimensions because the loop runs over all coordinates. - Zero filter. A product is zero exactly when one factor is zero, i.e. when the points share a coordinate on some axis. Identical points share all coordinates, so they are excluded as well. Stopping early gives the same result as computing the full product and testing it for zero.
- Ordering. Negating only the volume puts larger volumes first while keeping
iandjascending for ties. Volumes are integers, so no floating-point issues arise, and Python integers do not overflow.
Complexity
- Time:
O(n^2 * d + P log P), whereP <= n(n-1)/2is the number of reported pairs. - Space:
O(P)for the result.
Since P can be on the order of n^2, producing the output already costs that much, so nothing asymptotically better is possible.
Edge cases
n = 1gives no pairs, so the result is[].- Pairs that share a coordinate on any axis are dropped, even if they differ a lot elsewhere.
- Negative coordinates are handled by
abs. - If all points lie on a common axis-aligned hyperplane, the result is
[].
Reference solution
from typing import List
def rank_box_pairs(points: List[List[int]]) -> List[List[int]]:
n = len(points)
res = []
for i in range(n):
p = points[i]
for j in range(i + 1, n):
q = points[j]
v = 1
for a, b in zip(p, q):
if a == b:
v = 0
break
v *= abs(a - b)
if v:
res.append([i, j, v])
res.sort(key=lambda t: (-t[2], t[0], t[1]))
return res