Minimize flips on K |  ; Store previous flip events ; Remove an item which is out range of window . ; In a window , if A [ i ] is a even number with even times fliped , it need to be fliped again . On other hand , if A [ i ] is a odd number with odd times fliped , it need to be fliped again . ; Insert ; Driver code
def minKBitFlips ( A , K , N ) : NEW_LINE INDENT flip = [ ] NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( len ( flip ) > 0 and ( i - flip [ 0 ] >= K ) ) : NEW_LINE INDENT flip . pop ( 0 ) NEW_LINE DEDENT if ( A [ i ] % 2 == len ( flip ) % 2 ) : NEW_LINE INDENT if ( i + K - 1 >= N ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT flip . append ( i ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT A = [ 0 , 1 , 0 ] NEW_LINE N = len ( A ) NEW_LINE K = 1 NEW_LINE ans = minKBitFlips ( A , K , 3 ) NEW_LINE print ( ans ) NEW_LINE