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:
N=int(input())
S = input()
white = S.count(".")
black = 0
ans = white + black
for i in range (N):
    if S[i] == "." :
        white -= 1
    else:
        black += 1
    ans = min(ans,white+black)
print(ans)

Submission status: Accepted

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

ans = S.count('.')
min_ = ans

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

print(min_)

Submission status: Accepted

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

curr = 0
nblack = 0

for ch in s:
  if ch == '.':
    curr = min(nblack, 1 + curr)
  else:
    nblack += 1

print(curr)

Submission status: Accepted

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

cb = S.count("#")
cw = S.count(".")

if cb == 0:
	ans = 0
else:
	x = 0
	y = cw
	ans = float("inf")
	for i in range(N):
		if S[i] == "#":
			v = x + y
			if ans > v:
				ans = v
			x += 1
		else:
			y -= 1
			v = x + y
			if ans > v:
				ans = v


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
stones = input()
white = 0
black = 0
for i in range(N):
    if stones[i] == '.':
        white += 1
ans = white
for i in range(N):
    if stones[i] == '#':
        black += 1
    else:
        white -= 1
    if ans > black + white:
        ans = black + white
print(ans)

Submission status: Accepted

Solution code:
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()
N,S = ni(),ns()
#print(S)
#累積和を計算
b_ = [0]*(N+1)
w_ = [0]*(N+1)
if(S[0]=="."):
    b_[0]=0
    w_[0]=1
else:
    b_[0]=1
    w_[0]=0
for i in range(1,N):
    if(S[i]=="."):
        b_[i] = b_[i-1]
        w_[i] = w_[i-1]+1
    else:
        b_[i] = b_[i-1]+1
        w_[i] = w_[i-1]
ans = w_[N-1]
for i in range(N):
    ans = min(ans,b_[i]+w_[N-1]-w_[i])
    #print(ans)
#print(b_,w_)
ans = min(ans, b_[N-1])
print(ans)

Submission status: Accepted

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

n = int(input())
s = input()

acc = [0]*(n+1)
for i in range(n):
    acc[i+1] = acc[i]
    if s[i]==".":
        acc[i+1] += 1

res = 10**6
for i in range(n+1):
    r = acc[n] - acc[i]
    r += i - acc[i]
    res = min(res, r)

print(res)

Submission status: Accepted

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

ans = [S.count('.')]
for i in range(N):
    temp = ans[-1]
    if S[i] == '#':
        temp += 1
    else:
        temp -= 1
    ans.append(temp)

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

sol = 0
num_hash = 0
num_dot = 0

for si in s:
    if si == ".":
        num_dot += 1
    else:
        num_hash += 1
    sol = min(sol, num_hash - num_dot)

sol += num_dot
print(sol)

Submission status: Accepted

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

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

for c in s:
    if c == ".":
        w -= 1
    else:
        b += 1
    k = min(k, w + b)

print(k)

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools


# ひとつ入力
n = int(input())

# リストに入れる
h = list(input()) 

lb = [0]*len(h)
rw = [0]*len(h)
b=0
w=0
for i in range(len(h)):
    if h[i]=="#":
        b += 1
    lb[i] = b

for i in range(len(h)-1,-1,-1):
    if h[i]==".":
        w += 1
    rw[i] = w
answer = rw[0]
for i in range(len(h)-1):
    answer = min(answer,lb[i]+rw[i+1])
answer = min(answer,lb[len(h)-1])
#print(lb,rw)
print(answer)

Submission status: Accepted

Solution code:
def solve2(N, S):
    W = [0]*N
    W[0] = 1 if S[0] == '.' else 0
    for i in range(1, N):
        W[i] = W[i-1] + (1 if S[i] == '.' else 0)
    ans = min(W[-1], N-W[-1])
    temp = []
    for i in range(N-1):
        #print('left={}, right={}'.format(W[i], W[-1]-W[i]))
        left = i+1-W[i]
        right = W[-1]-W[i+1]
        #print(left, right)
        ans = min(ans, left+right)
        temp.append(left+right)
    return ans


# ... i / i+1 ####
# ####
N = int(input())
S = str(input())
print(solve2(N, S))

Submission status: Accepted

Solution code:
def solve(N, S):
    ans = [S.count('.')]
    for i in range(N):
        temp = ans[-1]
        temp += -1 if S[i] == '.' else 1
        ans.append(temp)
    return ans


# ... i / i+1 ####
# ####
N = int(input())
S = str(input())
print(min(solve(N, S)))

Submission status: Accepted

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

cand = []
black = 0
white = 0

for s in S:
    if s == '.':
        white +=1
cand.append(white)
        
for s in S:
    if s == '#':
        black += 1
    else:
        white -= 1
    cand.append(black+white)

print(min(cand))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#D問題
N=int(input())
S=str(input())
Slist=list(S)
BSUM=Slist.count("#")
WSUM=Slist.count(".")
Lb=[]
Rw=[]
for i in range(N+1):
    if i == 0:
        Lb.append(0)
        Rw.append(WSUM)
    else:
        if Slist[i-1] == "#":
            Lb.append(Lb[i-1]+1)
            Rw.append(Rw[i-1])
        else:
            Lb.append(Lb[i-1])
            Rw.append(Rw[i-1]-1)
MIN=2000001
for i in range(N+1):
    kari=Lb[i]+Rw[i]
    MIN=min(MIN,kari)
print(MIN)

Submission status: Accepted

Solution code:
import sys

N, S = int(sys.stdin.readline()), sys.stdin.readline()

b, w = 0, S.count('.')

ans = b + w
for si in S:
    if si == "#":
        b += 1
    if si == ".":
        w -= 1
    ans = min(ans, b + w)

print(ans)

Submission status: Accepted

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

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

b, w = 0, S.count('.')

ans = b + w
for si in S:
    if si == "#":
        b += 1
    if si == ".":
        w -= 1
    ans = min(ans, b + w)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def seek(n, s):
    s = s.rstrip('#').lstrip('.')
    turn_to_black = s.count('.')
    current_min = turn_to_black
    turn_to_white = 0

    if current_min == 0 or current_min == len(s):
        return 0

    # seek minimum turns to create "^\.*#*$"
    for i in range(len(s)):
        if s[i] == '#':
            turn_to_white += 1
        else:
            turn_to_black -= 1
        current_min = min(current_min, turn_to_black + turn_to_white)
    return current_min


def input_from_console():
    n = int(input())
    s = input()
    return n, s


def main():
    n, s = input_from_console()
    print(seek(n, s))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#Left = white ".", Right = Black "#"
# ....####
n = int(input())
s = list(input())
ans = 10**9 + 7
black = s.count("#")
white = s.count(".")
tmp = []
#tmp[0] = "#", tmp[1] = "."
if s[0] == "#":
    tmp.append([1,0])
else:
    tmp.append([0,1])

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

for i in range(n):
    left = tmp[i][0]
    right = white - tmp[i][1]
    ans = min(ans, left + right)

ans = min(ans, black)
ans = min(ans, white)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lines = list(input())
b_lines= [0 for i in range(len(lines)+1)]
w_lines=[0 for i in range(len(lines)+1)]

for i in range(len(lines)):
    if lines[i] == '#':
        b_lines[i+1]=b_lines[i]+1
    else:
        b_lines[i+1]=b_lines[i]

for i in reversed(range(len(lines))):
    if lines[i] == '.':
        w_lines[i]=w_lines[i+1]+1
    else:
        w_lines[i]=w_lines[i+1]

ans = list()
for i in range(n+1):
    ans.append(b_lines[i]+w_lines[i])
print(min(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
cumsum_b = [0] * (N+1)
 
for i in range(N):#累積和
  if S[i] == "#":
    cumsum_b[i+1] = cumsum_b[i] + 1
  else:
    cumsum_b[i+1] = cumsum_b[i]

ans = N
tmp = 0
for i in range(N+1):#境界を全探索
  tmp = cumsum_b[i] + ((N-i) - (cumsum_b[-1] - cumsum_b[i]))
  ans = min(ans,tmp)
else:
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
S = input()

W = S.count('.')
cur = W
ans = cur
for i in range(N):
    if S[i] == '.':
        cur -= 1
        if cur < ans:
            ans = cur
    else:
        cur += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = s.count(".")
now = ans
for i in s:
  if i == ".":
    now -= 1
  else:
    now += 1
  ans = min(now,ans)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate, chain

# 入力
N = int(input())
S = input()

# 黒色の石の個数に関する累積和を求めておく
dp = list(
    accumulate(
        chain(
            [0],
            map(lambda s: s == '#', S)
        )
    )
)

# 各iについて、左i個を白色、右N-i個を黒色にする際、色を変更する必要のある石の個数を求め、
# 最小値を解とする。
ans = min(
    dp[i] + (N - i - (dp[N] - dp[i]))
    for i in range(N + 1)
)
print(ans)

Submission status: Accepted

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

allblack=0
for i in range(N):
  if S[i]!='#':
    allblack+=1
    
answer=allblack
whitecount=allblack   
blackcount=0

for i in range(N):
  if S[i]=='.':
    whitecount-=1
  else:
    blackcount+=1
  tmp=blackcount+whitecount
  answer=min(answer,tmp)
  
print(answer)

Submission status: Accepted

Solution code:
N=int(input())

S=input()

A = [0]
a=0
for i in range(N):
    if S[i]=="#":
        a+=1
    A.append(a)
min_score=N

for i in range(N+1):
    min_score=min((N-i-(A[N]-A[i]))+A[i],min_score)
print(min_score)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 21 13:05:54 2019

@author: Yamazaki Kenichi
"""

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

a,ans = [],0
for c in S:
    if c == '.':
        ans += 1
        a.append(-1)
    else:
        a.append(1)
tmp = ans
for i in range(N):
    tmp += a[i]
    ans = min(ans,tmp)
#    print(ans,a[i],tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
count = S[0:N].count('.')
min = count
for i in range(0, N):
    if S[i:i + 1] == '#':
        count += 1
    else:
        count -= 1
    if count < min:
        min = count
if N == 1:
    min = 0
print("{}".format(min))

Submission status: Accepted

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

dp = [[0 for i in range(2)] for j in range(N+1)]

for i in range(1,N+1) :
    for j in range(2) :
        dp[i][j] = float("inf")

for i in range(N) :
    dp[i+1][0] = dp[i][0] + (S[i] == "#")
    dp[i+1][1] = min(dp[i][0], dp[i][1]) + (S[i] == ".")

ans = min(dp[N][0], dp[N][1])

print(ans)

Submission status: Accepted

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

w = [1 if s[i] == '.' else 0 for i in range(n)]
b = [1 if s[i] == '#' else 0 for i in range(n)]
for i in range(n - 1):
    w[i + 1] += w[i]
    b[i + 1] += b[i]

ans = float('inf')
for i in range(n):
    ans = min(ans, b[i] + w[n - 1] - w[i])
ans = min(ans, b[n - 1], w[n - 1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
N = int(input())
S = input()

if S.count('#') == N or S.count('.') == N:
    print(0)
else:
    l = 0
    r = S.count('.')
    min = l+r
    for n in range(1,N+1):
        if S[n-1] == '#':
            l += 1
        else:
            r -= 1
        if l+r < min:
            min = l+r
    print(min)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = S.count('.')
ans = a

for i in S:
    if i == '.':
        a -= 1
    else:
        a += 1
    ans = min(ans, a)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

s = input()

white = list([0]*n)
black = list([0]*n)

for i in range(n):
    if s[i] == ".":
        white[i] = (white[i - 1] + 1)
        black[i] = (black[i - 1])
    else:
        white[i] = (white[i - 1])
        black[i] = (black[i - 1] + 1)

res = white[-1]

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a,b = S.count('.'),0
ans = a+b

for i in S:
    if i =='.': a -= 1
    else: b += 1
    ans = min(ans,a + b)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S =(list(input()))
newS=[]
for i in S:
    if i == ".":
        newS.append(0)
    else:
        newS.append(1)

countW=newS.count(0)
countB=newS.count(1)
ans=min(countW,countB)
countB=0
for s in newS:
    if s ==1:
        
        countB+=1
    else:
      countW-=1
      
        
    ans=min(ans,countB+countW)
    #print(countB,countW)
#print(ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
B = [0] * (N + 1)
W = [0] * (N + 1)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


fin = sys.stdin.readline
N = int(fin())
S = fin().rstrip("\n")
assert len(S) == N

WHITE, BLACK = 0, 1

# dp[i][c] = S[i]の色をc ("." の時0，"#"の時1とする) にする時，S[i:]が有効になる時の最小の手数
min_changes_at = [[float('inf')] * 2 for _ in range(N)]
min_changes_at[-1] = [1, 0] if S[-1] == "#" else [0, 1]

for i in reversed(range(len(S) - 1)):
    color = S[i]
    if color == "#":
        # もしS[i]を白にする時S[i + 1]は白黒どちらでもいい．S[i]を反転させるのに手数1かかる
        min_changes_at[i][WHITE] = min(min_changes_at[i + 1]) + 1
        # S[i]を黒にする時，現在の位置での反転入らないが，右隣が黒でないといけない．
        min_changes_at[i][BLACK] = min_changes_at[i + 1][BLACK]
    else:  # WHITE
        # S[i]が白ならS[i + 1]はどっちでもOK
        min_changes_at[i][WHITE] = min(min_changes_at[i + 1])
        # S[i]を黒にするなら反転で1 + 隣は白のみ
        min_changes_at[i][BLACK] = min_changes_at[i + 1][BLACK] + 1

print(min(min_changes_at[0]))

Submission status: Accepted

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

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

ans = white + black

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

print(ans)

Submission status: Accepted

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

ans = float("inf")

r_b = s.count("#")
r_w = s.count(".")

l_b,l_w = 0,0

ans = min(ans,l_b + r_w)

for i in range(n):
  if s[i] == "#":
    r_b -= 1
    l_b += 1
  else:
    r_w -= 1
    l_w += 1

  ans = min(ans,l_b + r_w)

print(ans)

Submission status: Accepted

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

a = [(0, 0) for _ in range(len(s) + 1)]

i = 0
while i < len(s):
    bs, ws = a[i + 1]
    b0, w0 = a[i]

    if s[i] == '#':
        bs += 1
    else:
        ws += 1

    a[i + 1] = (bs + b0, ws + w0)
    i += 1


i = 0
can = len(s)
while i < len(a):
    can = min(can, a[i][0] + a[-1][1] - a[i][1])
    i += 1

print(can)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
stones = np.array([0 if stone == "." else 1 for stone in input()])
# stones = np.array([0 if stone == "." else 1 for stone in ".#....."])


def count_painted_stones_by_cumsum(stones, length):
    left_black_stones = np.roll(np.cumsum(stones), 1)
    left_black_stones[0] = 0
    right_white_stones = np.roll(np.cumsum(1 - stones[::-1]), 1)
    right_white_stones[0] = 0

    return np.min(left_black_stones + right_white_stones[::-1])


print(count_painted_stones_by_cumsum(stones, len(stones)))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
blacks = [0 for i in range(N + 1)]
whites = [0 for i in range(N + 1)]
for i in range(N):
    blacks[i + 1] = blacks[i] + (S[i] == '#')
    whites[i + 1] = whites[i] + (S[i] == '.')
result = N
for i in range(N + 1):
    result = min(result, blacks[i] + (whites[N] - whites[i]))
print(result)

Submission status: Accepted

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

acc = [0]
acc2 = [0]
for s in S:
    if s == ".":
        acc.append(acc[-1] + 1)
        acc2.append(acc2[-1])
    else:
        acc.append(acc[-1])
        acc2.append(acc2[-1] + 1)
acc.pop(0)

ans = float("inf")

for i in range(N):
    s = S[i]
    tmp = acc[-1] - acc[i]
    tmp2 = acc2[i]
    ans = min(ans , tmp + tmp2)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=list(input())
W,B=[0]*(n+1),[0]*(n+1)
for i in range(n):
    if S[i]=='.':
        W[i+1]=W[i]+1
        B[i+1]=B[i]
    else:
        W[i+1]=W[i]
        B[i+1]=B[i]+1
ans=float('inf')
for i in range(n+1):
    w=W[i]
    b=B[-1]-B[i]
    tmp=n-w-b
    ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
s = [int(i=='#') for i in input()]

blacks = 0
whites = np.sum(np.array(s)==0)
ans = [blacks+whites]

for x in s:
  if x==1:
    blacks += 1
  else:
    whites -= 1

  ans.append(blacks + whites)

print(np.min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
first_black = 0
count = 0
for i in range (N):
    if first_black == 0 and S[i]=='#':
        first_black = 1
    if first_black == 1 and S[i]== '.':
        count +=1
        
first_white = 0
white_count = 0
for i in range (N):
    if first_white == 0 and S[N-i-1]=='.':
        first_white = 1
    if first_white == 1 and S[N-i-1]== '#':
        white_count +=1

white_black = 10**6

black_count_left = 0
white_count_right = S.count('.')
ans = black_count_left + white_count_right

for i in range (N):
    if S[i] == '#':
        black_count_left+=1
    elif S[i] == '.':
        white_count_right-=1
    if ans >= black_count_left+white_count_right:
        ans = black_count_left+white_count_right
        
print(min(count,white_count, ans))

Submission status: Accepted

Solution code:
N = int(input())
s = input()
white = s.count('.')
black = 0
cnt = black + white
for i in s:
    if i == '#':
        black += 1
    else:
        white -= 1
    cnt = min(cnt,black + white)
    
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = input()



c = Counter(s)
SL = 0
SR = c['#']
DL = 0
DR = c['.']
if SR == 0 or DR == 0:
    score = 0
else:

    score = len(s)

    while True:
        x = s.find('.')
        if x == -1:
            break
        SR -= x
        SL += x
        s = s[x:]

        a = DL + DR
        b = SL + SR
        c = SL + DR
        score = min([a,b,c, score])
    #     print(s, score, [DL, DR, SL, SR])


        x = s.find('#')
        if x == -1:
            break
        DR -= x
        DL += x
        s = s[x:]

        a = DL + DR
        b = SL + SR
        c = SL + DR
        score = min([a,b,c, score])
    #     print(s, score, [DL, DR, SL, SR])
print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
read = input
n = int(read())
s = read()
SW = [0 for i in range(n)]
SB = [0 for i in range(n)]
for i,c in enumerate(s):
    if i == 0:
        SW[i] = 1 if c == '.' else 0
        SB[i] = 1 if c == '#' else 0
    else :
        SW[i] = SW[i-1] + 1 if c == '.' else SW[i-1]
        SB[i] = SB[i-1] + 1 if c == '#' else SB[i-1]
cost = n
for i in range(n):
    pass
#     print(i, SW[i], SB[i])
for i in range(n + 1):
    startW = 0
    endW = i # left W is [0,i)
    startB = i 
    endB = n # right B is [i, n)
#     print(i)
    if i == 0: # all B 
        cost = min(cost, SW[n-1])
    elif i == n: # all W
        cost = min(cost, SB[n-1])
    else:
        cost = min(cost, 
                   SB[i-1] + (SW[n-1] - SW[i-1])
                  )
#     print(i, cost)
print(cost)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
import collections
c = collections.Counter(s)
ans=c["."]
t=ans
for i in s:
    if(i=="."):
        t-=1
        ans=min(t,ans)
    if(i=="#"):
        t+=1
        ans=min(t,ans)
print(ans)

Submission status: Accepted

Solution code:
# S = input()
# N = int(input())
# a = list(map(int, input().split()))
# X = [int(input()) for _ in range(N)]

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

ans1 = 0
ans2 = 0
kuro_frag = False

for i in S:
    if i == "#":
        kuro_frag = True
    elif kuro_frag:
        ans1 += 1

kuro_count = 0
siro_count = 0

for i in range(N):
    if S[i] == "#":
        kuro_count += 1
        siro_count = 0
        ans2 += 1
    else:
        kuro_count = 0
        siro_count += 1



# print(min(ans1,ans2-kuro_count))


sirokuro_list = []

if S[0] == ".":
    first_sirofrag = True
else:
    first_sirofrag = False

kuro_count = 0
siro_count = 0

for i in range(N):
    if S[i] == "#":
        kuro_count += 1
        if siro_count != 0:
            sirokuro_list.append(siro_count)
        siro_count = 0
    else:
        siro_count += 1
        if kuro_count != 0:
            sirokuro_list.append(kuro_count)
        kuro_count = 0

if siro_count != 0:
    sirokuro_list.append(siro_count)
if first_sirofrag:
    sirokuro_list.pop(0)


# print(sirokuro_list)


i = 2
if len(sirokuro_list) > 1:
    sirowa = [sirokuro_list[1]]
    kurowa = [sirokuro_list[0]]
    while True:
        if i == len(sirokuro_list):
            break

        if (i % 2) == 0:
            kurowa.append(sirokuro_list[i] + kurowa[-1])
        if (i % 2) == 1:
            sirowa.append(sirokuro_list[i] + sirowa[-1])
        i+=1

    # print(kurowa)
    # print(sirowa)

    ans3 = min(ans1, ans2)

    for i in range(0, len(kurowa)):
        ans_tmp = 0
        ans_tmp += kurowa[i]
        ans_tmp += sirowa[-1] - sirowa[i]
        # print(ans_tmp)
        if ans_tmp < ans3:
            ans3 = ans_tmp

    print(ans3)
else:
    print(0)

Submission status: Accepted

Solution code:
def main(n):
	flg = False
	s = "." + input()
	ans = 0
	tempans = 0
	blk = 0
	wht = 0
	for i in range(n+1):
		if s[i] == "#":
			blk += 1
			if s[i-1] == ".":
				flg = True
		else:
			if flg:
				wht += 1
			if blk <= wht:
				ans += blk
				blk = 0
				wht = 0
				flg = False
	print(ans + wht)
main(int(input()))

Submission status: Accepted

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

w = [0] * (n + 1)
for i in range(n - 1, -1, -1):
    w[i] = w[i + 1]
    if s[i] == '.':
        w[i] += 1

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

print(ans)

Submission status: Accepted

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

white = [0] * (N + 1)
black = [0] * (N + 1)
for i, si in enumerate(S):
    black[i + 1] = black[i]
    white[i + 1] = white[i]
    if si == "#":
        black[i + 1] += 1
    if si == ".":
        white[i + 1] += 1

mi = 10 ** 10
for i in range(N + 1):
    b = black[i]
    w = white[N] - white[i]
    mi = min(w + b, mi)
print(mi)

Submission status: Accepted

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

a = []
pc = "."
pi = 0
for i,c in enumerate(S):
    if pc!=c:
        a.append(i-pi)
        pi = i
        pc = c
else:
    a.append(i+1-pi)

l = len(a)
w = 0
b = sum(a[2::2])
ans = w+b
for i in range(1,(l+1)//2):
    #ans = min(ans,sum(a[1:1+i*2:2])+sum(a[2+i*2::2]))
    if -1+i*2 < l:
        w += a[-1+i*2]
    if i*2 < l:
        b -= a[i*2]
    ans = min(ans,w+b)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ctb=0;ctw=0;f=0;cte=0;cts=0;ct=0
lb=[];lw=[];l=[];L=[]
for j in range(n-1):
  if s[j]=='#' and s[j+1]=='.':
    f=+1
for k in range(n-1,-1,-1):
  if s[k]=='#':
    cte+=1
  else:
    break
for l in range(n):
  if s[l]=='.':
    cts+=1
  else:
    break

for i in range(cts,n-cte):
  if ctb==0 and s[i]=='.':
    ctw+=1
  if ctw==0 and s[i]=='#':
    ctb+=1
  if ctb!=0 and s[i]=='.':
    ctw+=1
  if ctw!=0 and s[i]=='#':
    lb.append(ctb)
    lw.append(ctw)
    ctb=1
    ctw=0
lb.append(ctb)
lw.append(ctw)
lb.reverse()
lw.reverse()
l=lb+lw
x=sum(l[0:len(lb)])
L.append(x)
for m in range(len(lb),len(l)):
  x+=l[m]
  x-=l[m-len(lb)]
  L.append(x)
if f==0:
  print(0)
else:
  print(min(L))

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=input()
    # if len(set(s)) == 1:
    #     print(0)
    #     return
    # s = s[s.index('#'):]
    # n = len(s)
    ans = n
    b = [0] * (n+1)
    w = [0] * (n+1)
    for i in range(n):
        b[i+1] = b[i] + (1 if s[i] == '#' else 0)
        w[i+1] = w[i] + (0 if s[i] == '#' else 1)

    for i in range(n+1):
        tmp = b[i] + (w[n] - w[i])
        ans = min([ans, tmp])
    print(ans)
main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()[::-1]

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

ans = N
for i in range(len(BCS)):
    wh_to_bk = i - BCS[i]
    bk_to_wh = BCS[-1] - BCS[i]
    ans = min(ans, wh_to_bk + bk_to_wh)

print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

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

DOT = [0] * (N+1)
SHARP = [0] * (N+1)

for i in range(0, N):
    if S[i] == "#":
        SHARP[i+1] = SHARP[i] + 1
        DOT[i+1] = DOT[i]
    else:
        SHARP[i+1] = SHARP[i]
        DOT[i+1] = DOT[i] + 1

ret = 999999999999999

for d in range(N+1):
    # 0～dの#を.にかえる
    cnt1 = SHARP[d]
    # d+1～N-1の.を#にかえる
    cnt2 = DOT[N] - DOT[d]
    cnt = cnt1 + cnt2
    # print("{} + {} : {}".format(cnt1,cnt2,cnt))
    ret = min(cnt, ret)

# print("---")
print(ret)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
c = 0
w = [0]
for i in range(N):
    if S[i] == '.':
        c += 1
    w.append(c)
print(min([(i - w[i]) + (w[-1] - w[i]) for i in range(N + 1)]))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s = input()
    dot = 0
    has = 0
    for c in s:
        if c == ".":
            dot += 1
        else:
            has += 1
    left_dots = 0
    left_has = 0
    right_dots = dot
    right_has = has
    ans = min(dot, has)
    for left in range(N):
        c = s[left]
        if c == ".":
            left_dots += 1
            right_dots -= 1
        else:
            left_has += 1
            right_has -= 1
        # print(left_has, right_dots)
        ans = min(left_has+ right_dots, ans)
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import groupby
n = int(input())
s = input()
ldx = 0
for i in range(n):
    if s[i] == "#":
        break
    else:
        ldx += 1

s = s[ldx:]
m = len(s)

if m > 1:
    rdx = m
    for i in range(m):
        if s[m-1-i] == ".":
            break
        else:
            rdx = m-1-i
    s = s[:rdx]
    m = len(s)

ans1 = s.count(".")
if m <= 1:
    print(0)
    exit()

w = []
b = []
for k, g in groupby(s):
    if k == "#":
        b.append(sum(1 for _ in g))
    else:
        w.append(sum(1 for _ in g))

S_b = [0]
S_w = [0]
m = len(b)
for i in range(m):
    S_b.append(S_b[-1] + b[i])
    S_w.append(S_w[-1] + w[i])

ans = float("inf")
for i in range(m+1):
    ans = min(ans, S_b[i] + (S_w[-1]-S_w[i]))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=[0]
b=[0]

for i in range(len(s)):
    if s[i]=='#':
        w.append(w[-1]+1)
    else:
        w.append(w[-1])
    if s[-i-1]=='.':
        b.append(b[-1]+1)
    else:
        b.append(b[-1])
#print(w)
#print(b)
total=[]
for i in range(len(s)+1):
    total.append(w[i]+b[-i-1])
#print(total)
print(min(total))