Binary String (Codeforces 1680C)
Tags: Greedy, two pointers

You are given a string $$$s$$$ consisting of characters 0 and/or 1.You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:  the number of characters 0 left in the string;  the number of characters 1 removed from the string. What is the minimum cost of removal you can achieve?

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$), consisting of characters 0 and/or 1.The total length of strings $$$s$$$ in all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case, print one integer — the minimum cost of removal you can achieve.

Note: Consider the test cases of the example:  in the first test case, it's possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is $$$1$$$;  in the second test case, it's possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is $$$3$$$;  in the third test case, it's optimal to remove four characters from the beginning;  in the fourth test case, it's optimal to remove the whole string;  in the fifth test case, it's optimal to leave the string as it is. 

Sample Inputs:
5
101110110
1001001001001
0000111111
00000
1111

Sample Outputs:
1
3
0
0
0

Solution (Python):
def min_removal_cost(test_cases):
    for test_case in test_cases:
        count_1 = test_case.count('1')
        removal_costs = [test_case[:count_1].count('0')]
        total_zeros = test_case[:count_1].count('0')
        for i in range(0, len(test_case) - count_1):
            total_zeros += int(test_case[i]) - int(test_case[i + count_1])
            removal_costs.append(total_zeros)
        print(min(removal_costs))
num_test_cases = int(input())
test_cases = [input().strip() for _ in range(num_test_cases)]
min_removal_cost(test_cases)

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    s = input().strip()
    n = len(s)
    curr = 0
    prefixCount = [0]
    for i in range(n):
        if s[i] == '0':
            curr += 1
        prefixCount.append(curr)
    cnt = [curr, n - curr]
    ans = float('inf')
    for i in range(curr + 1):
        ans = min(ans, prefixCount[i + cnt[1]] - prefixCount[i])
    return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
for x in range(int(input())):
    a = list(input())
    b = a.count('1')
    y = [a[0:b].count('0')]
    m = a[0:b].count('0')
    for i in range(0, len(a) - b):
        m += int(a[i]) - int(a[i + b])
        y.append(m)
    print(min(y))