Trains the technique from
LeetCode 3191Minimum Operations to Make Binary Array Elements Equal to One IThis 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 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.
Example 1
One move over all three flags raises the whole row.
Example 2
Flipping the first three flags leaves 1, 1, 0, 0, 0 and flipping the last three finishes the row.
Example 3
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.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def min_operations(flags: list[int]) -> int:public int minOperations(int[] flags)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.