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:
from copy import deepcopy

N = int(input())
S = input()
M = [0 for i in range(N+1)]

for i in range(N):
    if S[i] != '#':
        M[0] += 1

mini = deepcopy(M[0])
    
for i in range(N):
    if S[i] == '#':
        M[i+1] = M[i] + 1
    else:
        M[i+1] = M[i] - 1
        if mini > M[i+1]:
            mini = deepcopy(M[i+1])

print(mini)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
S = list(map(lambda x: 0 if x=="." else 1, S))
#all_white
# all_white = sum(S)
# all_black = len(S) - all_white

# white, black, white
ans_list = []
for thresh in range(len(S)):        
    if thresh == 0:
        ans = len(S) - sum(S)
        ans_list.append(ans)
    
    else:
        ans = ans_list[-1]
        if S[thresh-1] == 1: 
            ans += 1
        else:
            ans -= 1
        ans_list.append(ans)
ans_list.append(sum(S))
        
print(min(ans_list))

Submission status: Accepted

Solution code:
import sys
N  = int(input())
S = input()
black = S.count("#")
white = N - black
if black == 0 or white == 0:
    print(0)
    sys.exit()
# 先頭とそれに続く白は除外できる→　黒の始まりからカウントする
# 最後とそれに続く黒は除外できる→　白の終わりは除外する
first = 0
for i in range(N):
    if S[i] == '#':
        first = i
        break
last = N - 1 
for i in reversed(range(N)):
    if S[i] == '.':
        last = i
        break
if first < last:
    S = S[first:last+1]
N = len(S)
# Sは先頭が黒、最後が白になっている
black = S.count("#")
white = N - black
ans = min(black , white)
# tempは黒→白に変えた数
temp = 0
for i in range(N-1):
    if S[i] == '#':
        # 黒を白に変えると仮定する
        black -= 1
        temp += 1
    else:
        # 白だったので、今後変える必要のある白が減る
        white -= 1
    ans = min(ans, temp + black , temp + white) 
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
 
input()
l = [0] + list(accumulate(c == '.' for c in input()))
print(min(i - x + l[-1] - x for i, x in enumerate(l)))

Submission status: Accepted

Solution code:
# -*- coding utf-8 -*-
N = int(input())
S = input()

ans = 0
a = 0
num1, num2 = 0, 0
# dp = [[0] * N for _ in range(N)]
dp = [[0] *2 for _ in range(N + 1)]
for i in range(N):
    dp[i + 1][0] = dp[i][0] + (1 if S[i] == '.' else 0)
    dp[i + 1][1] = dp[i][1] + (1 if S[i] == '#' else 0)

ans = 1e10
for i in range(1, N + 1):
    num = dp[i - 1][1] + (dp[N][0] - dp[i - 1][0])
    ans = min(ans, num)
ans = min(ans, dp[N][1])
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
S = input()
# 右全部#
b = []
w = []
for i in range(N):
    if S[i] == '#':
        b.append(i)
    if S[i] == '.':
        w.append(i)

kouho = []
for i in range(0,N+1):
    cost = 0
    #iより右にある白の数
    cost += len(w) - bisect.bisect_left(w,i)
    #iより左にある黒の数
    cost += bisect.bisect_left(b,i)
    
    if cost >= 0:
        kouho.append(cost)

print(min(kouho))

Submission status: Accepted

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

cnt = [0 for _ in range(N + 1)]
black = 0
white = 0

for idx in range(N):
    if S[idx] == '#':
        black += 1
    else:
        white += 1
    cnt[idx] = black - white

print(min(cnt) + white)

Submission status: Accepted

Solution code:
import math
from operator import itemgetter
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
s = S()

a = s.count("#")
b = s.count(".")
cnta = 0
cntb = 0
ans = MAX_INT
for i in range(N):
	if s[i] == "#":
		ans = min(ans,cnta + (b-cntb))
		cnta += 1
	else:
		cntb += 1
if ans == MAX_INT:
	print(0)
else:
	print(min(ans,a,b))

Submission status: Accepted

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

import re

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

numw, numb = 0, 0
lw, lb = [0], [0]
for i in range(len(S)):
    if S[i] == '.':
        numw += 1
    else:
        numb += 1
    lw.append(numw)
    lb.append(numb)

ans = N
for i in range(len(S) + 1):
    ans = min(ans, lb[i] + (lw[-1] - lw[i]))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = 0
cnt2 = 0
tmp = 0
tmp2 = 0
ans = 10**9
for i in s:
    if i=="#":
        cnt += 1
    else:
        cnt2 += 1
for i in s:
    if i=="#":
        tmp += 1
    else:
        tmp2 += 1
    ans = min(ans,tmp+cnt2-tmp2)
print(min(ans,cnt,cnt2))

Submission status: Accepted

Solution code:
import itertools

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

for i in range(N):
    if S[i] == '.':
        S[i] = 0
    else:
        S[i] = 1

B = list(itertools.accumulate(S))

MIN = 10000000
cnt = []
for i in range(N):
    cnt.append(B[i] + N - B[N-1] + B[i] - i - 1)
    # MIN = min(MIN, cnt)
MIN = min(cnt)



S_s = list(set(S))
if len(S_s) == 1:
    MIN = 0
else:
    MIN = min(MIN, S.count(0))
print(MIN)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

b = 0
w = 0
tb = 0
tw = 0

for i in reversed(range(n)):
    if s[i] == '#':
        tb += 1
        if tb > tw:
            b += tb
            w += tw
            tb = 0
            tw = 0
    else:
        tw += 1
print(tb + w)

Submission status: Accepted

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

res = float("inf")

# if "#" not in S or "." not in S:
#     res = 0

counts = []
counts.append((0, 0))
a, b = 0, 0
for s in S:
    if s == "#":
        a += 1
    elif s == ".":
        b += 1
    counts.append((a, b))

for i in range(N + 1):
    res = min(res, counts[i][0] + counts[-1][1] - counts[i][1])

print(res)

Submission status: Accepted

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

temp=s.count("#")
if temp==0 or temp==n:
    print(0)
else:
    head=0
    tail=n-temp
    ret=tail+head
    for c in s:
        if c=="#":
            head+=1
        else:
            tail-=1
        temp2=head+tail
        if temp2<ret:
            ret=temp2
    print(ret)

Submission status: Accepted

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

alBlack = S.count('.')
alWhite = S.count('#')

#どこで区切るかの問題

#累積和

accumB = [] #N個目の石までに含まれる黒の数
#[0]～[N-1]で取り出す

cnt = 0
for i in range(N): #0~N-1でN回ループする
  if S[i] == '#': #黒なら+1,白ならそのまま格納
    cnt += 1
  accumB.append(cnt)

minRev = N #最小ひっくり返し数。ただし端は考えない
for i in range(1, N, 1): #スライサーは1～N-1
  #左側と右側のひっくり返し数
  leftRev = accumB[i-1] #すぐ左の累積黒
  rightRev = N - accumB[N-1] - (i - accumB[i-1])
  #右端の累積白-すぐ左の累積白
  # = 文字数-右端の累積黒 - (スライサー-すぐ左の累積黒)
  minRev = min(minRev, leftRev+rightRev)
  
print(min(minRev,alBlack,alWhite))

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
bi = [0]
wi = [0]
b = 0
w = 0
for i in range(n):
    if s[i] == '#':
        b += 1
    if s[-i-1] == '.':
        w += 1
    bi.append(b)
    wi.append(w)
print(min(bi[i]+wi[-i-1] for i in range(n+1)))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
dp = [0 for i in range(N)]
b = 0
w = 0
flag1 = True
flag2 = True
for i in range(N):
    if flag1 and S[i] == '.':
        pass
    elif flag1:
        flag1 = False
        b += 1
    elif S[i] == '.':
        dp[i] = b
    else:
        b += 1

for i in reversed(range(N)):
    if flag2 and S[i] == '#':
        pass
    elif flag2:
        flag2 = False
        w += 1
    elif S[i] == '#':
        dp[i] = w
    else:
        w += 1
ans = float('inf')
for i in range(N-1):
    if (S[i] == '.' and S[i+1] == '#') or (S[i] == '#' and S[i+1] == '.'):
        ans = min(ans,dp[i]+dp[i+1])

if ans == float('inf'):
    print(0)
else:
    print(min(b,w,ans))

Submission status: Accepted

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


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

print(cost)

Submission status: Accepted

Solution code:
from sys import stdin
# import math
import numpy as np
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = stdin.readline().rstrip().split()[0]

N = int(stdin.readline().rstrip().split()[0])
S = stdin.readline().rstrip().split()[0]

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

W=np.array(W)
B=np.array(B[::-1])
ans = N-np.max(W+B)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input().rstrip()
    i = 0
    b_count = 0
    w_count = 0
    # Comsume left whites
    while i < len(s):
        if s[i] == '.':
            i += 1
        else:
            break

    #Consume right blacks
    j = len(s) - 1
    while j > i:
        if s[j] != '.':
            j -= 1
        else:
            break

    # Compute baseline count
    left = i
    right = j
    while i <= j:
        if s[i] == '.':
            w_count += 1
        i += 1
            

    i = left
    min_count = w_count
    count = w_count
    while i <= right:
        if s[i] == '.':
            count -= 1
        else:
            count += 1
        i += 1
        if count < min_count:
            min_count = count

    print(min_count)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

output = N

for i in range(len(black_number_list)):
    number = (black_number_list[i][0]) + (N - i - black_number_list[i][1])
    if number < output:
        output = number

print (output)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
import collections as coll
t = coll.Counter(s)
b1,w1=t["#"],t["."]

b = 0
w = 0
p = 0
q = w1
for i in range(n):
    if s[i] == "#":
        b += 1
    elif s[i] == ".":
        w += 1
    
    p = b + w1 - w
    if p < q:
        q = p

print(q)

Submission status: Accepted

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

one, d = 0, 0
for i in range(N):
    if S[i] == '#':
        one += 1
    elif d > one - (i + 1 - one):
        d = one - (i + 1 - one)
print(d + N - one)

Submission status: Accepted

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

counter = [[0,0] for i in range(N)]
count = 0
for i in range(N):
    if S[i] == '#':
        count += 1
    counter[i][0] = count

count = 0
for i in range(N)[::-1]:
    if S[i] == '.':
        count += 1
    counter[i][1] = count

mini = N
for i in range(N-1):
    if mini > counter[i][0] + counter[i+1][1]:
        #print(i, counter[i][0] + counter[i+1][1])
        mini = counter[i][0] + counter[i+1][1]

white = S.count('.')
mini = 0 if white == 0 or white == N else mini
mini = white if white < mini else mini
white = S.count('#')
mini = white if white < mini else mini
print(mini)
#print(counter)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
d = []
for c in s:
    if d==[]:
        d.append([c,1])
    else:
        if c == d[-1][0]:
            d[-1][1]+=1
        else:
            d.append([c,1])

endwhite = 0
endblack = 0
white = 0
black = 0
for p in d:
    if p[0] == '.':
        endwhite = black
        endblack = min(endblack+ p[1],endwhite+p[1])
        white += p[1]
    if p[0] == '#':
        endblack = min(endblack,endwhite)
        endwhite += p[1]
        black += p[1]
print(min(endwhite,endblack))

Submission status: Accepted

Solution code:
def ns():
    import sys
    return map(int, sys.stdin.readline().split())
  
def ss():
    import sys
    return sys.stdin.readline().strip()
  
def dp():
  N, = ns()
  S = ss()
  
  c = sum(1 for s in S if s == '.')
  DP = [float('inf')] * (N+1)
  DP[0] = c

  for i, s in enumerate(S):
    if i < N:
      if s == '.':
      	DP[i+1] = DP[i] - 1
      else:
        DP[i+1] = DP[i] + 1
  return min(DP)

print(dp())

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = [0] * (n + 1)
l_c = 0
r = [0] * (n + 1)
r_c = 0
for i in range(1,n + 1):
  if s[i - 1] == '#':
    l_c += 1
  l[i] = l_c
for i in reversed(range(0, n)):
  if s[i] == '.':
    r_c += 1
  r[i] = r_c
m = float('inf')
for i in range(n + 1):
  m = min(m,l[i]+r[i])
print(m)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
dpw = [0]*N
dpb = [0]*N
if S[0] == '.':
  dpw[0] = 1
else:
  dpb[0] = 1
for i in range(1,N):
  if S[i] == '.':
    dpw[i] = dpw[i-1]+1
    dpb[i] = dpb[i-1]
  else:
    dpb[i] = dpb[i-1]+1
    dpw[i] = dpw[i-1]
totalw = dpw[N-1]
totalb = dpb[N-1]
ans = min(totalw,totalb)
for i in range(N):
  cur = dpb[i]+totalw-dpw[i]
  if cur <= ans:
    ans = cur
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
temp = S.count('.')
cnt = min(temp, N)

for i in range(N):
    if S[i] == '#':
        temp += 1
    else:
        temp -= 1
    cnt = min(temp, cnt)
print(cnt)

Submission status: Accepted

Solution code:
from itertools import groupby, accumulate

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

l = [len(list(g)) for k, g in groupby(S)]
if S[0] == ".":
    l.pop(0)
if S[-1] == "#":
    l.pop()

if len(l) == 0:
    print(0)
    exit()

min_num = 10**18

black = [0] + list(accumulate(l[::2]))
white = [0] + list(accumulate(l[1::2]))

x = len(l)//2
for i in range(x+1):
    m = (black[i] - black[0]) + (white[x] - white[i])
    min_num = min(m, min_num)

print(min_num)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
ans=0
ans2=0
stack=[0]*(n+1)
for i in range(n):
  if s[i]!="#":
    stack[0]+=1
    stack[i+1]-=1
  else:
    stack[i+1]+=1
for i in range(n):
  stack[i+1]+=stack[i]
print(min(stack))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
s=input()
ans = 0
kuro = [0]*(n+1)
siro = [0]*(n+1)
if n==1:
    ans=0
if n ==2:
    if s=="#.":
        ans=1
else: 
    for i,j in enumerate(s[::-1]):
        if j==".":
            kuro[i+1]=kuro[i]+1
        else:
            kuro[i+1]=kuro[i]
    for i,j in enumerate(s):
        if j == "#":
            siro[i+1]=siro[i]+1
        else:
            siro[i+1]=siro[i]
            
    ku = np.array(kuro[::-1])
    si = np.array(siro)
    ans=np.min(ku+si)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline



def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    S = input_raw[0]

    new_S = ""
    count = 0
    for i in range(N):
        if S[i] != "#":
            count += 1
    min_count = count
    for i in range(N):
        if S[i] == ".":
            count -= 1
        else:
            count += 1
        min_count = min(min_count, count)
    print min_count


##    new_S1 = ""
##    new_S2 = ""
##    flag = False
##    for i in range(N):
##        if S[i] == "." and flag == False:
##            new_S1 += "."
##        else:
##            new_S1 += "#"
##            flag = True
##
##    flag = False
##    for i in range(N):
##        if S[N - i - 1] == "#" and flag == False:
##            new_S2 += "#"
##        else:
##            new_S2 += "."
##            flag = True
##    count1 = 0
##    count2 = 0
##    new_S2 = new_S2[::-1]
##    for i in range(N):
##        if S[i] != new_S1[i]:
##            count1 += 1
##        if S[i] != new_S2[i]:
##            count2 += 1
##    print min(count1, count2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
p = 0
q = 0
sai = 0
v = 0
w = 0
pairs = []
for i in range(n - 1, -1, -1):
    if s[i] == "." and sai == 0:
        p = 0
        q = 0
        p += 1
        v = 1
        sai = 1
    elif s[i] == "." and v == 0:
        pairs.append([q, p])
        p = 0
        q = 0
        w = 0
        p += 1
        v = 1
    elif s[i] == "." and v == 1:
        p += 1
    elif s[i] == "#" and w == 0 and sai == 1:
        v = 0
        q += 1
        w = 1
    elif s[i] == "#" and w == 1:
        q += 1
pairs.append([q, p])
rp = []
for i in range(len(pairs)):
    rp.append(pairs[-i-1])
d = []
prp = []
p0 = 0
p1 = 0
for i in range(len(rp)):
    p0 += rp[i][0]
    p1 += rp[i][1]
    prp.append([p0, p1])
for i in range(len(prp)):
    d.append(prp[i][0] + prp[len(prp) - 1][1] - prp[i][1])
d.append(prp[len(prp) - 1][0])
d.append(prp[len(prp) - 1][1])
print(min(d))

Submission status: Accepted

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

w_counter_all = 0
for i in range(n):
    if s[i] == '.': # white
        w_counter_all += 1

if s[0] == '.':
    cost1 = 0
else:
    cost1 = 1
cost1_list = [cost1]
for i in range(1, n):
    if s[i] == '#':
        # black
        cost1 += 1
    cost1_list.append(cost1)


cost_all = [w_counter_all]
for i in range(n):
    cost_all.append(n - i - 1 - cost1_list[-1] + cost1_list[i] * 2)
print(min(cost_all))

Submission status: Accepted

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

p = S.count('.')
q = 0
ans = p

for i in range(N):
    if S[i] == '#':
        q += 1
    else:
        p -= 1

    ans = min(ans,p+q)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()+'?'
ans=10**10
arr=[0]*n
ans1=n-s.count('#')
ans2=n-s.count('.')
if s[0]=='.':
  arr[0]=1
for i in range(1,n):
  if s[i]=='.':
    arr[i]=arr[i-1]+1
  else:
    arr[i]=arr[i-1]
for i in range(1,n):
  tmp=(i-arr[i-1])+(arr[-1]-arr[i])
  if tmp<ans:
    ans=tmp
print(min(ans,ans1,ans2))

Submission status: Accepted

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

k = 0
a = [0]
for t in s:
 if t == "#":
  k += 1
 a.append(k)
#print(a)

m = a[n]
p = n
for i in range(n+1):
 p = min(p, a[i] + (n-i) - (m-a[i]))
print(p)

Submission status: Accepted

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
S = read()[:-1]

accum_ten = [0]
accum_sha = [0]
for i, s in enumerate(S):
    if s == '.':
        accum_ten.append(accum_ten[-1] + 1)
        accum_sha.append(accum_sha[-1])
    else:
        accum_ten.append(accum_ten[-1])
        accum_sha.append(accum_sha[-1] + 1)

ans = float('inf')

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

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())
ls=list(input())
c_list=[ls.count(".")]

for i in ls:
    if i==".":
        c_list.append(c_list[-1]-1)
    else:
        c_list.append(c_list[-1]+1)

print(min(c_list))

Submission status: Accepted

Solution code:
from numpy import cumsum

N = int(input())
S = input()
ss = S.replace("#", "1").replace(".", "0")
* ls, = map(int, ss)
cum_ls = cumsum(ls)
first_black = -1
last_black = N
for i in range(N):
    if S[i] == "#":
        first_black = i
        break
for i in reversed(range(N)):
    if S[i] == "#":
        last_black = i
    else:
        break
if first_black == -1:
    print(0)
else:
    # print(cum_ls)
    mans = 10**10
    for i in range(N):
        #print("  ", cum_ls[i], (N-(i+1) - (cum_ls[-1] - cum_ls[i])))
        mans = min(mans, cum_ls[i] + (N-(i+1) - (cum_ls[-1] - cum_ls[i])))
        # print(mans)

    ans = min(S[first_black:].count("."), S[first_black:].count(
        "#"), S[:last_black].count("#"), mans)

    print(ans)

Submission status: Accepted

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

white=S.count(".")

counter=[white]

for i in range(N):
    if S[i]=="#":
        counter.append(counter[i]+1)
    elif S[i]==".":
        counter.append(counter[i]-1)
        
        
    
print(min(counter))

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	S = input()
	w = 0
	b = 0
	stack = []
	if S[0] == ".":
		stack.append(0)

	before = S[0]
	current = 0
	for i, c in enumerate(S):
		if c == ".":
			w += 1
		else:
			b += 1
		if c != before:
			stack.append(current)
			before = c
			current = 1
		else:
			current += 1
	stack.append(current)
	if len(stack) %2 != 0:
		stack.append(0)

	ans = min(w, b)
	w_change = 0
	b_change = b
	while stack:
		current_w = stack.pop()
		current_b = stack.pop()
		w_change += current_w
		b_change -= current_b
		ans = min(ans, w_change+b_change)

	print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

b = S.count('#')
w = S.count('.')
black = [0]*int(3e5)
white = [0]*int(3e5)

cnt = 0

for i in range(N):
    check = True

    if S[i] == '#':
        if not black[cnt]:
            cnt += 1
        black[cnt] += 1
    else:
        if not white[cnt]:
            cnt += 1
        white[cnt] += 1

black2 = [0]*int(3e5)
white2 = [0]*int(3e5)

wa = 0
for i in range(int(3e5)):
    wa += black[i]
    black2[i] = wa

wa = 0
for i in range(int(3e5)):
    wa += white[i]
    white2[i] = wa

ans = int(3e5)
for i in range(N+1):
    check = black2[i] + w - white2[i]
    ans = min(check, ans)

print(ans)

Submission status: Accepted

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

if '#' not in S:
    print(0)
elif '.' not in S:
    print(0)
else:
    l=[0 for i in range(N+1)]
    w = 0
    for i in range(N):
        if S[i] == '#':
            l[i+1] = l[i] + 1
        else:
            l[i+1] = l[i] - 1
            w += 1          

    print(min(l)+w)

Submission status: Accepted

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

one = []
zero = []
cnt_one = 0
cnt_zero = 0

if s[0] == "#":
    zero.append(0)

for i in s:
    if i == "#":
        cnt_one += + 1
        if cnt_zero != 0:
            zero.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            one.append(cnt_one)
            cnt_one = 0

if cnt_one != 0:
    one.append(cnt_one)
else:
    zero.append(cnt_zero)

if s[-1] == "#":
    zero.append(0)

sum = 0
sum_zero = [0]
for i in range(len(one)):
    sum += zero[len(one)-i]
    sum_zero.append(sum)

sum = 0
sum_one = [0]
for i in range(len(one)):
    sum += one[i]
    sum_one.append(sum)


print(min(sum_one[i] + sum_zero[len(one)-i] for i in range(len(one)+1)))

Submission status: Accepted

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

left_blacks = [0] * (n + 1)
right_whits = [0] * (n + 1)
for i in range(n):
    left_blacks[i+1] = left_blacks[i] + 1 if s[i] == "#" else left_blacks[i]

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

ans = 9999999999999999999
for i in range(n+1):
    ans = min(ans, left_blacks[i] + right_whits[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
nb = s.count('#')
nw = s.count('.')
if nw == n or nb == n:
    print(0)
    exit()
lb = 0
rw = nw

b = False
ans = float('inf')
for i in range(n):
    if b == False and s[i] == '#':
        ans = min(ans, lb + rw)
        b = True
        lb += 1
    elif s[i] == '#':
        lb += 1
    elif s[i] == '.':
        rw -= 1
        b = False
ans = min(ans, nb)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = 10 ** 9
White = [0] * (N + 1)
Black = [0] * (N + 1)

for i, c in enumerate(S):
    if c == ".":
        White[i + 1] = White[i] + 1
        Black[i + 1] = Black[i]
    else:
        White[i + 1] = White[i]
        Black[i + 1] = Black[i] + 1

ans = min(ans, Black[N], White[N])

for i in range(1, N):
    temp = Black[i] + White[N] - White[i]
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


# globals
inf = float('inf')
N = 0
S = ""


def set_inputs():
    global N, S
    N = get_int()
    S = get_str()
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    s = "".join(itertools.dropwhile(lambda c: c == ".", S))
    black = []
    white = []
    count = 0
    prev = "#"
    for c in s:
        if c == prev:
            count += 1
        else:
            if prev == "#":
                black.append(count)
            else:
                white.append(count)
            prev = c
            count = 1
    if prev == ".":
        white.append(count)
    t = len(black)
    forward = list(itertools.accumulate([0] + black))
    backward = list(itertools.accumulate([0] + white[::-1]))
    print(min([forward[i] + backward[t-i] for i in range(t+1)]))
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


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


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(str(input()))
black = 0
white = 0
count = 0
find = False

for i in reversed(range(0,n)):
    if find == True and s[i] == '#':
        black += 1
    elif s[i] == '.':
        white += 1
        find = True
    if black == white and find == True:
        count += white
        white = 0
        black = 0
        find = False
count += black
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()
pt = S.find("#")
ct = 0
bl_ls = []
wh_ls = []
last_wh = 0
last_bl = 0
S = "." + S + "#"
N += 2
for i in range(N):
    if S[i] == ".":
        last_wh += 1
        wh_ls.append(last_wh)
        bl_ls.append(last_bl)
    else:
        last_bl += 1
        wh_ls.append(last_wh)
        bl_ls.append(last_bl)
_min = 1e9
for i in range(N):
    res = wh_ls[N-1] - wh_ls[max(0,i-1)] + bl_ls[max(0,i-1)]
    _min = res if res < _min else _min
print(_min)

Submission status: Accepted

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

Submission status: Accepted

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


left_white = 0
right_black = 0

for ss in s:
    if ss == '.':
        left_white += 1
min_sum = left_white + right_black


for ss in s:
    if ss == '.':
        left_white -= 1
    else:
        right_black += 1
    min_sum = min(min_sum, left_white + right_black)
print(min_sum)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
left = []
right = []
cl = 0
cr = 0
for i in range(n):
  left += [cl]
  if S[i] == "#":
    cl += 1
  right += [cr]
  if S[n - 1 -i] == ".":
    cr += 1
min = cl
for i in range(n):
  if S[i] == "#":
    p = left[i] + right[n - 1 -i]
    if p < min:
      min = p
print(min)

Submission status: Accepted

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math


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

csum = [[0 for j in range(2)] for i in range(n+1)]
# csum[i][0] := iコ目までの黒の数
# csum[i][1] := iコ目までの白の数

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

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

ans = n*2
for i,j in csum:
  ans = min(ans,i+j)

print(ans)

Submission status: Accepted

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