267. Rectangles Touch
Two solar panels are mounted on a flat roof. Each panel is an axis-aligned rectangle given as [x1, y1, x2, y2], where (x1, y1) is the lower-left corner and (x2, y2) the upper-right corner, with x1 < x2 and y1 < y2. Panels a and b are described in the same coordinate system.
Return true only if the two panels share a region of strictly positive area. Panels that merely touch along an edge or at a single corner do not count, and neither does one panel sitting apart from the other. Run in O(1) time and O(1) space; coordinates may be as large as 10^9 in magnitude, which is fine for 32-bit comparisons because no arithmetic is needed.
Example 1
- Input:
- a = [-3,2,4,9], b = [1,5,8,12]
- Output:
- true
- Explanation:
The x-ranges overlap on 1..4 and the y-ranges on 5..9, so the shared region has area 12.
Example 2
- Input:
- a = [0,0,3,3], b = [3,0,7,3]
- Output:
- false
- Explanation:
They only share the vertical edge x = 3, which has zero area.
Example 3
- Input:
- a = [-20,-20,20,20], b = [-2,-3,4,5]
- Output:
- true
- Explanation:
The second panel lies fully inside the first, so they overlap.
Constraints
a.length = b.length = 4
-109 ≤ x1 < x2 ≤ 109 and -109 ≤ y1 < y2 ≤ 109 for each rectangle
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(1)
- Space
- O(1)