Minimum maintenance periods so no two adjacent power lines are down together

Quick Overview

Find the fewest maintenance periods needed to service a row of power lines when no two adjacent lines can be down at the same time. It tests whether you can model the row as a path graph and prove matching lower and upper bounds before reaching for a simulation.

Minimum maintenance periods so no two adjacent power lines are down together

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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. ```text Input: power_lines = [10] Output: 1 ``` A single line has no neighbors, so one period services it. ```text Input: power_lines = [7, 7] Output: 2 ``` The two lines are adjacent, so they need different periods. Equal values do not change that.

Overview: Find the fewest maintenance periods needed to service a row of power lines when no two adjacent lines can be down at the same time. It tests whether you can model the row as a path graph and prove matching lower and upper bounds before reaching for a simulation.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.