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 = str(input())
# '.'の場合は白, '#'の場合は黒

if S[0] == '.':
    count_white = [1]
    count_black = [0]
else:
    count_white = [0]
    count_black = [1]


for i in range(1, N):
    if S[i] == '.':
        # 見ている石が白なら
        count_white.append(count_white[i-1]+1)
        count_black.append(count_black[i-1])
    else:
        # 見ている石が黒なら
        count_white.append(count_white[i-1])
        count_black.append(count_black[i-1]+1)

tmp = 9999999999
for i in range(1, N):
    min_sum = 0
    min_sum += count_black[i-1]
    min_sum += (count_white[N-1] - count_white[i])
    tmp = min(tmp, min_sum)

# print(tmp)

cnt = 0
target = S[0]
for i in range(1, N):
    if target == '#' and S[i] == '.':
        cnt += 1
        target = '#'  # 黒に変える
        continue

    if S[i] == '#':
        target = '#'
    else:
        target = '.'

cnt_2 = 0
aim = S[-1]
for i in range(N-1, -1, -1):
    if aim == '.' and S[i] == '#':
        cnt_2 += 1
        aim = '.'
        continue

    if S[i] == '#':
        aim = '#'
    else:
        aim = '.'


print(int(min(cnt, cnt_2, tmp)))

Submission status: Accepted

Solution code:
N = int(input())
s = input()
l = []
b = 0
w = s.count(".")

for i in range(N):
    if s[i] == "#":
        l.append(b+w)
        b += 1
    else:
        w -= 1
        l.append(b+w)
print(min(l))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
N=int(input())
S=input()
w=S.count('.')
c=w
for i in range(N):
  if S[i]=='.':
    w-=1
  else:
    w+=1
  c=min(c,w)
print(c)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
b = 0
w = S.count('.')
res = w
for s in S:
	if s == '.':
		w -= 1
	else:
		b += 1
	res = min(res,b+w)
print(res)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

false_black = [0] * n
false_white = [0] * n
for i, x in enumerate(s):
    if s[i] == "#":
        false_black[i] = 1
    else:
        false_white[i] = 1

# 累積和
ac_fb = [0] + false_black
ac_fb = list(accumulate(ac_fb))
ac_fw = [0] + false_white
ac_fw = list(accumulate(ac_fw))

ans = 10 ** 6
for i, x in enumerate(s):
    tmp_ans = ac_fb[i] + (ac_fw[-1] - ac_fw[i])
    ans = min(ans, tmp_ans)
    
# このままだと全て白、黒の場合に失敗するので
print(min(ans, ac_fw[-1], ac_fb[-1]))

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()
res=s.count('.')
ans=res

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

Submission status: Accepted

Solution code:
import numpy as np
from itertools import accumulate
N =int(input())
S = input()

s = np.array(list(S))

k = np.where(s=="#", 1, 0)

l = [0] + list(accumulate(k))
ans = []
for i in range(N+1):
    p = l[i] +  N-i-l[N]+l[i]
    ans.append(p)

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
white = 0
black = 0
for i in range(n):
    if s[i] == ".":
        white += 1
ans = white + black
for j in range(n):
    if s[j] == ".":
        white -= 1
    else:
        black += 1
    if white + black < ans:
        ans = white + black
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
white = 0
black = 0
for i in range(n):
    if s[i] == ".":
        white += 1
ans = float("inf")
for j in range(n + 1):
    if white + black < ans:
        ans = white + black
    if j == n:
        break
    if s[j] == ".":
        white -= 1
    else:
        black += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [i for i in input()]

flg = 0
idx = 0
for i in S:
    if i == '#':
        flg = 1
        break
    idx += 1

if flg == 1:
    a = S[idx+1:].count('.')
    b = S[idx:].count('#')
    if S[-1] == '#':
        for ct in range(1, N+1):
            if S[-ct] == '.':
                break
        b -= ct-1
    ans = min(a, b)
else:
    ans = 0

count_blk = []
count_wht = []

ct_blk = 0
ct_wht = 0
for i in S:
    if i == '#':
        ct_blk += 1
    else:
        ct_wht += 1
    count_blk.append(ct_blk)
    count_wht.append(ct_wht)

for i in range(N):
    ans = min(ans, count_blk[i] + ct_wht - count_wht[i])

print(ans)

Submission status: Accepted

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

r=s.count(".")
l=0
c=0
ans=r

for i in s:
  if i=="#":
    l+=1
  else:
    r-=1
  if ans>l+r:
    ans=l+r
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
#print(S)
ans = 0
kari = 0
for i in range(N):
        if S[i] == ".":
                ans += 1
                kari += 1
for j in range(N):
        if S[j] == "#":
                kari += 1
        elif S[j] == ".":
                kari -= 1
        if kari < ans:
                ans = kari
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if '.' not in S or '#' not in S:
  print(0)
  sys.exit()
  
l = 0  # 境界より左の'#'の数
r = S.count('.')  # 境界より右の'.'の数
ans = l+r
for s in S:
  if s == '.':
    r -= 1
  else:
    l += 1
  ans = min(l+r, ans)

print(ans)

Submission status: Accepted

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

count_w = s.count('.')
count_b = 0
ans = count_w + count_b

for si in s:
    if si == '.':
        count_w -= 1
    else:
        count_b += 1
    now = count_w + count_b
    if now < ans:
        ans = now

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
bs = 0
ws = s.count('.')
ans = ws
for c in s:
    if c == '.':
        ws -= 1
    else:
        bs += 1
    now = ws + bs
    if ans > now:
        ans = now
print(ans)

Submission status: Accepted

Solution code:
input()

minv = res = rd = 0
for e in input():
    if e == ".":
        res += 1
        rd -= 1
        if rd < minv:
            minv = rd
    else:
        rd += 1

print(res + minv)

Submission status: Accepted

Solution code:
n = int(input())

minv = res = rd = 0
for e in input():
    if e == ".":
        res += 1
        rd -= 1
        minv = min(minv, rd)
    else:
        rd += 1

print(res + minv)

Submission status: Accepted

Solution code:

def count(s):
    w = s.count('.')
    candidate = w
    for i in range(len(s)):
        if s[i] == '#':
            w += 1
        else:
            w -= 1
        candidate = min(candidate, w)

    return candidate


def main():
    _ = int(input())
    s = input()  # '#.#' -> '###'

    print(count(s))


main()

Submission status: Accepted

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

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

Submission status: Accepted

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

# 右全部#にする

w = S.count('.')

ans = 10**9
cost = 0
for c in S:
    if c == '.':
        # 今見てる所以降全部白にする事を考える
        w -= 1
        # まだ見てない白の数 + 今まで白にした黒のコストが答え候補
        if w + cost < ans:
            ans = w + cost
    else:
        # 今見てる所が黒の場合、白の個数は減らない
        if w + cost< ans:
            ans = w + cost
        # 以降の計算に向けてコストを1増やす
        cost += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
b = [0]*(n)
w = [0]*(n)
if n == 1:
    print(0)
    exit()
if s[0] ==".":
    w[0] =1
else:
    b[0] =1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int (input ())
S = str (input ())
S = '.' + S
N += 1
cnt = 0
inf = int(1e9)

A = list(range(N))
for i in range(N):
    A[i] =[inf,inf]
A[0][0] = A[0][1] = 0
for i in range (1,N):
    for j in range (2):
        if j == 1:
            if S[i] == '.':
                A[i][j] = min(A[i][j], A[i-1][0] + 1)
                A[i][j] = min(A[i][j], A[i-1][1] + 1)
            else:
                A[i][j] = min(A[i][j], A[i-1][0] )
                A[i][j] = min(A[i][j], A[i-1][1] )
        else:
            if S[i] == '#':
                A[i][j] = min(A[i][j], A[i-1][0] + 1)
            else:
                A[i][j] = min(A[i][j], A[i-1][0])                

ans = min(A[N-1][0],A[N-1][1])

print (ans)

Submission status: Accepted

Solution code:
n = int(input())
S = list(input())
ans = S.count('.')
s = S.count('#')
d = ans
ss = 0
dd = 0
for i in range(n):
    if (S[i] == '#'):
        ss += 1
    else:
        dd += 1
    ans = min(ss + d - dd, ans)


print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
blacks_in_left = [0 for i in range(n+1)]
whites_in_right = [0 for i in range(n+1)]
for i in range(n):
  blacks_in_left[i+1] = blacks_in_left[i] + int(s[i] == '#')
  whites_in_right[n-1-i] = whites_in_right[n-i] + int(s[n-1-i] == '.')

res = n + 1
for i in range(n+1):
  # replace blacks in [0,i] with whites and whites in [i+1,n] with blacks
  res = min(res, blacks_in_left[i] + whites_in_right[i])
  
#print(blacks_in_left)
#print(whites_in_right)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
#N = 3
#S = "#.#"
stone_list = []
tmp = 1
for i in range(N):
    if i == N-1:
        stone_list.append(tmp)
        break
    if S[i+1] != S[i]:
        stone_list.append(tmp)
        tmp = 1
    else:
        tmp += 1
        
# whiteから初めてwhiteで終わらせる
if S[0] == '#':
    stone_list = [0] + stone_list
if S[N-1] == '#':
    stone_list = stone_list + [0]

tmp = sum(stone_list[2::2])
ans = tmp
for i in range(1,len(stone_list)-1,2):
    tmp = tmp + stone_list[i] - stone_list[i+1]
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop
import re

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, ss_raw()))
INF = 1 << 29


def runLength(in_str):
    kukan = 1
    start = 0
    ret = []
    for i in range(1,len(in_str)):
        if in_str[i-1]!=in_str[i]:
            ret.append([in_str[i-1],kukan,start])
            start = i
            kukan=0
        kukan+=1
    ret.append([in_str[-1],kukan,start])
    return ret

N = int_raw()
S= input()
 
def main():
    ans = INF
    numB = [0]
    for c in S:
        numB.append(numB[-1])
        if c=="#":
            numB[-1]+=1
    for i in range(len(S)+1):
        ans = min(ans, numB[i]+ len(S)-i-(numB[-1]-numB[i]))
    return ans

print(main())

Submission status: Accepted

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

a = []
#  .##..#.##.
w = s.count(".")
b = s.count("#")
m = min(w,b)

bb=0

for i in range(n):
    if s[i]=='.':
        w-=1
        a.append(bb+w)
    if s[i]=='#':
        bb += 1
        a.append(bb+w)
if len(a)>0:
    I = min(a)
    print(min(m,I))
else:
    print(m)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
b = [0]*N
w = [0]*N
ans = []
for n in range(N):
    if S[n] == '#':
        if n == 0:
            b[n] = 1
        else:
            b[n] = b[n-1] +1
    else:
        if n == 0:
            b[n] = 0
        else:
            b[n] = b[n-1]

    if S[N-1-n] == '.':
        if N-1-n == N-1:
            w[N-1-n] = 1
        else:
            w[N-1-n] = w[N-n] +1
    else:
        if N-1-n == N-1:
            w[N-1-n] = 0
        else:
            w[N-1-n] = w[N-n]

for n in range(N):
    ans.append(b[n]+w[n])


print(min(ans)-1)

Submission status: Accepted

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

Submission status: Accepted

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

bi=0
wi=0
b=[]
w=[]
for i in range(n):
	if sl[i] == "#":
		bi = bi+1
	else:
		wi = wi+1
	b.append(bi)
	w.append(wi)

c=0
res=2*10**6
for i in range(n-1):
	if (sl[i]=="#" and sl[i+1]==".") or (sl[i]=="." and sl[i+1]=="#") :
		c = b[i] + w[n-1]-w[i]
		if c < res:
			res = c

w0=w[n-1]
b0=b[n-1]
if w[0]==1:
	for i in range(n-1):
		if w[i]==w[i+1]:
			w0=w[n-1]-w[i]
			break
if b[n-1]-b[n-2]==1:
	for i in range(n-1):
		if b[n-1-i]==b[n-1-i-1]:
			b0=b[n-1-i]
			break

if w0<res:
	res=w0
if b0<res:
	res=b0

print(res)

Submission status: Accepted

Solution code:
n = int(input())
S = list(input())
ans = S.count('.')
c = ans
for i in range(n):
    if (S[i] == '#'):
        c += 1
    else:
        c -= 1
    ans = min(ans, c)


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()                                                      
list_S = list(S)
N1 = 0
list_X =[]
X = list_S.count('.')
list_X.append(X)
    
for color in list_S:
    if list_S[N1] == '.':
        X -= 1
        list_X.append(X)
        N1 +=1
    else:
        X += 1
        list_X.append(X)
        N1 +=1

X = list_S.count('#') 
list_X.append(X)

X_MIN = min(list_X)
print(X_MIN)

Submission status: Accepted

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

black_sum = [0]*(N+1)
white_sum = [0]*(N+1)
ans = S.count('.')
for i in range(N):
    key_black = 0
    key_white = 0
    if S[i]=='#':
        key_black = 1
    else:
        key_white = 1

    black_sum[i+1]=black_sum[i]+key_black
    white_sum[i+1]=white_sum[i]+key_white
tmp = 0
for i in range(1,len(black_sum)):
    
        left = black_sum[i]  #境界左の黒石の数
        right = white_sum[-1]-white_sum[i]  #境界右の白石の数
        tmp = left + right
        ans = min(tmp,ans)

print(ans)

Submission status: Accepted

Solution code:
#毎回関数を呼ぶとおそくなる
n = int(input())
stones = input()
score = stones.count(".")
r = score
for i in range(n):
	if stones[i] == ".":
		r -= 1
	else:
		r += 1
	score = min(r, score)
print(score)

Submission status: Accepted

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

Submission status: Accepted

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

count_black = 0
count_white = 0
a = n + 1
cnt_white = [0] * a
cnt_black = [0] * a
result = []

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

cnt_white[0] = count_white

for i in range(n):
    if s[i] == "#":
        count_black += 1
    else:
        count_white -= 1
    cnt_white[i+1] = count_white
    cnt_black[i+1] = count_black

for i in range(n+1):
    result.append(cnt_black[i] + cnt_white[i])

ans = min(result)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=input()
l1=[]
c=0
for i in range(n):
  if l[i]=="#":
    c+=1
  l1.append(c)
l2=[]
for i in range(n):
  l2.append(n-(i+1)-(c-l1[i])+l1[i])
print(min(min(l2),c,n-c))

Submission status: Accepted

Solution code:
#C
N = int(input())
S = str(input())
sign = ('.', '#') #w,b
if N == 1:
    print(0)
else:
    c1, c2 = 0, S.count('.')
    ans = c1 + c2
    for i in range(0,N):
        if S[i] == '#':
            c1 += 1
        else:
            c2 -= 1
        tmp = c1 + c2
        ans = min(ans, tmp)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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()

#.#
b = S.count("#")
w = S.count(".")

cotB = 0
cotW = 0
ans = w
for i,s in enumerate(S):
    if s ==".":
        cotW+= 1
    else:
        cotB+=1
    if ans >=  cotB + (w - cotW):
        ans = cotB + (w - cotW)
print(ans)

Submission status: Accepted

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

N = int(input())
S = [int(x) for x in input().replace('#', '1').replace('.', '0')]

b_cumsum = numpy.cumsum(S)
w_cumsum = numpy.cumsum(S[::-1])
b_total = b_cumsum[-1]
# print(S)
# print(b_cumsum)
# print(b_total)

ans = N
for i in range(N):

    # for white
    w_num = i + 1
    # w_cur = w_num - res[i]
    w_rev_num = b_cumsum[i]  # w_num - w_cur

    # for black
    b_num = N - w_num
    # b_cur = b_total - b_cumsum[i]
    b_cur = w_cumsum[b_num]
    ans = min(ans, w_rev_num + b_num - b_cur)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
b = []
nb = 0
w = []
nw = 0
for s_ in s:
  if s_ == "#":
    nb += 1
  if s_ == ".":
    nw += 1
  b.append(nb)
  w.append(nw)
ans = []
#print(b)
#print(w)
for i in range(n):
  if i == n-1 and s[i] == ".":
    ans.append(b[i]+1)
  elif i == 0:
    ans.append(w[n-1])
  else:
    ans.append(b[i-1]+w[n-1]-w[i-1])
ans.append(b[n-1])
#print(ans)
if n == 1:
  print("0")
else:
  print(min(ans))

Submission status: Accepted

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

cum_sum_b = [0]
cum_sum_w = [0]

sum_b = 0
sum_w = 0
for i, s in enumerate(S):
    if s == '#':
        sum_b += 1
    else:
        sum_w += 1
    cum_sum_b.append(sum_b)
    cum_sum_w.append(sum_w)

ans = N
for i in range(0, len(S) + 1):
    count = cum_sum_b[i] + cum_sum_w[len(S)] - cum_sum_w[i]
    ans = min(ans, count)

print(ans)

Submission status: Accepted

Solution code:
from sys import exit

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

stack = []

black = 0
white = 0

for i in range(len(S)):
    if S[i] == '.':
        white += 1

ans = len(S)
for i in range(len(S) + 1):
    if ans > black + white:
        ans = black + white

    if i == len(S):
        continue
    if S[i] == '.':
        white -= 1
    else:
        black += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
b = [0]*n
w = [0]*n
if s[0] == "#":
    b[0] = 1
for i in range(1,n):
    if s[i]=="#":
        b[i] = b[i-1]+1
    else:
        b[i] = b[i-1]
if s[-1] == ".":
    w[-1] = 1
for i in range(n-2,-1,-1):
    if s[i]==".":
        w[i] = w[i+1]+1
    else:
        w[i] = w[i+1]
a = min(w[0],b[n-1])
for i in range(n-1):
    if a > b[i]+w[i+1]:
        a = b[i]+w[i+1]
print(a)

Submission status: Accepted

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

shiro_num = s.count(".")
kuro_num = n - shiro_num

right_kuro_num = kuro_num
right_siro_num = shiro_num

result = shiro_num

for e in s:
    if e == '.':
        right_siro_num -= 1
    else:
        right_kuro_num -= 1
    num = right_siro_num + kuro_num - right_kuro_num
    result = min(num,result)

print(result)

Submission status: Accepted

Solution code:
import numpy
N=int(input())
S=list(map(str,input()))
S_modify = [0 if i=="." else 1 for i in S]
count_list = []

sum_list=numpy.cumsum(S_modify)
sum_list_reverse=numpy.cumsum(S_modify[::-1])
for i in range(N):
    count = sum_list[i]+((N-i-1)-sum_list_reverse[N-i-1])
    count_list.append(count)
print(min(count_list))

Submission status: Accepted

Solution code:
import numpy
N=int(input())
S=input()
S_modify = list(map(int,S.replace('.','0').replace('#','1')))
count_list = []

sum_list=numpy.cumsum(S_modify)
sum_list_reverse=numpy.cumsum(S_modify[::-1])
for i in range(N):
    count = sum_list[i]+((N-i-1)-sum_list_reverse[N-i-1])
    count_list.append(count)
print(min(count_list))

Submission status: Accepted

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

A = [0] * (N+1)
B = [0] * (N+1)

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

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

ans = 10 ** 9
for i in range(N+1):
    ans = min(ans, A[i] + B[i])

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
bres=0
wres=0
blst=[]
wlst=[]
for i in range(n):
    if s[i]=='#':
        bres+=1
    blst.append(bres)
for i in range(n-1,-1,-1):
    if s[i]=='.':
        wres+=1
    wlst.append(wres)
wlst=wlst[::-1]
ans=10**9
blst.insert(0,0)
wlst.insert(n,0)
for i in range(n+1):
    ans=min(blst[i]+wlst[i],ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
c1 = [0] * N
c2 = [0] * N
ans = N
for i in range(N):
    c1[i] += c1[i - 1] + 1 * (S[i] == "#")
    c2[i] += c2[i - 1] + 1 * (S[i] == ".")
c1 = [0] + c1
c2 = [0] + c2
for i in range(N):
    ans = min(ans, c1[i] + c2[N] - c2[i + 1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
c1 = [0] * N
c2 = [0] * N
ans = N
for i in range(N):
    c1[i] += c1[i - 1] + 1 * (S[i] == "#")
    c2[i] += c2[i - 1] + 1 * (S[i] == ".")
c1 = [0] + c1
c2 = [0] + c2
#print(c1)
#print(c2)
for i in range(N):
    #print(c1[i], c2[N] - c2[i])
    ans = min(ans, c1[i] + c2[N] - c2[i + 1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = 0
narabi = []
befores = '#'
for S in s:
    if S == befores:
        cnt += 1
    else:
        narabi.append(cnt)
        befores = S
        cnt = 1
narabi.append(cnt)
if s[-1] == '#':
    narabi.append(0)
hennkann = s.count('.')
saishou = s.count('.')
hashu = 0
dot = 1
for i in range(int(len(narabi) / 2)):
    hennkann = hennkann - narabi[dot] + narabi[hashu]
    saishou = min(saishou,hennkann)
    dot += 2
    hashu += 2
print(saishou)

Submission status: Accepted

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

cost=[]
left=0
right=0
for i in range(N):
	if S[i]=='#':
		right+=1

cost.append(N-right)
for i in range(0,N):
	if S[i]=='#':
		left+=1
		right-=1
	cost.append(left+N-i-1-right)

cost.sort()
print(cost[0])

Submission status: Accepted

Solution code:
import numpy as np

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

S_map = [1 if i=="." else 0 for i in S]
cumsum = np.array(S_map).cumsum()
cumsum = np.insert(cumsum, 0, 0)
ans = []

for i in range(N+1):
    if(i == 0 or i == N or S[i-1] != S[i]):
        if(i != 0):
            left_white = cumsum[i]
            left_black = i - left_white
        else:
            left_white = 0
            left_black = 0
        if(i != N):
            right_white = cumsum[-1] - cumsum[i]
            right_black = (N - i) - right_white
        else:
            right_white = 0
            right_black = 0
        ans.append(min(left_white+right_white, left_black+right_black, left_black+right_white))
print(min(ans))

Submission status: Accepted

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

white_count = S.count(".")
black_count = 0
ans = white_count
for s in S:
    if s == "#":
        black_count += 1
    else:
        white_count -= 1
    if ans > black_count + white_count:
        ans = black_count + white_count

print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import math

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


cnt = S.count(".")

l = [cnt] #all black
for i in range(N):
    if S[i]==".":
        cnt -= 1
        l.append(cnt)
    else:
        cnt += 1
        l.append(cnt)

# print(l)
print(min(l))

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import math

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

# ans = 10**6
# blacks = [0 for i in range(N)]
# black = 0
# white = 0
# for i in range(N):
#     if S[i] == "#":
#         black += 1
#
#     else:
#         white += 1
#     blacks[i] = black
#
# ans = min(black,white,ans)
#
# # print(blacks)
# for i in range(1,N):
#     cnt = 0
#     cnt += (N-(i+1))-(blacks[N-1]-blacks[i])#white
#     # print("white",cnt)
#     cnt += blacks[i-1]#black
#     # print(cnt)
#     ans = min(ans,cnt)
#
# print(ans)

#count white
cnt = S.count(".")

l = [cnt] #all black
for i in range(N):
    if S[i]==".":
        cnt -= 1
        l.append(cnt)
    else:
        cnt += 1
        l.append(cnt)

# print(l)
print(min(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
m = n
counts = s[:].count(".")
m = min(n,counts)
for i in range(n):
    counts += s[i].count("#") - s[i].count(".")
    if m > counts:
        m = counts
        
print(m)

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import bisect
from datetime import datetime as dt
n = input()
s = raw_input()

m = n
left = s[:0].count('#')
right = s[0:].count('.')
m = min(m, left + right)
for i in range(len(s)):
    if s[i] == '.':
        right -= 1
    else:
        left += 1
    m = min(m, right + left)
print m

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def MI(): return map(int, input().split())
def II(): return int(input())
def IS(): return input()
def LI(): return list(map(int, input().split()))


n = II()
s = list(IS())
black = [0] * 200005
black[0] = 0
counter = 0
for i in s:
    counter += 1
    if i == '#':
        black[counter] = black[counter-1] + 1
    else:
        black[counter] = black[counter-1]
mini = len(s) - black[n]
for i in range(1,n+1):
    mini = min(mini,(black[i] + n - i - (black[n] - black[i])))
print(mini)

Submission status: Accepted

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

#n = 9
#s = "........."
#print(s)
#print(len(s))

memo = []
countwb=0
now = s[0]
count = 0
if now == "#":
    memo.append(0)
while(count<n):
    if s[count]==now:
        countwb+=1
    else:
        memo.append(countwb)
        countwb = 1
        now = s[count]
    count+=1
memo.append(countwb)
#print(memo)

memo_r=[]
for i in range(len(memo)):
    memo_r.append(memo[-1*(i+1)])
#print(memo_r)

count_w = []
c_w = 0
if len(memo_r)%2==1:
    count_w.append(0)
    for  i in range(0,len(memo_r),2):
            c_w += memo_r[i]
            count_w.append(c_w)
else:
    count_w.append(0)
    count_w.append(0)
    for  i in range(1,len(memo_r),2):
            c_w += memo_r[i]
            count_w.append(c_w)
#print(count_w)

count_b = []
count_b.append(0)
count_b.append(0)
c_b = 0
for  i in range(1,len(memo),2):
        c_b += memo[i]
        count_b.append(c_b)
#print(count_b)

count_list = []
for i in range(len(count_b)):
    temp = count_w[-1*(i+1)]+count_b[i]
    count_list.append(temp)
#print(count_list)

print(min(count_list))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(raw_input())
s = str(raw_input())

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

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

print ans[n-1]

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
left_black=0
right_white=S.count(".")
ans=left_black+right_white

for i in S:
    if i==".":
        right_white-=1
        ans=min(ans,left_black+right_white)
    else:
        left_black+=1
        ans=min(ans,left_black+right_white)
print(ans)

Submission status: Accepted

Solution code:
N = input()
S = input()
count = 0

list_str = S
count_black = 0
count_white = S.count('.')
ans = count_black + count_white

for i in S:
    if i == '#':
        count_black += 1
    else:
        count_white -= 1

    if ans >= count_white + count_black:
        ans = count_black + count_white

# print(count_black)
# print(count_white)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
b = [0] * (n + 1)
w = [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 = [vb + vw for vb, vw in zip(b, w)]
print(min(ans))

Submission status: Accepted

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

wsum = [0]
bsum = [0]

for s in S:
	wsum.append(wsum[-1] if "." == s else wsum[-1] + 1)
for s in S[::-1]:
	bsum.append(bsum[-1] if "#" == s else bsum[-1] + 1)

#print(wsum)
#print(bsum)

msum = 2**31-1
for i in range(len(S) + 1):
	msum = min(msum, wsum[i] + bsum[-1-i])

print(msum)

Submission status: Accepted

Solution code:
N=int(input())
S = input()
w = [0]*(N+1)
b = [0]*(N+1)
for i, s in enumerate(S):
    if s=='.':
        w[i+1] = w[i]+1
        b[i+1] = b[i]
    elif s=='#':
        b[i+1] = b[i]+1
        w[i+1] = w[i]
ans = 10**20
for i in range(N+1):
    ans = min(ans, b[i]+w[N]-w[i])
print(ans)