FunctionRacerProblemsRace

Ground Covered by Rectangles

HardSorting and searchingPython, JavaScript, TypeScript, Java, C++

Each rectangle is [x1, y1, x2, y2]: its bottom left corner (x1, y1) and its top right corner (x2, y2), with sides parallel to the axes. The rectangles may overlap.

Return the area of the ground covered by at least one rectangle. Ground under several rectangles counts once.

Example 1

rects [[0,0,2,2],[1,1,3,3]]
returns 7

Each square covers 4, and they share a square of area 1: 4 + 4 - 1 = 7.

Example 2

rects [[0,0,5,1],[2,0,3,4]]
returns 8

The flat rectangle covers 5 and the upright one 4. They share one unit square.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems