AmazonOA #58
Segmentify (Min Even-Length Subsegments)
Estimated MediumJudge-ready
Flip bits of an even-length binary string so that every maximal run of identical characters has even length. First minimize the number of flips; then, among all plans with the fewest flips, minimize the number of maximal runs, and output the number of runs. The single-flip explanation in the source example is incorrect; the answer is computed independently with this two-level objective.
Input
A single line containing an even-length binary string, 2..100000.
Output
Output a single integer answer.
Example 1
Input:
11100110
Output:
1
Example 2
Input:
0101
Output:
1
Example 3
Input:
001100
Output:
3
Example notes
Example 1: the four pairs are 11, 10, 01, 10; each of the last three needs 1 flip and all of them can become 11, so after the minimum 3 flips there is only 1 run; the source's 2 runs is not optimal for the secondary objective. Example 2: flipping 1 bit in each pair makes everything 0, 1 run. Example 3: the three pairs are forced to be 00, 11, 00, so 3 runs are needed.
Editorial
Loading solution…