Minimum Rounds to Recreate a 1D Painting
You are given an integer array colors of length N, where colors[i] represents the final color visible at position i.
A painting is created on a 1-dimensional canvas using intervals.
Each color is used in at most one continuous interval during the original painting process.
Some painted intervals may later be fully or partially covered by other colors.
Your task is to determine the minimum number of rounds needed to recreate the given final painting under the following rules:
- In one round, you may paint multiple intervals.
- All intervals painted in the same round must be pairwise disjoint.
- Each color can be used for at most one interval in the entire process.
Return:
- the minimum number of rounds required to recreate the painting, or
- -1 if the given final painting is impossible to obtain under these rules.
A value 0 means that position is left blank and was never painted.
Example 1:
Input: colors = [0,1,4,5,1,3,3]
Output: 2
Explanation:
- Color 1 must have been painted before colors 4 and 5, because its interval stretches across them and is partially covered later.
- So at least 2 rounds are required.
Example 2:
Input: colors = [1,2,1]
Output: 2
Explanation:
- Color 1 must be painted first on interval [0,2], and then color 2 is painted on [1,1].
- Since these intervals overlap, they cannot be painted in the same round.
- So the minimum number of rounds is 2.
Now Your Turn!
Pick the correct output for the given inputInput: colors = [1,2,2,1,3,3]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= colors.length <= 100000
- 0 <= colors[i] <= N
- Here, N = colors.length
- 0 represents a blank position
- Every non-zero color appearing in the array must correspond to exactly one painted interval in the original process
- Each color can be painted at most once