Minimum Operations to Make Binary Array Elements Equal to One I
Real-world version: Raising Every Flag With Triple FlipsThe statement here is our own: the same inputs, outputs and technique.
A row of flags holds only the values 0 and 1. One move picks any three neighbouring flags and flips each of them: a 0 becomes a 1 and a 1 becomes a 0.
Return the fewest moves needed to leave every flag showing 1, or -1 when no number of moves can do it.
Examples
Example 1
- Input
- flags = [0, 0, 0]
- Output
- 1
One move over all three flags raises the whole row.
Example 2
- Input
- flags = [0, 0, 1, 0, 0]
- Output
- 2
Flipping the first three flags leaves 1, 1, 0, 0, 0 and flipping the last three finishes the row.
Example 3
- Input
- flags = [1, 1, 1, 0, 1]
- Output
- -1
The single 0 sits second from the end. Working left to right nothing needs flipping, and the two trailing flags start no window, so that 0 can never be raised.
Constraints
- 3 <= flags.length <= 10^5
- 0 <= flags[i] <= 1
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 min_operations(flags: list[int]) -> int:public int minOperations(int[] flags)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
- Minimum Number of K Consecutive Bit FlipsReal-world version: Flipping Windows to Raise Every SwitchHard
- Shortest Subarray with Sum at Least KReal-world version: Shortest Run Meeting the Grid QuotaHard
- Maximize the Minimum Powered CityReal-world version: Lift the Weakest Relay StopHard
- Max Consecutive Ones IIIReal-world version: Longest Verified Backup StreakMedium