1159. Painter's Reverse Pass

Medium3.5 points estimatedUnion FindSimulationArray

A fence has n unpainted planks numbered 0..n-1. A painter applies coats in the order given by paints. Coat paints[i] = [l, r, colour] covers planks l to r inclusive with colour, hiding whatever was visible there before. The two ends may be listed in either order.

Return an array of length n holding the colour finally visible on each plank, or -1 for a plank that never received any paint. Constraints: 1 <= n <= 2*10^5, 1 <= paints.length <= 10^5, 0 <= l, r < n and 0 <= colour <= 10^9. Repainting every plank of every coat is too slow; aim for near-linear O((n + paints.length) * alpha(n)) time and O(n) space.

Example 1

Input:
n = 6, paints = [[0,3,5],[2,4,7]]
Output:
[5,5,7,7,7,-1]
Explanation:

The second coat covers planks 2 to 4 with colour 7, leaving colour 5 on planks 0 and 1 and plank 5 unpainted.

Example 2

Input:
n = 3, paints = [[2,0,1]]
Output:
[1,1,1]
Explanation:

The ends are given in reverse, but the coat still covers planks 0 to 2 with colour 1.

Example 3

Input:
n = 4, paints = [[1,1,9],[1,1,8]]
Output:
[-1,8,-1,-1]
Explanation:

Both coats hit plank 1 and the later one wins, so plank 1 ends with colour 8.

Constraints

1 ≤ n ≤ 2*105
1 ≤ paints.length ≤ 105
0 ≤ l, r < n
0 ≤ colour ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O((n + q) * alpha(n))
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…