Problem:


Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

1 \leq N \leq 2\times 10^5

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0



Sample input:
3
#.#


Sample output:
1

Submission status: Accepted

Solution code:
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')

def sol():
    N = int(input())
    S = input().rstrip()

    # すべてを白/黒にするコスト
    ans = min(S.count('.'), S.count('#'))

    white = [0] * (N + 1)

    for i, s in enumerate(S, start=1):
        white[i] += white[i - 1]
        if s == '.':
            white[i] += 1

    for mid, s in enumerate(S):
        # 左をすべて白にする
        left = (mid - white[mid])

        # 右をすべて黒にする
        right = white[N] - white[mid + 1]

        ans = min(ans, left + right)

    print(ans)

sol()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
counter = 0
counter = s.count('.')
result = counter
for i in s:
    if i == '#':
        counter += 1
    else:
        counter -= 1
    result = min(result, counter)
print(result)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
b=0
w=S.count(".")
ans=b+w
for s in S:
    if s=="#":
        b+=1
    else:
        w-=1
    ans=min(ans, b+w)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

ans = s.count(".")
temp = ans

for i in s:
    temp += 1 if i == "#" else -1
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()


cost = 0
bc = 0
for it in s:
    if it =="#":
        bc += 1
    elif bc > 0:
        cost += 1
        bc -= 1

print(cost)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

cum_lw = [0] * N
cum_rw = [0] * N

if S[0] == ".":
    cum_lw[0] = 1

if S[-1] == ".":
    cum_rw[-1] = 1 

for i in range(1,N):
    if S[i] == ".":
        cum_lw[i] = cum_lw[i-1] + 1
    else:
        cum_lw[i] = cum_lw[i-1]

    if S[N-1-i] == ".":
        cum_rw[N-1-i] = cum_rw[N-i] + 1
    else:
         cum_rw[N-1-i] = cum_rw[N-i]

ans = N
for i in range(N):
    if i != N-1:
        bb = cum_lw[i] + cum_rw[i+1]
        wb = (i+1)-cum_lw[i] + cum_rw[i+1]
        ww = (i+1)-cum_lw[i] + (N-1-i)-cum_rw[i+1]
    else:
        bb = cum_lw[i]
        wb = (i+1)-cum_lw[i]
        ww = (i+1)-cum_lw[i]
    ans = min([ans, bb, wb, ww])

print(ans)

Submission status: Accepted

Solution code:
n,s=open(0)
b=0;a=w=s.count(".")
for x in s:
	b+=x<"."
	w-=x>"#"
	a=min(a,b+w)
print(a)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l_b=0
r_w=s.count(".")
ans=l_b+r_w
for x in s:
	if x=="#":
		l_b+=1
	else:
		r_w-=1
	ans=min(ans,l_b+r_w)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
b, w = 0, 0
for x in s:
    if x == '#': b += 1
    elif b > 0: # 黒の右に白があるので, 黒を白に置換
        w += 1
        b -= 1
print(w)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=s.count(".")
ww=0
b=s.count("#")
bb=0
ans=[bb+w]
for i in range(n):
	if s[i]==".":
		w-=1
		ww+=1
	else:
		b-=1
		bb+=1
	ans.append(bb+w)
print(min(ans))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from fractions import Fraction

import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from fractions import Fraction

n = int(input())
S = input()
f = [0] * (n+1)
for i in range(n):
    f[i+1] = f[i] + (S[i] == '#')
g = [0] * (n+1)
for i in range(n):
    g[i+1] = g[i] + (S[i] == '.')
k = S.index('#') if '#' in S else 0
ans = min(f[n], g[n]-g[k])
lst = []
for i in range(n-1):
    if S[i:i+2] == '#.':
        lst.append(i)
for i, j in zip(lst, lst[1:]):
    ans = min(ans, f[i+1]+g[n]-g[j])
print(ans)


# N, M, K = map(int, input().split())
# A = list(map(int, input().split()))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b,c = LI()
    if min(a,b) < c < max(a,b):
        print("Yes")
    else:
        print("No")
    return

#B
def B():
    n = I()
    s = S()
    k = I()
    f = s[k-1]
    for i in range(n):
        if s[i] != f:
            print("*",end = "")
        else:
            print(s[i],end = "")
    print()
    return

#C
def C():
    n = I()
    s = S()
    ans = 0
    f = [0]*(n+1)
    for i in range(n):
        f[i+1] = 1 if s[i] == "#" else 0
    for i in range(n):
        f[i+1] += f[i]
    ans = float("inf")
    for i in range(n+1):
        ans = min(ans, f[i]+(n-i)-f[n]+f[i])
    print(ans)
    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#G
def G():

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
white = s.count('.')
black = 0
min = white
for i in range(n):
    if s[i] == '#':
        black += 1
    else:
        white -= 1
    if black + white < min:
        min = black + white 
print(min)

Submission status: Accepted

Solution code:
import fractions,math,sys
from collections import Counter
n = int(input())
s = list(input())
cnt = Counter(s)
white_l = 0
black_l = 0
min = cnt['.']
for i in range(n):
    if s[i] == '#':
        black_l += 1
        cnt['#'] -= 1
    else:
        white_l += 1
        cnt['.'] -= 1
    if black_l + cnt['.'] < min:
        min = black_l + cnt['.']
print(min)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

W = [0 for _ in range(N)]
cnt = 0
for i in range(N):
    if S[i] == "#":
        cnt += 1
    W[i] = cnt
cnt = 0

B = [0 for _ in range(N)]
for i in range(N)[::-1]:
    if S[i] == ".":
        cnt += 1
    B[i] = cnt

X = [W[i] + B[i+1] for i in range(N-1)]
X.append(W[N-1])
X.append(B[0])

print(min(X))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
S = input().rstrip()

S

# wwwwwbbbbbを目指す
# 各地点に対して、左側(exclusive)にあるbを持つ

Lb = [0] * (N+1)
for i,s in enumerate(S,1):
    Lb[i] = Lb[i-1] + (1 if s == '#' else 0)

answer = N + 100
black = Lb[-1]
for i in range(N+1):
    j = i+1
    # [0,i), [i,N) に分ける
    left = Lb[i]
    right = N - i - (black - Lb[i])
    cost = left + right
    if answer > cost:
        answer = cost

print(answer)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
b=0
w=S.count(".")
ans=b+w
for s in S:
  if s=="#":
    b+=1
  else:
    w-=1
  ans=min(ans, b+w)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(str(input()))

right_white = S.count('.')
right_black = len(S) - right_white

ans = right_white

left_white=0
left_black=0

for i in range(N):
    if(S[i] == '#'):
        left_black = left_black+1
        right_black = right_black-1
    else:
        left_white = left_white+1
        right_white = right_white-1

    if(ans > (left_black + right_white)):
        ans = (left_black + right_white)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#Tenka1 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(input())
s = input()
d = [0]+[1 if s[i] == "#" else 0 for i in range(n)]
d = list(accumulate(d))
ans = float('inf')
for i in range(1,n+1):
    ans = min(ans,d[i-1]+(n-i)-(d[-1]-d[i]))
print(ans)

Submission status: Accepted

Solution code:
# 2019/07/26
# かんにんぐ

n=int(input())
s=list(input())
bk=0
wh=s.count('.')
cnt=wh
for i in range(n):
	if s[i]=='#':bk+=1
	else:wh-=1
	cnt=min(cnt,bk+wh)

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

tb = S.count(".")
tw = 0
mi = tb + tw
for i in range(N):
    if S[i] == "#":
        tw = tw +1
    else:
        tb = tb -1
    mi = min(mi,tb+tw)


print(mi)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

left = 0
right = 0
for i in range(N):
    if S[i] == ".":
        right += 1

ans = left + right

for i in range(N):
    if S[i] == "#":
        left += 1
    else:
        right -= 1
    ans = min(ans, left + right)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l=[0]
r=[0]
for i in range(n):
    if s[i]=="#":
        l.append(l[i]+1)
    else:
        l.append(l[i])
    if s[n-i-1]==".":
        r.append(r[i]+1)
    else:
        r.append(r[i])
r.reverse()
print(min([l[i]+r[i] for i in range(n+1)]))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

W = [0] * N
W[-1] += S[-1] == "."
for i in range(N - 2, -1, -1):
    W[i] = W[i + 1] + (S[i] == ".")

B = [0] * N
B[0] += S[0] == "#"
for i in range(1, N):
    B[i] = B[i - 1] + (S[i] == "#")

T = [w + b - 1 for w, b in zip(W, B)]
T = min(T)
print(min(T, W[0], B[-1]))

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
x = S.count(".")
ans = x
for i in range(N):
    if S[i]=="#":
        x += 1
    else:
        x -= 1
    ans = (min(ans,x))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
leftblack = 0
rightwhite = s.count(".")
ans = rightwhite

for i in range(n):
  if s[i] == "#":
    leftblack += 1
  else:
    rightwhite -= 1
  ans = min(ans,leftblack + rightwhite)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
m = s.count(".")
ans = m

for i in range(n):
    if s[i] == "#":
        m += 1
    else:
        m -= 1
    ans = min(ans, m)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
m = s.count(".")
ans = m

for i in range(len(s)):
    if s[i] == "#":
        m += 1
    else:
        m -= 1
    ans = min(ans, m)
print(ans)

Submission status: Accepted

Solution code:
def solve(n, s):
    total = s.count(".")
    ans = total
    cnt = 0
    for i in range(n):
        if s[i] == "#":
            cnt += 1
        else:
            cnt -= 1
        ans = min(ans, total+cnt)
    return ans

assert solve(3, "#.#") == 1
assert solve(5, "#.##.") == 2
assert solve(9, ".........") == 0

n = int(input())
s = input()
print(solve(n, s))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
m = s.count('.')
ans = m

for i in range(n):
    if s[i] == '#':
        m += 1
    else:
        m -= 1
    ans = min(ans, m)
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
m = s.count(".")
ans = m

for i in range(n):
    if s[i] == "#":
        m += 1
    else:
        m -=1
    ans = min(ans, m)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
m = s.count('.')
ans = m

for i in range(n):
    if s[i] == '#':
        m += 1
    else:
        m -= 1
    ans = min(ans, m)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

t = s.count(".")
r = 0
ans = t
for m in range(n) :
    if s[m] == "." :
        t -= 1
    else :
        r += 1
    ans = min(ans, t+r)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

if "." not in s or "#" not in s:
    print(0)
else:
    ans = len(s)

    s = s[s.find("#"):n-s[::-1].find(".")]
    if s == "":
        print(0)
    else:
        cnt_b = [0 for i in range(len(s))]
        cnt = 0
        for i in range(len(s)):
            if s[i] == "#":
                cnt += 1
            cnt_b[i] = cnt
        ans = min(ans, len(s)-cnt_b[-1])
        for i in range(len(s)):
            ans = min(ans, cnt_b[i] + (len(s)-cnt_b[-1])-(i+1-cnt_b[i]))
        print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=s.count('.')
b=0
ans=w
for i in range(n):
    if s[i]=='#':
        b+=1
    else:
        w-=1
    ans=min(ans,w+b)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = [s for s in input()]
w,b = [0],[0]
for i in range(n):
    if S[i] is '#': w.append(w[i]+1)
    else:   w.append(w[i])
    if S[n-i-1] is '.': b.append(b[i]+1)
    else:   b.append(b[i])
b = b[::-1]
#print(w,b)
ans = float('inf')
for i in range(n+1):
    ans = min(ans, w[i]+b[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()


left_black = 0
right_white = s.count(".")
answer = left_black + right_white

for w in s:
    if w == "#":
        left_black += 1
    else:
        right_white -= 1
    answer = min(answer, left_black + right_white)

print(answer)

Submission status: Accepted

Solution code:

N = int(input())
S = " " + str(input()) + " "

ans = N+2
prev = ""


black = 0
white = S.count(".")

first_white = white
first_black = N - first_white

for i in range(N+2):
    current = S[i]

    ans = min(ans, black+white)

    if current is ".":
        white -= 1
    elif current is "#":
        black += 1

    prev = current

"""
if ans == N + 1:
    ans = 0
"""

print(min(ans, first_black, first_white))

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# 初項s, 交差dのn個の数列の和
def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N = int(input())
    S = input()

    total_b = S.count("#")
    total_w = S.count(".")
    ans = min(total_b, total_w)
    b, w = 0, 0
    for i in range(N):
        left_b = b
        right_w = total_w - w
        ans = min(ans, left_b + right_w)

        if S[i] == "#":
            b += 1
        else:
            w += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# N個の石, 石の状態は長さNの文字列Sで表される, '.'なら白, '#'なら黒
# 黒い石のすぐ右に白い石があるような箇所がないようにした

"方針：ある場所を起点にそこから右の白を黒に(その点も含む)、そこより左を黒を全て白に"
N = int(input())
S = str(input())

black = [0]
white = [0]

for i in range(N):
    if S[i] == '#':  # 黒なら
        black.append(black[-1] + 1)
        white.append(white[-1])
    else:
        black.append(black[-1])
        white.append(white[-1]+1)

ans = N
for i in range(N+1):
    ans = min(ans, black[i] + (white[-1]-white[i]))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())

black = [0]
white = [0]

for i in range(N):
    if S[i] == '#':  # 黒のパターン
        black.append(black[-1]+1)
        white.append(white[-1])
    else:
        black.append(black[-1])
        white.append(white[-1]+1)

ans = N

for i in range(N+1):
    ans = min(ans, black[i] + (white[-1]-white[i]))
    # iを起点にして、その点を含んだ左側にある黒の数 + その点より右側にある白の数

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

W = [0]*N
B = [0]*N
white = 0
black = 0

for i in range(N):
    if S[i] == '.':
        white += 1
    if S[-i-1] == '#':
        black += 1

    W[i] = white
    B[-i-1] = black

ans = N - max(white, black)
for i in range(N-1):
    tmp = abs((i+1)-W[i]) + abs((N-i-1)-B[i+1])
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
input()
S=input()
dot=S.count(".")
ans=dot
count=0
for s in S:
    if s=="#":count+=1
    else:dot-=1
    ans=(min(ans,count+dot))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
W = [0]*(n+1)
B = [0]*(n+1)
for i in range(n):
    if S[i] == '.':
        W[i] = 1
    else:
        B[i] = 1
for i in range(1,n+1):
    W[i] += W[i-1]
    B[i] += B[i-1]
W = [0] + W
B = [0] + B
res = n
for i in range(n+1):
    res = min(W[-1]-W[i]+B[i],res)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

m = S.count('.')
mi = m
# すべて黒にするためのコスト
for s in S:
    if s == '.':
        m -= 1
    else:
        m += 1
    mi = min(mi,m)
# .....####### から
# ......###### にする

print(mi)

Submission status: Accepted

Solution code:
# TPBC201C - Stones
n = int(input())
s = input()
ans = 0
blk = 0
for i in s:
    if i == "#":
        blk += 1
    else:
        if blk > 0:
            ans += 1
            blk -= 1
print(ans)

Submission status: Accepted

Solution code:
# TPBC201C - Stones
import sys
input = sys.stdin.readline

n = int(input())
s = input().rstrip()
ans = 0
blk = 0
for i in s:
    if i == "#":
        blk += 1
    else:
        if blk > 0:
            ans += 1
            blk -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

n = int(input())
s = list(input())
res = 10 ** 9

cta = 0
ctb = 0
a = [0] * n
b = [0] * n
for i in range(n):
    if s[i] == ".":
        cta += 1
    else:
        ctb += 1
    a[i] = cta
    b[i] = ctb
for i in range(n):
    res = min(res, b[i] + a[-1] - a[i])
res = min(res, a[-1], b[-1])
print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ac = [0]

# 黒の累積和テーブルを作る
for i in range(N):
    if S[i] == "#":
        ac.append(ac[i] + 1)
    else:
        ac.append(ac[i])

#print(ac)

work_min = N
for j in range(0, N + 1):
    work_sum = ac[j] + (N - j - (ac[N] - ac[j]))
    if work_min > work_sum:
        work_min = work_sum

print(work_min)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cost = s.count('.')
min = cost
for i in range(len(s)):
  if s[i]  == '.':
  	cost -= 1
  else:
    cost += 1
  if cost < min:
    min = cost
print(min)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans1 = S.count("#")#全部..........にするために必要な変更数
ans2 = S.count(".")#全部##########にするために必要な変更数
#ここから下は、....###########にするための変更数を累積和で求める。
L =[0]*(N+1)
for i in range(N):
    if S[i] == "#":
        L[i+1] = L[i] + 1
    else:
        L[i+1] = L[i]
ans3 = N
for i in range(1,N+1):
    if ans3 > 2*L[i]+N-i-L[N]:
        ans3 =  2*L[i]+N-i-L[N]
print(min(ans1,ans2,ans3))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    s = list(input())
    black_count = [0 for _ in range(n + 1)]
    ans = float('inf')

    for i, si in enumerate(s):
        if si == '#':
            black_count[i + 1] = black_count[i] + 1
        else:
            black_count[i + 1] = black_count[i]

    for j in range(n + 1):
        ans = min(ans, black_count[j] + (n - j) - black_count[n] + black_count[j])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Stones
https://atcoder.jp/contests/tenka1-2019-beginner/tasks/tenka1_2019_c

"""
import sys


def solve(N, S):
    black = 0
    white = S.count('.')
    ans = float('inf')

    for s in S + ' ':
        ans = min(ans, black+white)
        if s == '#':
            black += 1
        else:
            white -= 1
    return ans


def __solve(N, S):
    ans1 = 0
    prev = '@'
    for s in S:
        if prev == '#' and s == '.':
            ans1 += 1
            prev = '#'
        else:
            prev = s
    return ans1


def main(args):
    N = int(input())
    S = input()
    ans = solve(N, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=[0]
b=[0]
for i in range(n):
    if s[i]==".":
        w.append(w[i]+1)
        b.append(b[i])
    else:
        w.append(w[i])
        b.append(b[i]+1)
ans=1000000
cnt=s.count(".")
for j in range(n+1):
    if ans>b[j]+cnt-w[j]:
        ans=b[j]+cnt-w[j]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
bc = 0
cost = 0

for i in s:
  if i == '#':
    bc += 1
  else:
    if bc > 0:
      bc -= 1
      cost += 1
print(cost)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())

diff = s.count('.')

difflist = [diff]

for i in s:
  if i == '.':
    diff -= 1
  else:
    diff += 1
    
  difflist.append(diff)
  
print(min(difflist))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

white = len([char for char in S if char == '.'])

left_black = 0
right_white = white
result = left_black + right_white
for char in S:
    if char == '.':
        right_white -= 1
    else:
        left_black += 1
    result = min(result, right_white + left_black)
print(result)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

white = len([char for char in S if char == '.'])

result = 0
if white == 0:
    result = 0
else:
    result = white
    left_black = 0
    right_white = white
    for char in S:
        if char == '.':
            right_white -= 1
        else:
            left_black += 1
        result = min(result, right_white + left_black)
print(result)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

count_list = []

black = 0
white = s.count('.')

count_list.append(white)

for i in range(n):
    if s[i] == '#':
        black += 1
    if s[i] == '.':
        white -= 1
    count_list.append(black + white)

print(min(count_list))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

N = int(input())
S = input()

result = S.count('.')
r = result
def dfs(i):
    global result
    global r
    if i == N:
        return float('inf')
    if S[i] == '#':
        r += 1
    else:
        r -= 1
    result = min(result,r)
    return min(result,dfs(i+1))

print(dfs(0))

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())

b = 0
w = s.count(".")
m = w

for i in range(n):
	if s[i] == "#":
		b += 1
	else:
		w -= 1
	m = min(m, b + w)
print(m)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
cntw = [0]*(N+1)
cntb = [0]*(N+1)
for i in range(N):
    cntb[i+1] = cntb[i]+1 if S[i]=="#" else cntb[i]
    cntw[N-1-i] = cntw[N-i]+1 if S[N-1-i]=="." else cntw[N-i]
ans = 1e10
for i in range(N+1):
    ans = ans if ans < cntb[i]+cntw[i] else cntb[i]+cntw[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
# at position 0
l_b = 0
r_w = S.count('.')
 
needHand = l_b + r_w
for i,v in enumerate(S):
  if S[i] == '#': #black
    l_b += 1
  else:
    r_w -= 1
  ret = l_b + r_w
  
  if ret <= needHand:
    needHand = ret
 
ans = needHand
print("{}".format(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

# at position 0
l_b = 0
r_w = S.count('.')

needHand = l_b + r_w
for i in S:
  if i == '#': #black
    l_b += 1
  else:
    r_w -= 1  
  needHand = min(needHand, l_b + r_w)

ans = needHand
print("{}".format(ans))

Submission status: Accepted

Solution code:
n=int(input())
s=input()

w=0
for i in s:
        if i==".":
                w+=1

b=0
min=10**5+1
for i in range(len(s)):
        temp=0
        if i>=1 and s[i-1]=="#":
                b+=1
        if s[i]==".":
                w-=1
        temp=w+b
        if temp<min:
                min=temp

print(min)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    left_black = 0
    right_white = S.count('.')
    min_change = left_black + right_white
    for s in S:
        if s == '#':
            left_black += 1
        else:
            right_white -= 1
        min_change = min(min_change, left_black + right_white)
    print(min_change)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()

score_list = [0 for i in range(n+2)]
score_list[0] = sum([1 if _s == "#" else 0 for _s in s])

score_list[n+1] = n - score_list[0]
 
for i in range(1, n+1):
    if s[i-1] == ".":
        score_list[i] = score_list[i-1] + 1
    else:
        score_list[i] = score_list[i-1] - 1

max_score = max(score_list)
print(n - max_score)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = [0] * N
b = [0] * N
counter = []

if S[0] == '#':
    a[0] = 1
if S[N-1] == '.':
    b[N-1] = 1

for i in range(N-1):
    if S[i+1] == '#':
        a[i+1] = a[i] + 1
    else:
        a[i+1] = a[i]
    if S[N-i-2] =='.':
        b[N-i-2] = b[N-i-1] + 1
    else:
        b[N-i-2] = b[N-i-1]
counter.append(a[N-1])
counter.append(b[0])
for i in range(N-1):
    counter.append(a[i] + b[i+1])

print(min(counter))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

# valid: ..*##*
# base: all black
dist = S.count(".")
min_dist = dist
for i in range(N):
  # .*i + #*(N-i)
  if S[i] == "#":
    dist += 1
  else:
    dist -= 1
    if dist < min_dist:
      min_dist = dist

print(min_dist)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

W = S.count('.')
B = 0
ans = W
if W == 0:
    print (0)
else:
    for i in range(0,N):
#        print (i)
        if S[i] == '#':
            B += 1
        else:
            W -= 1
        ans = min(ans, B+W)
    print (ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

black = S.count('#')
white = N - black

answer = 0
if (white > 0 and black > 0):
  answer = min(black, white)
  black_left = 0
  white_left = 0
  for i in range(N - 1):
    if (S[i] == '#'):
      black_left += 1
    else:
      white_left += 1
    black_right = black - black_left
    white_right = white - white_left
    answer = min(answer, black_left + white_right)

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
W = S.count(".")
B = 0
ans = B+W
for i in range(N):
    if S[i] == ".":
        W -= 1
    elif S[i] == "#":
        B += 1
    ans = min(ans, B+W)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
white_count = 0
black_count = 0

for c in s:
    if c == ".":
        white_count += 1

maybe_answer_list = []

maybe_answer_list.append(white_count + black_count)

for c in s:
    if c == "#":
        black_count += 1
    else:
        white_count -= 1
    maybe_answer_list.append(white_count + black_count)

print(min(maybe_answer_list))

Submission status: Accepted

Solution code:
from itertools import accumulate
N=int(input())
S=input()
black=[0]*N
white=[0]*N
for i,s in enumerate(S):
        if s=='#':
                black[i]=1
        else:
                white[i]=1
black = [0] + black
black = list(accumulate(black))
white = [0] + white[::-1]
white = list(accumulate(white))[::-1]
ans=[]
for i in range(N+1):
        ans.append(black[i]+white[i])
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

answer = ['.' for i in range(N)]
diff = 0
for i in range(N):
    if S[i] != answer[i]:
        diff += 1

counts = [diff]

for i in range(1, N+1):
    v = answer[-i] 
    if v == S[-i]:
        diff += 1
    else:
        diff -= 1
    answer[-i] = '.' if v == '#' else '#'
    counts.append(diff)

print(min(counts))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

num_of_white = 0
num_of_black = 0

for i in range(1, N):
  if S[i] == '.':
    num_of_white += 1

result = num_of_white

for i in range(1, N):
  if S[i-1] == '#':
    num_of_black += 1
  if S[i] == '.':
    num_of_white -= 1
  
  if result > (num_of_black + num_of_white):
    result = num_of_black + num_of_white

print(result)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

count = [0]*(len(S)+1)
count[0] = S.count('.')
for i in range(1, len(S)+1):
    count[i] = count[i-1] + (1 if S[i-1]=='#' else 0) - (1 if S[i-1]=='.' else 0)

print(min(count))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
black = s[:0].count("#")
white = s[0:].count(".")
count = [black+white]    
    
for i in range(1, n+1):
    if s[i-1]== "#":
        black +=1
    else:
        white -=1
    count.append(white+black)
    
print(min(count))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
s = list(input())
res = 10**9

cta = 0
ctb = 0
a = [0]*n
b = [0]*n
for i in range(n):
    if s[i] == '.':
        cta += 1
    else:
        ctb += 1
    a[i] = cta
    b[i] = ctb
for i in range(n):
    res = min(res,b[i]+a[-1]-a[i])
res = min(res,a[-1],b[-1])
print(res)

Submission status: Accepted

Solution code:
N=int(input())
S=input()


num=S.count("#")
if num>S.count("."):#全部黒にする
  num=S.count(".")
  
  
c_w=0
c_b=S.count(".")

for i in range(0,N): #i番目まで白
  if S[i]=="#":
    c_w+=1
  else:
    c_w=c_w
    c_b-=1
  num=min(num,c_w+c_b)
 
print(num)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

c_b = 0
c_w = S.count('.')
ans = c_w

for i in range(N):
    if S[i] == '#':
        c_b += 1
    else:
        c_w -= 1
    ans = min(c_b+c_w, ans)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
b,w,ans=0,0,0
w=S.count('.')
ans=w
for i in S:
    if i=='#':
        b+=1
    else:
        w-=1
    ans=min(b+w,ans)
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())

a = list(input())
su = 0
co = 1
coo = 0
c = 0
cc = 0
k = a[0]
c1 = n
c2 = 0
c3 = 0
aa = []
p = a.count("#")
pp = a.count(".")

for i in range(n-1):
	if k == a[i+1]:
		co += 1
	else:
		aa.append([k,co])
		k = a[i+1]
		co = 1

aa.append([k,co])
co = 0
if aa[-1][0] == ".":
	aa.append(["#",0])
for i in range(len(aa)):
	if aa[-(i+1)][0] == "#":
		c += aa[-(i+1)][1]
		c3 += aa[-(i+1)][1]
		c1 = min(c1,cc + p - c)
	else:
		cc += aa[-(i+1)][1]
		c2 += aa[-(i+1)][1]
		c1 = min(c1,cc + p - c)

print(min(c1,c2,c3))