Minimum Operations to Make the Array Alternating
Real-world version: Fewest Changes to Make the Strip AlternateThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def minimum_operations(readings: list[int]) -> int:public int minimumOperations(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Task SchedulerReal-world version: Press Die CooldownMedium
- Maximum Palindromes After OperationsReal-world version: Noticeboard Word StripsMedium
- Equal Sum Arrays With Minimum Number of OperationsReal-world version: Fewest Redials to Level Two TalliesMedium
- Majority ElementReal-world version: Quorum Drift ValueEasy