A power grid is a row of power lines, given as an array power_lines. Line i is adjacent to line i - 1 and line i + 1, and to no other line.
Every line must be serviced in exactly one maintenance period. A period can service any subset of lines as long as no two lines in that subset are adjacent, because two adjacent lines must never be down at the same time.
Return the minimum number of maintenance periods needed to service every line.
Function Signature
def min_maintenance_periods(power_lines: list[int]) -> int:
Rules
-
Only the positions of the lines matter. The values stored in
power_lines
are labels and do not affect the answer.
-
The row is linear. The first and last lines are not adjacent to each other.
Constraints
-
1 <= len(power_lines) <= 10^5
Examples
Input: power_lines = [1, 2, 3, 4, 5]
Output: 2
Lines 0, 2 and 4 can share period 1, and lines 1 and 3 can share period 2. One period is not enough because lines 0 and 1 are adjacent.
Input: power_lines = [10]
Output: 1
A single line has no neighbors, so one period services it.
Input: power_lines = [7, 7]
Output: 2
The two lines are adjacent, so they need different periods. Equal values do not change that.