Ctrl K

Equal-Area Line

Medium
Square tiles are stuck to a wall, their sides parallel to the axes. Tile tiles[i] = [x, y, l] has its bottom-left corner at (x, y) and side length l. Tiles may overlap: each tile's area counts on its own, even where another tile covers it. Draw a horizontal line at height h. The total area of the tile parts above the line must equal the total area of the parts below it. Return the smallest such h. Answers within 10⁻⁵ of the true value are accepted.
Example 1
tiles=[[0, 0, 2], [5, 4, 2]] → 2.0

Each tile has area 4, so 4 of the total 8 must lie below the line. The lower tile is fully below height 2 and nothing lies between 2 and 4, so the lowest suitable line is at height 2.

Example 2
tiles=[[3, 1, 4]] → 3.0

A single tile spanning heights 1 to 5 is split in half exactly in the middle, at height 3.

Example 3
tiles=[[0, 0, 2], [1, 0, 2]] → 1.0

The tiles overlap, but each one's area counts separately, 8 in total. Both span heights 0 to 2, so the line at height 1 cuts each of them in half.

Constraints
  • 1 ≤ tiles.length ≤ 5·10⁴
  • tiles[i] = [x, y, l]
  • 0 ≤ x, y ≤ 10⁹
  • 1 ≤ l ≤ 10⁹
  • the total area of all tiles does not exceed 10¹²
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFind in a Shifted Sorted Array MediumFirst and Last Copy MediumFind in a Row-Chained Grid MediumFind in a Shifted Array With Repeats MediumMinimum of a Shifted List Medium
loading Python…
Input
tiles=[[0, 0, 2], [5, 4, 2]]
Expected
2.0

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.