All problems
0614MediumMathGeometry

Total Rug Coverage

Tracked in this browser only
Write code

Trains the technique from

LeetCode 223Rectangle Area

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def compute_area(ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
Java
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2)
September 7
Apply