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:
# coding: utf-8
# Your code here!

N = int(input())
S = ['.'] + [x for x in input()] + ['#']

cum_white = []
count = 0
for i, s in enumerate(S):
    if s == '#':
        count += 1
    cum_white.append(count)
    
min_num = int(1e10)
for i, n_white in enumerate(cum_white):
    num = n_white + (N + 1 - i - (cum_white[-1] - n_white))
    min_num = min(min_num, num)
    
print(min_num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
INF=10**10
cost_w=0
cost_b=s.count(".")
ans=[INF for i in range(n+1)]
ans[0]=cost_b
for i in range(n):
    if s[i]=="#":
        cost_w+=1
    else:
        cost_b-=1
    ans[i+1]=cost_w+cost_b
print(min(ans))

Submission status: Accepted

Solution code:
import sys
import numpy as np

readInt = lambda: int(sys.stdin.readline())
readIntN = lambda: [int(v) for v in sys.stdin.readline().split(' ')]

n = readInt()
s = sys.stdin.readline().strip()

w = 0
b = 0
for c in s:
    w += int(c == '.')
    b += int(c == '#')


wc = 0
bc = 0
ret = min(w, b)
for c in s:
    wc += int(c == '.')
    bc += int(c == '#')

    ret = min(ret, bc + (w - wc))
print(ret)

Submission status: Accepted

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

w = '.'
b = '#'

white = [0 for i in range(n+1)]
black = [0 for i in range(n+1)]
ans = [0 for i in range(n+1)]
for i in range(n):
    if S[i] == b:
        black[i+1] = black[i] + 1
    else:
        black[i+1] = black[i]
    if S[-i-1] == w:
        white[-i-2] = white[-i-1] + 1
    else:
        white[-i-2] = white[-i-1]

for i in range(n+1):
    ans[i] = white[i] + black[i]
print(min(ans))

Submission status: Accepted

Solution code:
def get_input():
    N = int(input())
    S = input().strip()
    return N,S

w="."
b="#"


def solve(N,S):
    cur = 0
    whites = []
    for i in range(N):
        if S[i] == w:
            cur+=1
            whites.append(cur)
        else:
            whites.append(cur)
    cand = []
    for i in range(N):
        if i == 0:
            cand.append(whites[N-1])
        else:
            cand.append(i -whites[i] + (whites[N-1] - whites[i-1])) #all black i to N
    cand.append(N-whites[-1]) # all white 
    return min(cand)

if __name__ =="__main__":
    N,S = get_input()
    ans = solve(N,S)
    print(ans)

Submission status: Accepted

Solution code:
def chmin(a,b):
  if a < b:
    return a
  else:
    return b

N = int(input())
S = list(input() + "#")
dp = [[0,0] for i in range(N+2)]
dp[0][1] = 1

for i in range(N):
  if( S[i] == "#" ):
    if( S[i+1] == "#" ):
      dp[i+1][0] = chmin(dp[i][0],dp[i][1])
      dp[i+1][1] = dp[i][1]+1
    else:
      dp[i+1][0] = dp[i][1]
      dp[i+1][1] = chmin(dp[i][0],dp[i][1]) + 1
  else:
    if( S[i+1] == "#" ):
      dp[i+1][0] = chmin(dp[i][0],dp[i][1])
      dp[i+1][1] = dp[i][0]+1
    else:
      dp[i+1][0] = dp[i][0]
      dp[i+1][1] = chmin(dp[i][0],dp[i][1]) + 1
print( chmin(dp[N-1][0],dp[N-1][1]) )

Submission status: Accepted

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

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

# 全探索だと2^Nで無理

# WWWW...BBBB...となるように変える。(全てW、全てBもOK)
# W: .
# B: #

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

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:
        W[i] = W[i-1]
        B[i] = B[i-1]+1

# i-1までをWに、iからをBに変えるとする
# W[i-1]で、i-1までのWの個数が分かる(Wi-1)
# この時、i-(Wi-1)個のBをWに変える必要がある
# W[-1] - W[i-1]で、Bに変える必要のあるWの個数が分かる

mn = min(W[-1], B[-1]) # 全部B, 全部Wに変える場合を計算
for i in range(1, N):
    toW = i - W[i-1]
    toB = W[-1] - W[i-1]
    mn = min(mn, toB+toW)
print(mn)

Submission status: Accepted

Solution code:
#coding:utf-8

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

result = 999999
left = 0
right = len([x for x in S if x == '.'])
result_i = left + right
result = result_i

for i in range(0, N):
    if S[i] == '.':
        right -= 1
    if S[i] == '#':
        left += 1
    result_i = left + right
    result = min(result, result_i)
print(result)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
whites=[0]*(N+1)
whites[0]=1
curwhi=1
for k,i in enumerate(S):
    if i==".":
        curwhi+=1
    whites[k+1]=curwhi
curmin=1000000
allwhi=whites[N]
for k,w in enumerate(whites):#k個目まで白
    new=k+1+allwhi-2*w
    curmin=min(new,curmin)
print(curmin)

Submission status: Accepted

Solution code:

n = int(input())

s = input()


white = s.count('.')

lb = 0
rw = white


ans = rw + lb;

for c in s:

    if c == '.':
        rw -= 1
    else:
        lb += 1

#    print((lb,rw))
    t = rw + lb 

    ans = min(t, ans)

print(ans)

Submission status: Accepted

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

black = []
white = []

for i in range(N):
    prev = 0 if i == 0 else white[i-1]
    if S[N-i-1] == '.':
        white.append(prev + 1)
    else:
        white.append(prev)
    prev = 0 if i == 0 else black[i-1]
    if S[i] == '#':
        black.append(prev + 1)
    else:
        black.append(prev)
white = [0] + white
black = [0] + black
mn = 1e9

for i in range(N+1):
    res = white[N-i] + black[i]
    if res < mn:
        mn = res

print(mn)

Submission status: Accepted

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

ans = 10 ** 6
count=0
l_c = [0] * n
for i, c in enumerate(s):
    if c == "#":
        count += 1
    l_c[i] = count
count=0
r_c = [0] * n
for i, c in enumerate(s[::-1]):
    if c == ".":
        count += 1
    r_c[n-1-i] = count

for i in range(1,n):
    ans = min(ans, l_c[i-1] + r_c[i])

ans = min(ans, s.count("#"), s.count("."))
print(ans)

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
import collections
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
n = ip()                                     #入力整数1つ
#a,b,c= (int(i) for i in input().split())       #入力整数横2つ以上
#a = [int(i) for i in input().split()]        #入力整数配列
a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#k = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(3)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文
rb = 0
rw = 0
for i in range(n):
    if a[i] == '#':rb+=1
    else : rw+=1
lb = 0
lw = 0
s = rw
for i in range(n):
    if a[i] == '#':
        lb+=1
        rb-=1
    else :
        lw+=1
        rw-=1
    s = min(s,lb+rw)
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
T = [0]
for s in S:
    T.append(T[-1] if s == "." else 1+T[-1])
sm = T[-1]
mini = 100000000000000
length = N+1
for t in T:
    length -= 1
    if mini > t+(length-(sm-t)):
        mini = t+(length-(sm-t))
print(mini)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ndot=0
nx=0
mx=10000000
for i in range(n+1):
    if i==0:
        ndot=sum([1 for i in s if i=="."])
        nx=0
    else:
        if s[i-1]==".":
            ndot-=1
        elif s[i-1]=="#":
            nx+=1
    mx=min(mx,nx+ndot)
print(mx)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N = int(input())
    S = [0 if c == '.' else 1 for c in input().strip()]
    T = [0] + list(accumulate(S))

    B = T[N]
    ans = 10**5
    for i in range(N+1):
        convert_w = T[i]
        convert_b = (N - i) - (B - T[i])
        ans = min(ans, convert_w + convert_b)

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())	
s2 = s[:]

l=[0]
cnt1=0
for i in range(len(s)):
    if(s[i] == "#"):
        cnt1 +=1
    l.append(cnt1)
ansl=[]
for i in range(len(l)):
    ansl.append(n-i-cnt1+2*l[i])



print(min(ansl))

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)

Submission status: Accepted

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

white = [0]*(N+2)
black = [0]*(N+2)

black[0] = 0
white[N] = 0

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

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

count_min = 2 * 10 ** 6
for i in range(N+1):
    if black[i]+white[i] < count_min:
        count_min = black[i]+white[i]

print(count_min)

Submission status: Accepted

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

w = s.count('.')
m = w
for c in s:
    if c == '.':
        w -= 1
        if w < m: m = w
    elif c == '#':
        w += 1

print(m)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()
BN = [0 for i in range(N)]
if S[0] == '.':
    BN[0] = 0
else:
    BN[0] = 1
for i in range(1, N):
    if S[i] == '.':
        BN[i] = BN[i-1]
        continue
    BN[i] = BN[i-1] + 1
ans = min(N-BN[-1], BN[-1])
for i in range(1,N):
    white = BN[i-1]
    black = N-i-(BN[-1]-BN[i-1])
    # print(N, i, BN[-1], BN[i-1])
    # print(white, black)
    ans = min(ans, white+black)
print(ans)

Submission status: Accepted

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

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

left_w = [0] * (N)
right_b = [0] * (N)

for i in range(N):
  if i == 0:
    left_w[0] = int(S[0] == '#')
    right_b[-1] = int(S[-1] == '.')
  else:
    left_w[i] = left_w[i-1] + int(S[i] == '#')
    right_b[-i-1] = right_b[-i] + int(S[-i-1] == '.')
left_w = [0] + left_w
right_b = right_b + [0]
# print(left_w)
# print(right_b)

res = N
for i in range(N+1):
  res = min(res, right_b[i] + left_w[i])
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
'''
k=int(input())
a,b,c=map(int,input().split())
a=[int(i) in range(n)]
a=[0]*n
for i in range(n):
    a.append(int(i))
'''
pre=0
b=[0]*n
w=[0]*n

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

#print(s,b,w)
ans=min(n,w[0])
for i in range(n):
    ans=min(ans,b[i]+w[i])
    
print(ans)

Submission status: Accepted

Solution code:
# ...###のようにする
N = int(input())
S = input()

white_change_counts = [-1] * (N + 1)
black_change_counts = [-1] * (N + 1)
max_count = -1
white_change_counts[0] = 0
black_change_counts[0] = S.count('.')
min_count = white_change_counts[0] + black_change_counts[0]

for index in range(1, N + 1):
    if S[index - 1] == ".":
        white_change_counts[index] = white_change_counts[index - 1]
        black_change_counts[index] = black_change_counts[index - 1] - 1
    else:
        white_change_counts[index] = white_change_counts[index - 1] + 1
        black_change_counts[index] = black_change_counts[index - 1]

    count = white_change_counts[index] + black_change_counts[index]
    if min_count > count:
        min_count = count

print(min_count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
w = 0
b = 0
for c in S:
    if c == '.':
        w += 1
    if c == '#':
        b += 1
w0 = 0
b0 = 0
min_change = w
for c in S:
    if c == '.':
        w0 += 1
        w -= 1
    if c == '#':
        b0 += 1
        b -= 1
    min_change = min(min_change, w + b0)
print(min_change)

Submission status: Accepted

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

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

a = [0] * n

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

needs = [n]
cnt = 0
for i in range(n):
    if s[i] == "#":
        need = 0
        cnt += 1
        need += a[i]
        needs.append(need + cnt - 1)
need = min(needs)

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

print(min(black,need))

Submission status: Accepted

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

w = S.count('.')
v = w
ans = w
for c in S:
    if c == '.':
        v -= 1
    else:
        v += 1
    ans = min(ans, v)
print(ans)

Submission status: Accepted

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

clusters=[]
W, B = 0, 1
c_prev = None
seq=0
for c in S:
    if c != c_prev and c_prev!=None:
        clusters.append(seq)
        seq=0
    seq+=1
    c_prev=c
    
clusters.append(seq)
    
if S[0]=='.':
    clusters = [0] + clusters
if S[-1]=='#':
    clusters.append(0)

min_nflip = nflip = sum(clusters[1::2])
for i in range(len(clusters)//2):
    nflip+=clusters[i*2]-clusters[i*2+1]
    min_nflip = min(min_nflip, nflip)

print(min_nflip)

Submission status: Accepted

Solution code:
n=int(input())
s=list(str(input()))
m = [0 for i in range(len(s))]
if s[0] == ".":
    m[0] = 1
else:
    m[0] = 0
for i in range(1,len(s)):
    if s[i] == ".":
        m[i] = m[i-1]+1
    else:
        m[i] = m[i-1]
l = [0 for i in range(len(s)+1)]
l[0] = m[-1]
for i in range(1,len(s)+1):
    l[i] = i-m[i-1] + m[-1]-m[i-1]
print(min(l))

Submission status: Accepted

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

ruiseki_r = 0
ruiseki_l = 0


left_ruiseki = []
right_ruiseki = []


for s in S:
    if s == '#':
        ruiseki_l += 1
    left_ruiseki.append(ruiseki_l)


for s in S[::-1]:
    if s == '.':
        ruiseki_r += 1
    right_ruiseki.append(ruiseki_r)

right_ruiseki = right_ruiseki[::-1]
right_ruiseki.append(0)

cost = float('inf')

for i in range(N):
    tmpcost = left_ruiseki[i] + right_ruiseki[i+1]
    cost = min(cost,tmpcost)

ans = min(left_ruiseki[N-1], right_ruiseki[0], cost)

print(ans)

Submission status: Accepted

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

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

c = w
for i in range(len(S)):
    if S[i] == ".":
        w -=1
    else:
        b += 1
    if c > w + b:
        c = w + b
print(c)

Submission status: Accepted

Solution code:
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

n = int(input())
s = input()
b = [0]
w = [0]
for i in range(n):
    bt = b[i]
    wt = w[i]
    if s[i] == "#":
        bt += 1
    else:
        wt +=1
    b.append(bt)
    w.append(wt)

cost = 1000000000000

for i in range(0,n+1):
    temp = 0
    temp += b[i]
    temp += w[-1]-w[i]

    # print(i)
    # print(b[i]-b[1])
    # print(w[-1]-w[i])

    if cost > temp:
        cost = temp
    #print(cost)
    # print("*********")


#print(b)
#print(w)

print(cost)

Submission status: Accepted

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

w = s.count('.')
b = 0
ret = w
for i in range(0, n):
    if s[i] == '.':
        w -= 1
    else:
        b += 1
    if ret > w + b:
        ret = w+b
print(ret)

Submission status: Accepted

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

dp = [None]*(N+1)

dp[0] = 0
for s in S:
  if s == '.':
    dp[0] += 1

for i in range(1, N+1):
  if S[i-1] == '.':
    dp[i] = dp[i-1] - 1
  elif S[i-1] == '#':
    dp[i] = dp[i-1] + 1

print(min(dp))

Submission status: Accepted

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

changes = 0

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

min_changes = changes

for i in range(n):
    if s[n - 1 - i] == ".":
        changes += 1
    else:
        changes -= 1
    
    if changes < min_changes:
        min_changes = changes

print(min_changes)

Submission status: Accepted

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

WHITE = '.'
BLACK = '#'

white_dp = [0 for _ in range(N)]
black_dp = [0 for _ in range(N)]

black2white = 0
for i in range(N):
    black2white += 1 if S[i] == BLACK else 0
    white_dp[i] = black2white
white_dp = [0] + white_dp

white2black = 0
for i in range(N-1, -1, -1):
    white2black += 1 if S[i] == WHITE else 0
    black_dp[i] = white2black
black_dp.append(0)

ans = N
for i in range(N+1):
    ans = min(ans, white_dp[i]+black_dp[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

s = []
for i in input():
  if i == '.':
    s.append(0)
  else:
    s.append(1)

black_num = sum(s)
change_num = n - black_num
ans = change_num

for i in s:
  if i == 0:
    change_num -= 1
  else:
    change_num += 1
  ans = min(ans,change_num)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

bk_all = wh_all = 0
for s in S:
    if s == "#":
        bk_all += 1
    else:
        wh_all += 1

ans = min(bk_all, wh_all)


left_bk = 0
right_wh = wh_all
for i in range(N):
    if S[i] == '#':
        left_bk += 1
    else:
        right_wh -= 1
    ans = min(ans, left_bk + right_wh)
print(ans)

Submission status: Accepted

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

black = 0
black_counts = [0]*N
white = 0
white_counts = [0]*N
for i in range(N):
    if S[i] == '#':
        black += 1
    black_counts[i] = black

    if S[-1-i] == '.':
        white += 1
    white_counts[N-1-i] = white

min_count = min(black_counts[N-1], white_counts[0])
for i in range(N-1):
    min_count = min(min_count, black_counts[i] + white_counts[i+1])
print(min_count)

Submission status: Accepted

Solution code:
n=int(input())
b,w=[0],[0]
for i in input():
  if i==".":
    b.append(0)
    w.append(1)
  else:
    b.append(1)
    w.append(0)
b.append(0)
w.append(0)
for i in range(1,n+1):
  b[i]+=b[i-1]
for i in range(n,0,-1):
  w[i]+=w[i+1]
ans=200000
for i in range(1,n+2):
  ans=min(ans,b[i-1]+w[i])
print(ans)

Submission status: Accepted

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

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

min_num = min(right_white, n-right_white)

for x in s:
    if x == ".":
        right_white -= 1
    else:
        left_black += 1

    min_num = min(min_num, right_white+left_black)

print(min_num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w_list=[0]
w_count=0
b_list=[0]
b_count=0
for i in s:
    if i=='.':
        w_count=w_count+1
        w_list.append(w_count)
        b_list.append(b_count)
    else:
        b_count=b_count+1
        w_list.append(w_count)
        b_list.append(b_count)
Min=n
for i in range(0,n+1):
    w_number=i-w_list[i]
    b_number=n-i-(b_list[n]-b_list[i])
    number=w_number+b_number
    if number<Min:
        Min=number
print(Min)

Submission status: Accepted

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

rd = 0
for e in s:
    if e == ".":
        rd += 1

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

print(res)

Submission status: Accepted

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

b = [0]
w = [0]

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

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

Submission status: Accepted

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

def f(s):
    b = 0
    for i in s:
        if i == "#":
            b += 1
        yield b

def g(cb, n):
    bl = 0 if n == 0 else cb[n-1]
    wr = (len(cb) - n) - (cb[-1] - bl)

    return bl + wr

cb = list(f(S))
print(min([g(cb, i)for i in range(len(cb) + 1)]))

Submission status: Accepted

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

num_w=num_b=0
for i in range(n):
    let = s[i]
    if let == ".":
        num_w+=1
    else:
        num_b+=1


ans=num_w

mini = ans

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

print(mini)

Submission status: Accepted

Solution code:
_n=int(input())
_s=input()
t=len([1 for s in _s if s=="."])
c=[]
c.append((t,0))
for n in range(_n):
    if _s[n]=="#":
        t+=1
    else:
        t-=1
    c.append((t,n+1))
c.sort()
print(c[0][0])

Submission status: Accepted

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

num = s.count('.')
nums = [num]
for c in s:
    if c == '#':
        num += 1
    else:
        num -= 1
    nums.append(num)

print(min(nums))

Submission status: Accepted

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

b = S.count('#')

cnt = 0

result = N-b

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = [0 for _ in range(n)]
b = [0 for _ in range(n)]
for i in range(n):
    if s[i] == '.':
        w[i] = 1
    else:
        b[i] = 1
wsum = [0 for _ in range(n+1)]
bsum = [0 for _ in range(n+1)]
for i in range(n):
    wsum[i+1] = wsum[i] + w[i]
    bsum[i+1] = bsum[i] + b[i]
ans = 10**6
for i in range(n+1):
    ans = min(ans, bsum[i]-bsum[0]+wsum[n]-wsum[i])
print(ans)

Submission status: Accepted

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

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

if s[0]=="#": W[0]=1
else: B[0]=1

for i in range(1,N):
    if s[i]=="#":
        B[i]=min(B[i-1],W[i-1])
        W[i]=W[i-1]+1

    else:
        B[i]=min(B[i-1],W[i-1])+1
        W[i]=W[i-1]

print(min(B[N-1],W[N-1]))

Submission status: Accepted

Solution code:
import sys
# gcd
# from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


N = ii()
S = li()
intS = [0] * N
for i in range(N):
    if S[i] == '#':
        intS[i] = 1
# print(intS)
acuS = list(accumulate(intS))
# print(acuS)
minS = 10 ** 9
for i in range(N):
    minS = min(minS, acuS[i] + (N-1-i) - (acuS[N - 1] - acuS[i]))
# print(minS)
print(min(minS, N - acuS[N-1]))

Submission status: Accepted

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

rd = 0
for e in s:
    if e == ".":
        rd += 1

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

print(res)

Submission status: Accepted

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

w = [0]
w_total = 0
b = [0]
b_total = 0
for i in range(N):
    if S[i] == '#':
        w_total += 1
    w.append(w_total)
    if S[N - 1 - i] == '.':
        b_total += 1
    b.append(b_total)

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
black = [0]*(n+1)
white = [0]*(n+1)
for i in range(n):
    if s[i] == '#':
        black[i+1] = black[i] + 1
        white[i+1] = white[i]
    elif s[i] == '.':
        black[i+1] = black[i]
        white[i+1] = white[i] + 1
ans = 10000000000000000000000000000000000000000
for i in range(n+1):
    ans = min(ans,white[-1]-white[i]+black[i])

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate
INF = float("inf")


def solve(N: int, S: str):
    stones = [1 if s == "#" else 0 for s in S]
    acc = [0]+list(accumulate(stones))
    counter = 0
    m = INF
    for i in range(N+1):
        m = min(m, (acc[i]) + (N-i-(acc[N]-acc[i])))
    print(m)

    return


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:
a=int(input())
b=input()
c = 0
mx = 0
mxc = 0
for i in range(len(b)):
    if b[i] == ".":
        c += 1
        if mx < c:
            mx = c
            mxc = i+1
    else:
        c -= 1


print(int((mxc-mx)/2+(len(b)-mxc-mx+c)/2))

Submission status: Accepted

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

Submission status: Accepted

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


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=input()
cnt1=[0]*n
cnt2=[0]*n

if s[0]=="#": cnt1[0]=1
if s[n-1]==".": cnt2[n-1]=1

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

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

ans=10**18
for i in range(n-1):
    ans=min(ans,cnt1[i]+cnt2[i+1])
ans=min(ans,cnt1[n-1])
ans=min(ans,cnt2[0])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math

#https://atcoder.jp/contests/agc008/submissions/15248942
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

#ランレングス圧縮
def rle_comp(S):
     rle = []
     pre = 'X'
     chain = 1
     for c in S:
         if c == pre:
             chain += 1
         else:
             if pre != 'X':
                 rle.append([pre,chain])
             
             pre = c
             chain = 1
     rle.append([pre,chain])
 
     return rle

N = ini()
S = input()
rle_s = rle_comp(S)
w = S.count(".")
b = S.count("#")
if rle_s[-1][0] == "#":
    b -= rle_s[-1][1]
    del rle_s[-1]

wb = 0
#先頭が黒
if rle_s[0][0] == "#":
    wb += rle_s[0][1]
    if len(rle_s) >= 4:
        for v in range(3,len(rle_s),2):
            wb += rle_s[v][1]

#先頭が白
else:
    if len(rle_s) >= 3:
        for v in range(2,len(rle_s),2):
            wb += rle_s[v][1]

if wb != 0:
    print(min(min(w,b),wb))
else:
    print(min(w,b))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())
num = 0
for i in range(N):
  if S[i] == '#' and S[i + 1] ==".":
    num++
print(num)

Submission status: Runtime Error

Solution code:
from copy import *
n = int(input())
s = list(input())
S = []
l = 0
for i in range(n-1):
    if s[i] != s[i+1]:
        S.append(s[l:i+1])
        l = i+1
if S[-1][0] == s[-1]:
    S[-1] += s[l:]
else:
    S.append(s[l:])


ans = 0
m = len(S)

if S[0][0] == '#':
    for i in range(m//2):
        ans += min(len(S[i*2]), len(S[i*2+1]))
else:
    for i in range(m//2 - 1):
        ans += min(len(S[i*2+1]), len(S[i*2+2]))

print(ans)

Submission status: Runtime Error

Solution code:
 = int(input())
s = list(input())
cnt1 = 0
for i in range(n-1):
    if s[i] == '#' and s[i+1] == '.':
        cnt += 1
        s[i+1] = '#'

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


print(min(cnt1, cnt2))

Submission status: Runtime Error

Solution code:
def main():
  N = map(int,input.split())
  S = input()
  black = S.count('#')
  white = S.count('.')

  if black == N or white == N:
    print(0)
    return 
 	
  B = [0]*(N+1)
  W = [0]*(N+1)
  for i in range(N):
    if S[i] == '#':
      B[i+1] = 1
    elif W[i] == '.':
      W[i+1] = 1
  
  for i in range(1,N+1):
    B[i]+=B[i-1]
    W[i]+=W[i-1]
  ans = float('inf')
  for i in range(1,N+1):
    ans = min(ans, B[i]+white-W[i])
  
main()