Rectangle Area
Real-world version: Total Rug CoverageThe statement here is our own: the same inputs, outputs and technique.
Two rugs lie flat on a floor, each with its sides parallel to the floor's grid lines. A survey marker sits at the origin, so a coordinate may be negative.
The first rug spans from the corner (ax1, ay1) to the opposite corner (ax2, ay2), with ax1 the smaller horizontal coordinate and ay1 the smaller vertical one. The second rug spans from (bx1, by1) to (bx2, by2) under the same rule. A rug may be folded flat, in which case one of its sides has length zero and it covers nothing.
Return the area of floor that has at least one rug over it. Floor that both rugs cover counts once. Two rugs whose edges only meet along a line, or at a single point, do not share any area.
Examples
Example 1
- Input
- ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = 0, by1 = 0, bx2 = 4, by2 = 3
- Output
- 24
The first rug covers 16 units of floor and the second covers 12. The floor they share runs horizontally from 0 to 2 and vertically from 0 to 2, so 4 units are under both and are counted once: 16 + 12 - 4 = 24.
Example 2
- Input
- ax1 = 0, ay1 = 0, ax2 = 2, ay2 = 2, bx1 = 2, by1 = 0, bx2 = 4, by2 = 2
- Output
- 8
The rugs meet along the vertical line at 2 and share no area, so the total is 4 + 4 = 8.
Example 3
- Input
- ax1 = -5, ay1 = -5, ax2 = 5, ay2 = 5, bx1 = -1, by1 = -1, bx2 = 1, by2 = 1
- Output
- 100
The smaller rug lies entirely on top of the larger one, so the covered floor is just the larger rug's 100 units.
Constraints
- -10^4 <= ax1 <= ax2 <= 10^4
- -10^4 <= ay1 <= ay2 <= 10^4
- -10^4 <= bx1 <= bx2 <= 10^4
- -10^4 <= by1 <= by2 <= 10^4
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def compute_area(ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Rectangle OverlapReal-world version: Do the Two Plots Share Any GroundEasy
- Largest Triangle AreaReal-world version: The Largest Triangle Between PegsEasy
- K Closest Points to OriginReal-world version: Nearest Survey StakesMedium
- Detonate the Maximum BombsReal-world version: Largest Chain of Quarry ChargesMedium