Skip to the code
cf.
+50 XP

#664 Area Covered by Panels

hard

A dashboard lets users place panels freely, and panels may overlap. To show how much of the canvas is used, the app needs the total area covered by at least one panel. Each panel is [left, top, right, bottom] with left < right and top < bottom.

Write coveredArea(panels) that returns that area. Overlapping parts count once.

Example 1

Input: panels = [[0,0,4,4],[2,2,6,6]]
Output: 28

Each panel has area 16, and they share a 2 × 2 square, counted once: 16 + 16 - 4 = 28.

Example 2

Input: panels = [[0,0,3,3],[5,5,6,6]]
Output: 10

The panels don't overlap: 9 + 1 = 10.

Constraints

  • 0 <= panels.length <= 100
  • 0 <= left < right <= 10^4
  • 0 <= top < bottom <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).