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

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

l = 0
r = n
while l < n and s[l] == ".":
    l += 1
while r > 0 and s[r-1] == "#":
    r -= 1
if l >= r:
    print(0)
    sys.exit()

A = []
a = 0
b = 0
for i in range(l, r):
    if s[i] == "#":
        if b > 0:
            A.append([a, b])
            a = 1
            b = 0
        else:
            a += 1
    else:
        b += 1
if b > 0:
    A.append([a, b])
# print(A)

DP = [0, 0]
for i in range(len(A)):
    DPN = DP[:]
    DPN[0] = min(DP) + A[i][1]
    DPN[1] = DP[1] + A[i][0]
    DP = DPN[:]
print(min(DP))

Submission status: Accepted

Solution code:
# ....#..#..##.####
# まず、左端の.と右端の#は答えに影響しないので削除する。
# ....#..#..##.####
# #..#..##.
# この結果の文字列が0であればそこで終了
# そうでない場合、
# この結果に対して、黒と白のペアを作って数を数える
# [1,2][1,2][2,1]
# すべて黒にする場合、
# 2 + 2 + 1
# となる
# 白を選ぶ場所を一つずつずらすと、
# 1 + 2 + 1
# 1 + 1 + 1
# 1 + 1 + 2
# となり、1 + 1 + 1が最小値
# これを求める

N=int(input())
S=input()
from collections import deque
q=deque(list(S))
# print(q)

while q[0]==".":
  q.popleft()
  if len(q)==0:
    print(0)
    exit(0)
  
while q[-1]=="#":
  q.pop()
  if len(q)==0:
    print(0)
    exit(0)
  
S="".join(list(q))
pairs=S.replace(".#",". #").split()
# print(pairs)
datas=[[]]*len(pairs)
blackchange=0
for i in range(len(pairs)):
  white=pairs[i].count(".")
  datas[i] = [len(pairs[i])-white,white]
  blackchange+=white

# print(datas)
ans=blackchange
# print(ans)
for i in range(len(datas)):
  blackchange-=(datas[i][1]-datas[i][0])
  if ans > blackchange:
    ans = blackchange
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
b=[None]*(N+1)
b[0]=0
w=[None]*(N+1)
w[N]=0
for i in range(1,N+1):
    b[i]=b[i-1]+(S[i-1]=='#')
for i in range(N-1,-1,-1):
    w[i]=w[i+1]+(S[i]=='.')
print(min([x+y for x,y in zip(b,w)]))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
shiro_sum = [0] * (N+1)
kuro_sum = [0] * (N+1)
for i in range(N):
    if S[i] == '.':
        shiro_sum[i+1] = shiro_sum[i] + 1
    else:
        shiro_sum[i+1] = shiro_sum[i] + 0

    if S[i] == '#':
        kuro_sum[i+1] = kuro_sum[i] + 1
    else:
        kuro_sum[i+1] = kuro_sum[i] + 0

res = 10**9
# i == 0
for i in range(N+1):
    tmp = 0
    # leftを全て白に
    # 左側にある黒の数を数える。
    tmp += kuro_sum[i]

    # rightを全て黒に
    # 右側にある白を数えて、全体の数から白の合計を消す
    tmp += shiro_sum[N] - shiro_sum[i]
    res = min(res, tmp)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
shiro_sum = [0] * (N+1)
kuro_sum = [0] * (N+1)
for i in range(N):
    if S[i] == '.':
        shiro_sum[i+1] = shiro_sum[i] + 1
    else:
        shiro_sum[i+1] = shiro_sum[i] + 0

    if S[i] == '#':
        kuro_sum[i+1] = kuro_sum[i] + 1
    else:
        kuro_sum[i+1] = kuro_sum[i] + 0

res = 10**9
# i == 0
for i in range(N+1):
    tmp = 0
    # leftを全て白に
    # 左側にある黒の数を数える。
    tmp += kuro_sum[i] - kuro_sum[0]

    # rightを全て黒に
    # 右側にある白を数えて、全体の数から白の合計を消す
    tmp += shiro_sum[N] - shiro_sum[i]
    res = min(res, tmp)
print(res)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
s = list(input())
for i in range(n):
    if s[i] == "#":
        s[i] = 1
    else:
        s[i] = 0
cs = [0] + list(accumulate(s))
m = n
for i in range(n+1):
    num = cs[i] + (n-i)-(cs[n]-cs[i])
    m = min(m,num)
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
S = input()[:-1]
acc = [0]

for Si in S:
    if Si=='#':
        acc.append(acc[-1]+1)
    else:
        acc.append(acc[-1])

ans = 10**18

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

print(ans)

Submission status: Accepted

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

white = [0] * (N + 1)
for i in range(N):
    if S[i] == ".":
        white[i + 1] = white[i] + 1
    else:
        white[i + 1] = white[i]

ans = N
for i in range(N + 1):
    tmp = (i - white[i]) + (white[-1] - white[i])
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
rw = s.count(".")
rb = n - rw
ans = rw
lw = 0
lb = 0
for i in s:
    if i == ".":
        lw += 1
        rw -= 1
    else:
        lb += 1
        rb -= 1
    ans = min(ans, lb + rw)
print(ans)

Submission status: Accepted

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

    s_b = [1 if c=='#' else 0 for c in s]
    s_w = [1 if c=='.' else 0 for c in s[::-1]]

    s_b = cumsum(s_b)
    s_w = cumsum(s_w)[::-1]

    ans = []
    for i in range(n):
        ans.append(s_b[i]+s_w[i+1])

    print(min(ans))

def cumsum(a):
    s = 0
    sums = [0]
    for a_i in a:
        s+=a_i
        sums.append(s)

    return sums


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
S=list(input())
W=[0]*(n+1)
B=[0]*(n+1)
for i in range(n):
  if S[i]=="#":
    B[i+1]=B[i]+1
  else:
    B[i+1]=B[i]
for i in range(n-1,-1,-1):
  if S[i]==".":
    W[i]=W[i+1]+1
  else:
    W[i]=W[i+1]
ans=10**10
for i in range(n+1):
  a=W[i]+B[i]
  if a<ans:
    ans=a
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
flag="-1"
l=[]
b=0
w=0
for i in range(N):
    if S[i]=="#":
        if flag=="w":
            l.append([b,w])
            b=1
            w=0
            flag="b"
        else:
            flag="b"
            b+=1
    else:
        if flag=="b":
            w+=1
            flag="w"
        elif flag=="-1":
            continue
        else:
            flag="w"
            w+=1
if flag=="w":
    l.append([b,w])

aw=0
ab=0
for i in range(len(l)):
    aw+=l[i][0]
    ab+=l[i][1]
ans=[]
ans.append(aw)
ans.append(ab)
tmpw=0
tmpb=0
val=0
for i in range(len(l)):
    tmpw+=l[i][1]
    tmpb+=l[i][0]
    val=(ab-tmpw)+tmpb
    ans.append(val)
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
t_min = min(S.count("#"), S.count("."))
S_first = 0
S_second = S.count(".")
for i in range(0,N):
    if S[i] == "#":
        S_first += 1
    else:
        S_second -= 1
    t = S_first + S_second
    t_min = min(t_min, t)
print(t_min)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = input()
INF = 10**9
white_cnt = s.count('.')
black_cnt = 0
ans = INF
for c in s:
    ans = min(ans, black_cnt + white_cnt)
    if c == '#':
        black_cnt += 1
    else:
        white_cnt -= 1
else:
    ans = min(ans, black_cnt + white_cnt)

print(ans)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = [i for i in input()]
cnt_s, cnt_d = [0]*(n+1), [0]*(n+1)
for i in range(n):
    if s[i] == "#":
        cnt_s[i+1] = 1
    else:
        cnt_d[i+1] = 1
cnt_s, cnt_d = list(itertools.accumulate(cnt_s)), list(itertools.accumulate(cnt_d))
ans = 10**6
cnt_d_l = cnt_d[-1]
for i in range(n+1):
    ans = min(ans, cnt_s[i] - cnt_s[0] + (cnt_d_l - cnt_d[i]))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=str(input())
#c=c[::-1]
#print(c)
black=c.count('#')
white=n-black
left=0#左は黒を消す
right=white#右はしろを消す
#print(white,black)
ans=right
for i in range(n):
    if c[i]=='#':
        left+=1
    else:
        right-=1
    
    ans=min(ans,left+right)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
b_c,w_c = s.count('#'),s.count('.')
ans = w_c
b_r,w_r = 0,0
for i in range(n-1):
    if s[i]=='#':b_r+=1
    else:w_r+=1
    if s[i]!=s[i+1]:
        ans = min(ans,b_r+w_c-w_r)
if s[-1]=='#':b_r+=1
else:w_r+=1
ans = min(ans,b_r+w_c-w_r)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    a = [0]*n
    b = [0]*n
    for i in range(n):
        if s[i] == "#":
            a[i] += 1
        if 0 < i:
            a[i] += a[i-1]
    for i in reversed(range(n)):
        if s[i] == ".":
            b[i] += 1
        if i < n-1:
            b[i] += b[i+1]
    ans = float("inf")
    for i in range(1, n):
        ans = min(ans, a[i-1]+b[i])
    ans = min(ans, b[0], a[n-1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

sharp_list = [0 for i in range(n)]
dot_list = [0 for i in range(n)]
for i in range(n):
    if s[i] == '#':
        sharp_list[i] = sharp_list[i-1] + 1
    else:
        sharp_list[i] = sharp_list[i-1]
sharp_list = [0] + sharp_list
for i in range(n-1,-1,-1):
    if s[i] == '.':
        dot_list[i] = dot_list[(i+1)%n] + 1
    else:
        dot_list[i] = dot_list[(i+1)%n]
dot_list += [0]
ans = 10**9
for i in range(n+1):
    ans = min(sharp_list[i]+dot_list[i],ans)
print(ans)

Submission status: Accepted

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

ldp = [0] * (N+1)
rdp = [0] * (N+1)

for i in range(N):
    if S[i] == "#":
        ldp[i+1] = ldp[i] + 1
    else:
        ldp[i+1] = ldp[i]

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

#print(ldp)
#print(rdp)
ans = 10 ** 9
for i in range(N+1):
    ans = min(ans, rdp[i]+ldp[i])

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l=[]
k=len(s.replace(".",""))
ans=n-k
#自分含め左にいくつ黒があるか
cnt=0
#左の黒、右の白
for i in range(n):
  cnt+=int(s[i]=="#")
  ans=min(ans,2*cnt+n-i-1-k)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(str,input()))
left = 0
right = 0
for i in range(n):
    if s[i]==".":
        right += 1
ans = right
if s[0]==".":
    right -= 1
for j in range(1,n):
    if s[j-1]=="#":
        left += 1
    if s[j]==".":
        right -= 1
    ans = min([ans,left+right])
if s[n-1]=="#":
    left += 1
ans = min([ans,left])
print(ans)

Submission status: Accepted

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

w = S.count('.')
b = N - w
ans = min(w, b)
l, r = 0, w

for si in S:
    if si == '#':
        l += 1  # 現時点iも含め左側に存在する黒の数
    else:
        r -= 1  # 現時点iも含め右側に存在する白の数
    ans = min(ans, l+r)

print(ans)

Submission status: Accepted

Solution code:
from decimal import *
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    s = S()
    N = len(s)
    white = [0 if s[i] == '.' else 1 for i in range(N)]
    count = sum(white)
    lst = set()
    lst.add(count)

    for i in range(N-1, -1, -1):
        if white[i]:
            count -= 1
        else:
            count += 1
        lst.add(count)
    print(min(lst))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

count_white = [0]*N

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

white_total = count_white[-1]
ans = 10**9
for i in range(N):
    num_black_left = i+1 - count_white[i]
    num_white_right = white_total - count_white[i]

    ans_temp = num_black_left + num_white_right

#    print i, ans_temp                                                                                                                                    

    ans = min(ans, ans_temp)

print min(ans, white_total)

Submission status: Accepted

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

s_list = []

for item in s:
    if item == '.':
        s_list.append(0)
    else:
        s_list.append(1)

min_val = s_list.count(0)

left_val = 0
right_val = s_list.count(0)

for i in range(len(s_list)):
    if s_list[i] == 0:
        right_val -= 1
        min_val = min(left_val + right_val, min_val)
    else:
        left_val += 1

print(min_val)

Submission status: Accepted

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

l = [[0]*2 for _ in range(N+1)]
r = [[0]*2 for _ in range(N+1)]

for i,j in enumerate(s, start=1):
    l[i][0] = l[i-1][0]
    l[i][1] = l[i-1][1]
    if j=="#":
        l[i][0] += 1
    else:
        l[i][1] += 1

for i,j in enumerate(s[::-1], start=1):
    r[N-i][0] = r[N-i+1][0]
    r[N-i][1] = r[N-i+1][1]
    if j=="#":
        r[N-i][0] += 1
    else:
        r[N-i][1] += 1

ans = N
for i in range(N+1):
    ans = min(ans, l[i][0]+r[i][1])

print(ans)

Submission status: Accepted

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

l, r = [[0]*2 for _ in range(N+1)], [[0]*2 for _ in range(N+1)]
for i,j in enumerate(s, start=1):
    if j=="#":
        l[i][1] = l[i-1][1]
        l[i][0] = l[i-1][0] + 1
    else:
        l[i][0] = l[i-1][0]
        l[i][1] = l[i-1][1] + 1

for i,j in enumerate(s[::-1], start=1):
    if j=="#":
        r[N-i][1] = r[N-i+1][1]
        r[N-i][0] = r[N-i+1][0] + 1
    else:
        r[N-i][0] = r[N-i+1][0]
        r[N-i][1] = r[N-i+1][1] + 1

ans = N
for i in range(N+1):
    ans = min(ans, l[i][0]+r[i][1])

print(ans)

Submission status: Accepted

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

l, r = [[0]*2 for _ in range(N+1)], [[0]*2 for _ in range(N+1)]
for i,j in enumerate(s, start=1):
    if j=="#":
        l[i][1] = l[i-1][1]
        l[i][0] = l[i-1][0] + 1
    else:
        l[i][0] = l[i-1][0]
        l[i][1] = l[i-1][1] + 1

for i,j in enumerate(s[::-1], start=1):
    if j=="#":
        r[N-i][1] = r[N-i+1][1]
        r[N-i][0] = r[N-i+1][0] + 1
    else:
        r[N-i][0] = r[N-i+1][0]
        r[N-i][1] = r[N-i+1][1] + 1

# black white
# .....#####
# 1111100000

ans = N
for i in range(N+1):
    ans = min(ans, l[i][0]+r[i][1])

print(ans)

Submission status: Accepted

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

w = s.count('.')
b = n - w
ans = min(w, b)
r = 0
l = w
for si in s:
    if si == '#':
        r += 1
    else:
        l -= 1
    if ans > r + l:
        ans = r + l
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
lS = len(S)
l = 0
bw = [0, 0]
for i in range(lS):
    if (S[i] == "."):
        bw[1] += 1

ans = sum(bw)
for i in range(lS):
    if (S[i] == "#"):
        bw[0] += 1
    if (S[i] == "."):
        bw[1] -= 1

    if (ans > sum(bw)):
        ans = sum(bw)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
White_rui = [0]
Black_rui = [0]

white = 0
black = 0
for i in range(n):
    if s[i] == '.':
        white += 1
    else:
        black += 1
    White_rui.append(white)
    Black_rui.append(black)

ans = 10**6
for i in range(n):
    white_to_black = White_rui[-1] - White_rui[i+1]
    black_to_white = Black_rui[i] - Black_rui[0]
    ans = min(ans, white_to_black+black_to_white)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
data = [0] * (N + 2)
cnt = 0
for i in range(N):
    if S[i] == "#":
        cnt += 1
    data[i + 1] = cnt
data[-1] = cnt
ans = float("inf")
for i in range(N + 1):
    turn = data[i] + N - i - data[-1] + data[i]
    if ans > turn:
        ans = turn
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
leftblack=0
rightwhite=0
for i in range(0,N):
    if S[i]==".":
        rightwhite+=1
ans=leftblack+rightwhite
for i in range(0,N):
    if S[i]=='.':
        rightwhite-=1
    else:
        leftblack+=1
    test=leftblack+rightwhite
    if ans>test:
        ans=test

print(ans)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')

N = I()
S = input()

blacks = S.count('#')
if blacks == 0:
    print(0)
    exit(0)

ans = blacks

# w[i] := i番目以降にいくつの白があるか
# b[i] := i番目より前にいくつの黒があるか
w = [0] * N
b = [0] * N
for i, c in enumerate(S):
    if i == 0:
        continue
    if S[i - 1] == '#':
        b[i] += 1
    if i < N - 1:
        b[i + 1] = b[i]
for i, c in enumerate(reversed(S)):
    i = N - i - 1
    if i == N - 1:
        if c == '.':
            w[i] = 1
        continue
    w[i] = w[i + 1]
    if c == '.':
        w[i] += 1


for i, c in enumerate(S):
    if c == '#':
        ans = min(ans, w[i] + b[i])

print(ans)

Submission status: Accepted

Solution code:
def main():
    length = int(input())
    stones = list(input())
    white = [0] * length
    if stones[0] == ".":
        white[0] = 1
    for i in range(1, length):
        if stones[i] == ".":
            white[i] = white[i - 1] + 1
        else:
            white[i] = white[i - 1]
    answer = min(white[-1], length - white[-1])
    for i in range(length):
        answer = min(answer, i + 1 + white[-1] - 2 * white[i])
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
b = [0] * n
w = [0] * n
x,y = 0,0
ans = 0
for i in range(n):
    if s[i] == '#':
        x += 1
    else:
        y += 1
    b[i] = x
    w[i] = y

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

Submission status: Accepted

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

wn = s.count('.')
bn = n - wn
ans = min(wn,bn)

lw = 0
lb = 0
for i in range(n):
  if s[i]=='.':
    lw += 1
  else:
    lb += 1      
  ans = min(ans, lb + (wn - lw))
  
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = 1<<48

def solve(N: int, S: str):
    b = [1 if S[i] == '#' else 0 for i in range(N)]
    w = [1 if S[i] == '.' else 0 for i in range(N)]

    rb = [0] * (N+1)
    for i in range(N):
        rb[i+1] = rb[i] + b[i]

    rw = [0] * (N+1)
    for i in range(N-1, -1, -1):
        rw[i] = rw[i+1] + w[i]

    ans = INF
    for i in range(N+1):
        ans = min(ans, rb[i]+rw[i])
    
    print(ans)

    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 最終的に.......#####の並びになると想定
# それぞれの累積和の足し算が最小になる境目を求める
N = int(input())
S = list(input())
b = [0] * N
w = [0] * N
x, y = 0, 0
ans = 0
# 累積和
for i in range(N):
    if S[i] == '#':
        x += 1
    else:
        y += 1
    b[i] = x
    w[i] = y

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
lb = 0
rw = sum([s == "." for s in S])
ans = rw

for s in S:
  if s == ".":
    rw -= 1
  else:
    lb += 1
  ans = min(ans, rw+lb)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=input()
    
    ans=s.count('.')
    t=ans
    for i in range(n):
        if s[i]=='.':
            t-=1
        else:
            t+=1           
        ans=min(ans,t)
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Accepted

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

def main():
    N = int(input())
    s = input()
    ans = s.count(".")
    count = ans
    for st in s:
        if st == "#":
            count += 1
        else:
            count -= 1
        ans = min(ans,count)
        
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = [0 if s == "#" else 1 for s in list(input())]
ruisekiwa = [0]*(N+2)
i = 1
r = 0
while i <= N:
  r = r+S[i-1]
  ruisekiwa[i] = r
  i += 1
ruisekiwa[N+1] = ruisekiwa[N]
ans = 10**10
i = 1
while i <= N+1:
  towhite = (i-1)-(ruisekiwa[i-1]-ruisekiwa[0])
  toblack = ruisekiwa[N+1]-ruisekiwa[i-1]
  ans = min(ans, towhite+toblack)
  i += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
if S=='#'*N or S=='.'*N:
  print(0)
  exit()
rightB=0
for i in S:
  if i=='#':
    rightB+=1
rightW=N-rightB
leftB=0
leftW=0
ans=min(rightB,rightW)
for i in range(N):
  if S[i]=='.':
    leftW+=1
    rightW-=1
    d=rightW+leftB
    ans=min(ans,d)
  else:
    leftB+=1
    rightB-=1
    d=rightW+leftB
    ans=min(ans,d)
print(ans)

Submission status: Accepted

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

w = S.count('.')
b = 0
ans = w+b

for i in range(n):
    if S[i] == '.':
        w -= 1
    else:
        b += 1
    ans = min(ans, w+b)

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

count = []
for i in range(N):
    count.append([0,0])

cb = 0
for i in range(N):
    count[i][0] = cb
    if S[i] == "#":
        cb += 1
cw = 0
for i in range(N):
    count[N-i-1][1] = cw
    if S[N-i-1] == ".":
        cw += 1

ans = 10**10
for i in range(N):
    ans = min(ans,sum(count[i]))
print(ans)

Submission status: Accepted

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

b = [0 for k in range(n+1)]
c = 0
for k in range(n):
    if s[k] == "#":
        c += 1
    b[k+1] = c

w = [0 for k in range(n+1)]
c = 0
for k in range(n):
    if s[-k-1] == ".":
        c += 1
    w[-k-2] = c

ans = 10**9
for k in range(n+1):
    ans = min(ans,b[k]+w[k])

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = input()
white = sum([1 for i in s if i == "."])
ans = white#全部黒にするとき
l = 0 # i までのwhiteの数
for i in range(n):
    if s[i] == ".": l += 1
    #print(ans,i+white-2*l)
    ans = min(ans,i+1+white-2*l)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(best_cost)

Submission status: Accepted

Solution code:
import numpy as np
def f(lst):
    return np.cumsum(lst)

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

res1 = s.count('.')
res2 = s.count('#')

a = [0]*n
b = [0]*n

for i in range(n):
    if s[i]=='.':
        a[i] += 1
    else:
        b[i] += 1
cum_a = f(a).tolist()
cum_b = f(b).tolist()
res3 = np.inf
for i in range(n):
    if a[i]==1:
        res3 = min(res3, cum_b[i]+(res1-cum_a[i]))

print(min(res1, res2, res3))

Submission status: Accepted

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

Submission status: Accepted

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

white_count = [0]*(N+1)
black_count = [0]*(N+1)

for i in range(N):
  black_count[i+1] = black_count[i]
  if S[i] == "#":
    black_count[i+1] += 1
  
S = S[::-1]
for i in range(N):
  white_count[i+1] = white_count[i]
  if S[i] == ".":
    white_count[i+1] += 1

    
answer = 10**6
for i in range(N+1):
  answer = min(answer,black_count[i]+white_count[N-i])
  
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
if len(set(S)) == 1:
    print(0)
    exit()

sum_blacks = [0] * N
if S[0] == "#":
    sum_blacks[0] += 1
for i in range(1, N):
    if S[i] == "#":
        sum_blacks[i] = sum_blacks[i - 1] + 1
    else:
        sum_blacks[i] = sum_blacks[i - 1]
ans = float('inf')
ans = min(ans, sum_blacks[-1])
ans = min(ans, N - sum_blacks[-1])
for i in range(N):
    l_blacks = sum_blacks[i]
    l_whites = i + 1 - l_blacks
    r_blacks = sum_blacks[-1] - l_blacks
    r_whites = N - l_blacks - l_whites - r_blacks
    ans = min(ans, l_blacks + r_whites)
print(ans)

Submission status: Accepted

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

w_num=0
b_num=0
w_l=[0]
b_l=[0]

for i in s:
 if i=='.':
  w_num+=1
 else:
  b_num+=1
 w_l.append(w_num)
 b_l.append(b_num)

ans=10**5
for i in range(n+1):
 ans=min(ans,b_l[i]+(w_num-w_l[i]))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a,b=0,0 # a:'.'の個数 b:'#'の個数
x=0 # x:'.'の個数
for i in s:
    if i==".":
        x+=1
ans=x
for i in s:
    if i==".":
        a+=1
    else:
        b+=1
    ans=min(ans,b+x-a)
print(ans)

Submission status: Accepted

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

white = s.count('.')
black = n - white
w = 0
b = 0
y = [white]
for i in range(n):
    if s[i] == '#':
        b += 1
    else: w += 1
    y.append(b+white-w)
    if w == white:
        break
print(min(y))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans=0
for stone in s:
    if stone==".":
        ans+=1
ans_mn=ans
for i in range(len(s)):
    if s[i]=="#":
        ans+=1
    if s[i]==".":
        ans-=1
    if ans < ans_mn:
        ans_mn=ans
print(ans_mn)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
b_lst=[]
w_lst=[]
b_cnt=0
w_cnt=0
for i in range(len(s)):
    b_lst.append(b_cnt)
    w_lst.append(w_cnt)
    if s[i]=="#":
        b_cnt+=1
    if s[len(s)-i-1]==".":
        w_cnt+=1
b_lst.append(b_cnt)
w_lst.append(w_cnt)
w_lst.reverse()

ans=10**9
for b,w in zip(b_lst,w_lst):
    if ans > b+w:
        ans=b+w
print(ans)

Submission status: Accepted

Solution code:
input()
s=input()
cnt=s.count(".")
l=[cnt]
for i in s:
    cnt= cnt-1 if i=="." else cnt+1
    l.append(cnt)
print(min(l))

Submission status: Accepted

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

ans=[w_cnt]
b=0
w=w_cnt

for i in s:
    if i=="#":
        b+=1
    else:
        w-=1
    ans.append(b+w)

print(min(ans))

Submission status: Accepted

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

ans=[w_cnt]
b=0
w=w_cnt

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

print(min(ans))

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
kuro = s.count("#")
shiro = n-kuro
mi = shiro
k = 0
sh = shiro
for i in range(n):
    if s[i] == "#":
        k += 1
    else:
        sh -= 1
    mi = min(mi,k+sh)
print(mi)

Submission status: Accepted

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

black_right, min_val = 0, 2000001
white_left = s.count('.')
ans = [white_left+black_right]

for moji in s:
    if moji == '.':
        white_left -=1
    else:
        black_right += 1
    ans.append(white_left+black_right)
    #min_val = min(min_val, white_left+black_right)

print(min(ans))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
 
def main():
    N = int(input())
    S = input().strip()
    ans = 0
    x = 0
    for c in S:
        if c == "#":
            x += 1
        else:
            if x > 0:
                ans += 1
                x = max(x-1, 0)
    print(ans)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

cumdots = [0] * (N+1)

for i in range(N):
  cumdots[i+1] = cumdots[i] + (1 if S[i]=="." else 0)
  
#print(cumdots)

hands = N+1
for i in range(N):
  #i番目までをdot, i+1番目からsharpとする
  leftops = i - cumdots[i]
  rightops = cumdots[N] - cumdots[i+1]
  hands = min(hands, leftops + rightops)
  
print(hands)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
WL = 0
WR = 0
BL = 0
BR = 0

for i in range(N):
    if S[i] == '#':
        BR += 1
    else:
        WR += 1

ans = BL+WR

for i in range(N):
    if S[i] == '#':
        BR -= 1
        BL += 1
    else:
        WR -= 1
        WL += 1
    ans = min(ans,BL+WR)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
WL = 0
WR = 0
BL = 0
BR = 0

for i in range(N):
    if S[i] == '#':
        BR += 1
    else:
        WR += 1

ans = BL+WR

for i in range(N):
    if S[i] == '#':
        BR -= 1
        BL += 1
    else:
        WR -= 1
        WL += 1
    ans = min(ans,BL+WR)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
N = int(input())
S = list(str(input()))

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

minN = w
for j in range(N) :
  if S[j] == "#" :
    b += 1
  else :
    w -= 1
  minN = min(minN, b+w)
print (minN)

Submission status: Accepted

Solution code:
from itertools import groupby

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

l = [[i, len(list(j))] for i, j in groupby(S)]

w = 0
bl = 0
for i in S:
    if i == ".":
        w += 1
    else:
        bl += 1

ans = [w, bl]
cnt = 0
for i, j in l:
    if i == ".":
        w -= j
        ans.append(cnt + w)
    else:
        cnt += j
        ans.append(cnt + w)
        
print(min(ans))

Submission status: Accepted

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())
S = list(map(str, sys.stdin.readline().rstrip()))

left = [None] * N
right = [None] * N
tmp_white,tmp_black = 0,0
for i in range(N):
	if S[i] == '#':
		tmp_black += 1
	left[i] = tmp_black

for i in range(N):
	if S[-(i+1)] == '.':
		tmp_white += 1
	right[-(i+1)] = tmp_white

ans = min(left[-1], right[0])

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

print(ans)