Official algorithmic problem description and constraints.
There is a canvas with N
N total cells, and each cell can be painted white or black. Currently, the state of the board can be represented by a binary string S
S, where Si=1
Si
=1 if and only if the i
i-th call in the canvas is painted black, and Si=0
Si
=0 otherwise.
You want the black cells to form a contiguous subarray, i.e. if cell x
x and cell y
y are coloured black, then all cells z
z between the 2
2 must also be black. Note that if there are no black cells, this condition is satisfied.
You can only change the colour of a cell from white to black (but not the other way around). Find the minimum number of changes needed.
For each test case, output on a new line the minimum number of cells that need to be changed from white to black, such that the black cells form a contiguous subarray.
Input
Output
4 2 00 3 101 4 0110 10 0100010011 0 1 0 5
Test Case 1 : There are no black cells, so the condition is satisfied.
Test Case 2 : We can colour the 2nd
2nd
cell, and now the condition is satisfied.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.