All problems
1088MediumArrayHash TableGreedyCounting

Fewest Changes to Make the Strip Alternate

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2170Minimum Operations to Make the Array Alternating

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A strip of positive readings reads readings. The strip alternates when every two readings that sit two places apart are equal and no two neighbouring readings are equal. A strip of one reading alternates already.

One change replaces a single reading with any positive whole number. Return the fewest changes that leave the strip alternating.

Examples

Example 1

Input
readings = [2, 1, 2, 1]
Output
0

The strip already alternates: the even positions all read two, the odd ones all read one, and the two differ.

Example 2

Input
readings = [1, 1, 1, 1]
Output
2

Both sides want to read one, but they must differ, so one side has to change every one of its two readings.

Example 3

Input
readings = [5, 5]
Output
1

The two readings are equal and sit next to each other, so one of them has to change.

Constraints

  • 1 <= readings.length <= 10^5
  • 1 <= readings[i] <= 10^5

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def minimum_operations(readings: list[int]) -> int:
Java
public int minimumOperations(int[] readings)
September 7
Apply