bits of length n made of 0 and 1. Two operations are allowed:
- pick an index i and invert every character from position 0 through i, at a cost of i + 1;
- pick an index i and invert every character from position i through n - 1, at a cost of n - i.
Inverting a character turns 0 into 1 and vice versa.
Return the minimum total cost of making all characters of the string the same.bits="1100" → 2
Flip the tail starting at position 2 for 4 − 2 = 2 and get 1111.
bits="10110" → 4
Flip the head up to position 0 for 1 (00110), the tail from position 4 for 1 (00111), the head up to position 1 for 2 (11111). Total 4.
bits="0" → 0
bits contains only 0 and 1bits="1100"
2
Run checks the first 3 tests in your browser. Submit checks all 165 tests on the server, hidden ones included.