Ctrl K

Tent Heights Along the Fair

Medium
Fair tents stand along a straight road. Tent tents[i] = [start, end, height] has height height and covers the half-open interval [start, end): the point start is included and the point end is not. Tents may overlap. For each point of the road covered by at least one tent, define its average height: the sum of the heights of all tents over it, divided by their count with integer division. Describe the road with the fewest non-overlapping pieces [left, right, average], where the average height is average everywhere on [left, right). Adjacent pieces that touch and share the same average must be merged into one. Stretches with no tents are left out of the answer. The pieces may be returned in any order.
Example 1
tents=[[0, 6, 3], [4, 8, 6]] → [[0, 4, 3], [4, 6, 4], [6, 8, 6]]

On [0, 4) there is only the first tent, average 3. On [4, 6) both: (3 + 6) // 2 = 4. On [6, 8) only the second, 6.

Example 2
tents=[[1, 3, 5], [2, 4, 4], [2, 4, 6]] → [[1, 4, 5]]

[1, 2): 5. [2, 3): (5 + 4 + 6) // 3 = 5. [3, 4): (4 + 6) // 2 = 5. The average is 5 everywhere and the pieces touch, so they merge into one.

Example 3
tents=[[0, 2, 7], [5, 9, 7]] → [[0, 2, 7], [5, 9, 7]]

There are no tents between 2 and 5, so the two pieces with average 7 stay separate.

Constraints
  • 1 ≤ len(tents) ≤ 10⁵
  • tents[i] = [start, end, height]
  • 0 ≤ start < end ≤ 10⁸
  • 1 ≤ height ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumJobs With a Cooldown MediumSpread Out the Labels MediumCross Out Half the List MediumOne Event per Day Medium
loading Python…
Input
tents=[[0, 6, 3], [4, 8, 6]]
Expected
[[0, 4, 3], [4, 6, 4], [6, 8, 6]]

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