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, S = int(input()), input()
bl = [0] * N
for i in range(N):
    if S[i] == "#":
        bl[i] += 1
bl_sum = [0] * N
for i in range(N):
    if i == 0:
        bl_sum[i] = bl[i]
    else:
        bl_sum[i] = bl_sum[i-1] + bl[i]

ans = N - bl_sum[N-1]

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

print(ans)

Submission status: Accepted

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

left_s = 0
right_d = 0

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

for i in range(N):
    if S[i] == '#':
        left_s += 1
    else:
        right_d -=1
    ans_tmp = left_s + right_d
    if ans > ans_tmp:
        ans = ans_tmp
print(ans)

Submission status: Accepted

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

s = [0 for _ in range(N+1)]
for i, x in enumerate(S):
    if x == "#":
        s[i+1] = s[i] + 1
    else:
        s[i+1] = s[i]
num = N
for i in range(N+1):
    w = s[i] - s[0]
    b = (N - i) - (s[N] - s[i])
    if w + b < num:
        num = w + b
print(num)

Submission status: Accepted

Solution code:
from sys import stdin
def solve():
  n = int(input())
  s = stdin.readline().rstrip()
  
  leftblack = 0
  leftwhite = 0
  rightblack = s.count('#')
  rightwhite = s.count(".")
  
  ans = leftblack+rightwhite
  
  for i in range(n):
    if s[i] == "#":
      leftblack += 1
      rightblack -= 1
    else:
      leftwhite += 1
      rightwhite -= 1
    ans = min(ans,leftblack+rightwhite)
  
  print(ans)
solve()

Submission status: Accepted

Solution code:
def solve():
  n = int(input())
  s = input()
  
  leftblack = 0
  leftwhite = 0
  rightblack = s.count('#')
  rightwhite = s.count(".")
  
  ans = leftblack+rightwhite
  
  for i in range(n):
    if s[i] == "#":
      leftblack += 1
      rightblack -= 1
    else:
      leftwhite += 1
      rightwhite -= 1
    ans = min(ans,leftblack+rightwhite)
  
  print(ans)
solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

# -*- coding: utf-8 -*-
n = int(input())
s = list(input())

sum_w = [0]*(n+1)
sum_b = [0]*(n+1)

for i in range(n):
    if s[i] == '#':
        sum_b[i+1] = sum_b[i] + 1
    else:
        sum_b[i+1] = sum_b[i]

    if s[i] == '.':
        sum_w[i+1] = sum_w[i] + 1
    else:
        sum_w[i+1] = sum_w[i]

ans = n
for i in range(n+1):

    # 0番目からi番目までの黒の数
    black = sum_b[i]
    #  i+1番目からn番目(最後)までの白の数
    white = sum_w[n]-sum_w[i]

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

Submission status: Accepted

Solution code:

# -*- coding: utf-8 -*-

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

sum_w = [0]*(n+1)
sum_b = [0]*(n+1)
ans = n

for i in range(n):
    if s[i] == '#':
        sum_b[i+1] = sum_b[i] + 1
    else:
        sum_b[i+1] = sum_b[i]

    if s[i] == '.':
        sum_w[i+1] = sum_w[i] + 1
    else:
        sum_w[i+1] = sum_w[i]

ans = n
for i in range(n+1):
    ans = min(ans, sum_b[i]+sum_w[n]-sum_w[i])
print(ans)

Submission status: Accepted

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

cost = S.count(".")
ans = cost

for i in range(N):
  if S[i] == "#":
    cost += 1
  else:
    cost -= 1
  ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
bool = []
for i in s:
    if i == "#":
        bool.append(1)
    else:
        bool.append(0)
black = sum(bool)
bool_t = [0]
for i in range(n):
    bool_t.append(bool[i] + bool_t[i])
ans = float("inf")
for i in range(n + 1):
    ans = min(ans, bool_t[i] + n - black - (i - bool_t[i]))
print(ans)

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(tmp, ans)
    
print(ans)

Submission status: Accepted

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

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

count = 0
brack_stone_num = 0
white_stone_num = 0
WtoB_stone_num = 0
for i in range(N-1, -1, -1):
    if S[i] == "#":
        if  white_stone_num == 0:
            brack_stone_num = 0
        else:
            brack_stone_num += 1
        if brack_stone_num > white_stone_num:
            WtoB_stone_num += white_stone_num
            brack_stone_num = 0
            white_stone_num = 0
    else:
        white_stone_num += 1
    
print(brack_stone_num + WtoB_stone_num)

Submission status: Accepted

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

w = 0
e = s.count('.')
result = e

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

    result = min(result, w+e)

print(result)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
sharp=[0]*n
dot=[0]*n
if s[0]=="#":
    sharp[0]+=1
else:
    dot[0]+=1
for i in range(1,n):
    if s[i]=="#":
        sharp[i]=sharp[i-1]+1
        dot[i]=dot[i-1]
    else:
        sharp[i]=sharp[i-1]
        dot[i]=dot[i-1]+1
MIN=3*10**6
for i in range(n):
    if s[i]=="#":
        MIN=min(MIN,(sharp[i]-1)+(dot[-1]-dot[i]))
    else:
        MIN=min(MIN,sharp[i]+(dot[-1]-dot[i]))
print(MIN)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
b = S.count(".")    # 黒に変えるべき石の数
w = 0               # 白に変えるべき石の数
ans = b

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

    else:
        b -= 1
    t = w + b
    ans = min(ans, t)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
i = 0
b = S.count('.')
w = 0
min = b
for i in range(N):
    if S[i] == '#':
        w += 1
    else:
        b -= 1
    
    T = b + w
    if T < min:
        min = T

print(min)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime,random
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = int(input())
S = input()
Sum_White = [0]
Sum_Black = [0]

tmp_w = tmp_b = 0
for s in S:
    if s == '#':
        tmp_b += 1
    else:
        tmp_w += 1
    Sum_Black.append(tmp_b)
    Sum_White.append(tmp_w)

#print(Sum_Black,Sum_White)
ans = INF
for i in range(N+1):
    tmp = Sum_Black[i] + (Sum_White[N]-Sum_White[i])
    ans = min(ans,tmp)
    #print(i,tmp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from __future__ import print_function
import numpy as np
import sys

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

n=int(input())
S=list(input())
array0=np.zeros(n,dtype=int)
array1=np.zeros(n,dtype=int)
array2=np.zeros(n,dtype=int)

array0[0]= 1 if S[0]=='#' else 0
for i in range(1,n):
    array0[i]= array0[i-1]+1 if S[i]=='#' else array0[i-1]


array1[n-1]= 1 if S[n-1]=='.' else 0
for i in range(n-2,-1,-1):
    array1[i]= array1[i+1]+1 if S[i]=='.' else array1[i+1]
    
for i in range(0,n):
    array2[i]=array0[i]+array1[i]
eprint(array0)
eprint(array1)
eprint(array2)
    
print(min(array2)-1)
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#

Submission status: Accepted

Solution code:
N = int(input())
S = [1 if i == "." else 0 for i in input()]
# 黒は#,白が.
# 黒は0,白は1
# 黒の右に白は許されない
# ある場所を境に左は全部白、右は全部黒にするから、それの逆の個数を数える
# ある境より右にある白の数
rw = sum(S)
# ある境より左にある黒の数
lb = 0
min_temp = rw+lb
for idx,s in enumerate(S):
    # 黒の場合
    if s==0:
        # rw pass
        lb += 1
    # 白の場合
    elif s==1:
        rw -= 1
    # 小さい方を残す
    min_temp = min([min_temp,rw+lb])
    if min_temp == 0:break
print(min_temp)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = input()

    if '#' not in S or '.' not in S:
        print(0)
        exit()

    b_num = 0
    w_num = S.count('.')
    ans = 10e6

    for i in range(N+1):
        ans = min(ans, b_num + w_num)
        if i != N:
            if S[i] == '#':
                w_num += 1
            else:
                b_num -= 1

    print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

white = S.count('.')
black = N - white

l_white = 0
l_black = 0
r_white = white
r_black = black

cost = l_black + r_white

for s in S:
    if s == '.':
        l_white += 1
        r_white -= 1
    else:
        l_black += 1
        r_black -= 1
    cost = min(cost, l_black + r_white)
    
print(cost)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
N = [[0 for _ in range(2)] for _  in  range(len(S))]

B,W = 0,1
# 左から.W 右へ#B
for idx,s in enumerate(S):
  if s is "#":
    N[idx][B] = N[idx-1][B] + 1
    N[idx][W] = N[idx-1][W]
  else:
    N[idx][B] = N[idx-1][B]
    N[idx][W] = N[idx-1][W] + 1

print(int(min([N[i][B]+N[-1][W]-N[i][W] for i in range(len(S)-1)]+[N[-1][W],N[-1][B]]   )))

Submission status: Accepted

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

black_point = 0
turn_to_white = 0
turn_to_black = s.count(".")
min_turn = turn_to_black
previous_s = s[0]

for i, current_s in enumerate(s[1:]):
	if previous_s == "#":
		turn_to_white += 1
	else:
		turn_to_black -= 1
	min_turn = min(turn_to_white + turn_to_black, min_turn)
	previous_s = current_s

if s[-1] == "#":
	turn_to_white += 1
else:
	turn_to_black -= 1
min_turn = min(turn_to_white + turn_to_black, min_turn)

print(min_turn)

Submission status: Accepted

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

import numpy as np

N = np.zeros((len(S),2))
B,W = 0,1
# 左から.W 右へ#B
for idx,s in enumerate(S):
  if s is "#":
    N[idx,B] = N[idx-1,B] + 1
    N[idx,W] = N[idx-1,W]
  else:
    N[idx,B] = N[idx-1,B]
    N[idx,W] = N[idx-1,W] + 1

print(int(min([N[i,B]+N[-1,W]-N[i,W] for i in range(len(S)-1)]+[N[-1,W],N[-1,B]]   )))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
	
def main():
	N = int(input())
	S = input()
	
	if N == 1:
		print(0)
		sys.exit()
	
	kuro = []
	for i, val in enumerate(S):
		if i == 0:
			if val == "#":
				kuro.append(1)
			else:
				kuro.append(0)
			continue
		if val == "#":
			kuro.append(kuro[i-1] + 1)
		else:
			kuro.append(kuro[i-1])
	
	shiro = [0 for _ in range(N)]
	for i in range(N-1, -1, -1):
		if i == N-1:
			if S[i] == ".":
				shiro[i] = 1
			else:
				shiro[i] = 0
			continue
		if S[i] == ".":
			shiro[i] = shiro[i+1] + 1
		else:
			shiro[i] = shiro[i+1]
			
	ans = -1
	for i in range(N):
		if i == 0:
			continue
		tmp = kuro[i-1] + shiro[i]
		if ans > tmp or ans == -1:
			ans = tmp
	
	#print(kuro)
	#print(shiro)
	print(min(ans,shiro[0],kuro[N-1]))
	
	
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from functools import reduce
l = int(input())
src = list(map(lambda x: 1 if x=="." else 0, input()))
lb = 0
rw = sum(src)
a = rw
for i in range(l):
  if src[i]==0:
    lb+=1
  else:
    rw-=1
  a = min(a, lb + rw)
  if a==0:
    break
print(a)

Submission status: Accepted

Solution code:
from functools import reduce
l = int(input())
src = list(map(lambda x: 1 if x=="." else 0, input()))
left_black = 0
right_white = sum(src)
ans = right_white
for i in range(l):
  if src[i]==0:
    left_black+=1
  else:
    right_white-=1
  ans = min(ans, left_black + right_white)
  if ans==0:
    break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
black_in_left = 0
white_in_right = s.count('.')
ans = black_in_left + white_in_right

for x in s:
    if x == '#':
        black_in_left += 1
    if x == '.':
        white_in_right -= 1
    cost = black_in_left + white_in_right
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

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

b = 0
w = S.count(".")
ans = w

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

print(ans)

Submission status: Accepted

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

    ans = min(ans, w + b)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
list1=[0]*N
for i in reversed(range(N)):
    if i==N-1 and S[i]==".":
        list1[i]+=1
    elif i==N-1:
        list1[i]=0
    elif S[i]==".":
        list1[i]=list1[i+1]+1
    else:
        list1[i]=list1[i+1]   
list2=[]
for i in range(N+1):
    if i==0:
        tmp=N-list1[0]
    elif i==N:
        tmp=list1[0]
    else:
        tmp=i-list1[0]+2*list1[i]
    list2.append(tmp)
print(min(list2))

Submission status: Accepted

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

white = S.count('.')
black = 0
ans = white + black

for x in S:
    if x == '.':
        white -= 1
    else:
        black += 1
    ans = min(ans, white+black)

print(ans)

Submission status: Accepted

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

c_b = 0
c_w = S.count('.')
sum = []

for i in S:
        if i == '.':
                c_w = c_w - 1
        sum.extend([c_b+c_w])
        if i == '#':
                c_b = c_b + 1

print(min(sum))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
b = S.count(".")
c = 0
d = N
for i in range(0, N):
    if S[i] == ".":
        b -= 1
        d = min(b+c, d)
    if S[i] == "#":
        c += 1
        d = min(b + c -1, d)
print(d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
min = N
clm_sum = [0]
for s in S:
    if s == '#':
        clm_sum.append(clm_sum[-1] + 1)
    else:
        clm_sum.append(clm_sum[-1])
for i in range(1,len(clm_sum)):
    if clm_sum[i-1] + ((N - i) - (clm_sum[-1] - clm_sum[i])) < min:
        min = clm_sum[i-1] + ((N - i) - (clm_sum[-1] - clm_sum[i]))
print(min)

Submission status: Accepted

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

a,b = 0,0
for i in s:
    if i == '.':
        a += 1 #白の個数
    else:
        b += 1 #黒の個数

min_c = c = 10**10
counta, countb = 0, 0
for k, i in enumerate(s):
    if s[k] == '.':
        counta += 1
    if s[k] == '#':
        countb += 1
        c = (a - counta) + (countb - 1)
    if min_c > c:
        min_c = c
        
print(min(a,b, min_c))

Submission status: Accepted

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

left = [0]*(n+1)
right = [0]*(n+1)
for i in range(n):
    left[i+1] = left[i] + (s[i] == "#")
    right[n-i-1] = right[n-i] + (s[n-i-1] == ".")
v_min = n + 1
for i in range(n+1):
    v = left[i] + right[i]
    if v_min > v:
        v_min = v
print(v_min)

Submission status: Accepted

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

c_b = 0
c_w = S.count('.')
sum = []

for i in S:
	if i == '.':
		c_w = c_w - 1
	sum.extend([c_b+c_w])
	if i == '#':
		c_b = c_b + 1

print(min(sum))

Submission status: Accepted

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

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

for i in range (len(S)):
  if S[i]==".":
    white -=1
  else:
    countb +=1 
  ans=min(ans, countb+white)

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N = int(input())
S = input()
black = 0
white = 0
l = [] # 

for s in S:
    if s == "#":
        black += 1
    else:
        white += 1

if white == 0:
    print(0)
    
else:
    leftBlack = 0
    rightWhite = white
    l.append(leftBlack + rightWhite)
    for s in S:
        if s == "#":
            leftBlack += 1
        else:
            rightWhite -= 1
            
        l.append(leftBlack + rightWhite)
        
    print(min(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

# .......####### という形を目指す

#累積和を計算
sum_black = [0]
sum_white = [0]
for n in range(0,N):
    if S[n] == '#': #black
        sum_black.append(sum_black[n] + 1)
        sum_white.append(sum_white[n] + 0)
    else:
        sum_black.append(sum_black[n] + 0)
        sum_white.append(sum_white[n] + 1)

num_min = N
for n in range(0,N+1):
    num = sum_black[n] + (sum_white[-1] - sum_white[n]) 
    if num < num_min:
        num_min = num

print(num_min)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
left = 0
right = S.count(".")
ans = right

for i in range(len(S)):
    if S[i] == ".":
        right -= 1
    else:
        left += 1

    ans = min(ans, left + right)
  
print(ans)

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:
        b[i]=b[i-1]+1
        w[i]=w[i-1]
ans=min(w[n-1], b[n-1])
for i in range(1, n):
    ans=min(ans, b[i-1]+w[n-1]-w[i-1])
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

wlist=[0 for i in range(n)]
blist=[0 for i in range(n)]

wnum=0
bnum=0

for i in range(n):
  if s[i]=='.':
    wnum+=1
  else:
    bnum+=1
  wlist[i]=wnum
  blist[i]=bnum
#print(wlist,blist)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
whsum = [0]*(N+1)
blsum = [0]*(N+1)
for i in range(N):
    whsum[i+1] = whsum[i]+(1 if S[i]=="." else 0)
    blsum[i+1] = blsum[i]+(1 if S[i]=="#" else 0)
res = 10**6
for left in range(N+1):
    tmp = 0
    tmp += blsum[left]-blsum[0]
    tmp += whsum[N]-whsum[left]
    res = min(res,tmp)
print(res)

Submission status: Accepted

Solution code:
#!/snap/bin/pypy3

def main():
    N = int(input())
    S = input()

    whitestones = []
    tmpcount = 0
    for stone in S:
        if stone == '.':
            tmpcount += 1
        whitestones.append(tmpcount)

    totalwhite = whitestones[-1]

    print(min(min(i - wstone + totalwhite - wstone for i, wstone in enumerate(whitestones, start=1)), totalwhite))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
_ = int(input())
S = input()
ans = 10**9
cost = S.count('.')
for s in S:
    cost -= s == '.'
    ans = cost if cost< ans else ans
    cost += s == '#'
print(ans)

Submission status: Accepted

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

ans = 10**9
cost = 0
for s in S:
    w -= s == '.'
    ans = w + cost if w + cost< ans else ans
    cost += s == '#'
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(N,S):
	num_b = 0
	counter=["0"]*N
	pointer = []
	flag = 0;
	for i in range(N):
		if(S[i]=='#'):
			if(flag==0):
				counter[i]=num_b
				flag=1
				pointer.append(i)
			num_b+=1
		elif(S[i]=='.'):
			flag=0
	if(pointer==[]):return 0
	min_change=N+1
	for i in pointer:
		#all_white
		a = counter[i] + (num_b - counter[i])
		if(min_change>a):min_change=a
		#all_black
		a = (i - counter[i]) + (N - i - (num_b - counter[i]))
		if(min_change>a):min_change=a
		#L_white R_black
		a = counter[i] + (N - i - (num_b - counter[i]))
		if(min_change>a):min_change=a

	return min_change



if __name__ == '__main__':
	N = int(input())
	S = input()
	print(solve(N,S))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
c = 0
a = 0
cw = [0] * N
cb = [0] * N
tw = 0
tb = 0
for i in range(N):
    if S[i] == '.':
        tw += 1
        cw[i] = tw
        cb[i] = tb
    else:
        tb += 1
        cb[i] = tb
        cw[i] = tw
A = cw[N - 1]
for i in range(N):
    A = min(A, cb[i] + cw[N - 1] - cw[i])
print(A)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
c = 0
a = 0
cw = [0] * N
cb = [0] * N
tw = 0
tb = 0
for i in range(N):
    if S[i] == '.':
        tw += 1
        cw[i] = tw
        cb[i] = tb
    else:
        tb += 1
        cb[i] = tb
        cw[i] = tw
A = cw[i]
for i in range(N):
    A = min(A, cb[i] + cw[N - 1] - cw[i])
print(A)

Submission status: Accepted

Solution code:

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

s = s.rstrip('#')
s = s.lstrip('.')

def seek(n, s):
    turn_to_black = s.count('.')
    current_min = turn_to_black
    turn_to_white = 0

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

Submission status: Accepted

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

s = s.rstrip('#')
s = s.lstrip('.')

if len(set(s)) <= 1:
    print(0)
    exit(0)

turn_to_black = s.count('.')
current_min = turn_to_black
turn_to_white = 0

for i in range(len(s)):
    if s[i] == '#':
        turn_to_white +=1
    else:
        turn_to_black -=1
    current_min = min(current_min, turn_to_black + turn_to_white)
print(current_min)

Submission status: Accepted

Solution code:
n = int(input())  # 数値入�?
str = list(input())  # 一�?字ずつ格�?
dotto = str.count(".")
ans = dotto
syapu = 0

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

    if dotto + syapu < ans:
        ans = dotto + syapu

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
c = []
c.append(S.count('.'))

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

  if S[i-1] == '.':
    c.append(c[i-1] - 1)   

print(min(c))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
tmp = 0
for i in range(N):
    if S[i] == '.': tmp += 1
minVal = tmp
#S[p] -> w
for p in range(N):
    if S[p] == '.': tmp -= 1
    else: tmp += 1
    minVal = min(minVal, tmp)
print(minVal)

Submission status: Accepted

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

import sys
import copy

sys.setrecursionlimit(1000000)

# input = sys.stdin.readline

# ~~~~~~~~~~~~~~~~~~~~~_(＾～＾ ｣ ∠)_~~~~~~~~~~~~~~~~~~~~~


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

W = S.count('.')
B = S.count('#')

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

w_p_dp, b_p_dp = [0] * (N + 1), [0] * (N + 1)
for i in range(N):
    w_p_dp[i + 1] = w_p_dp[i] + w_dp[i]
    b_p_dp[i + 1] = b_p_dp[i] + b_dp[i]

ans = 100000000000000000
for i in range(N + 1):
    w = b_p_dp[i]
    b = W - w_p_dp[i]
    ans = min(ans, w + b)

print(ans)

Submission status: Accepted

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

all_black_num = S.count('#')
all_white_num = N - all_black_num

if all_black_num == 0 or all_white_num == 0:
    ans = 0
else:
    left_black_num = 0
    left_white_num = 0
    right_white_num = all_white_num - left_white_num
    right_black_num = all_black_num - left_black_num
    ans = left_black_num + right_white_num
    for i, c in enumerate(S):
        if c == '#':
            left_black_num += 1
        elif c == '.':
            left_white_num += 1
        right_white_num = all_white_num - left_white_num
        right_black_num = all_black_num - left_black_num
        ans = min(ans, left_black_num + right_white_num)
        if right_black_num == N - i - 1:
            break
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
S.insert(0,0)
# b = S.count("#")
w = S.count(".")
black = 0
white = w
ans = 2e5
for i in range(N+1):
    if S[i] == "#":
        black += 1
    elif S[i] == ".":
        white -= 1
    # print(black,white)
    ans = min(black + white, ans)
print(ans)

Submission status: Accepted

Solution code:
_,s=open(0);a=[s.count('.')]
for i in s:a+=a[-1]+(i<'.'or-1),
print(min(a))

Submission status: Accepted

Solution code:
_,s=open(0);c=s.count('.');a=[c]
for i in s:c+=i<'.'or-1;a+=c,
print(min(a))

Submission status: Accepted

Solution code:
_=input()
S=input()
numc=0
numw=0
numcmin=0
for s in S:
    if s == '#':
        numc += 1
    else:
        numw += 1
        numc -= 1
        if numc < numcmin:
            numcmin = numc
print(numcmin+numw)

Submission status: Accepted

Solution code:
_=input()
S=input()
numc=0
numw=0
numcmin=0
for s in S:
    if s == '#':
        numc = numc + 1
    else:
        numw = numw + 1
        numc = numc - 1
        if numc < numcmin:
            numcmin = numc
print(numcmin + numw)

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

white_sum = white[N]

for i in range(1, N):
    a = white_sum - white[i]
    b = black[i]
    # a 境目より右領域にどれだけ白があるか，その白は黒に変更
    # b 境目より左領域にどれだけ黒があるか，その黒は白に変更
    cnt = a + b
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
_,S=input(),input()
numw=S.count('.')
numc,numcmin=0,0
for s in S:
    if s == '#':
        numc = numc + 1
    else:
        numc = numc - 1
        if numc < numcmin:
            numcmin = numc
print(numcmin+numw)

Submission status: Accepted

Solution code:
_,S=input(),input()
numc,numw,numcmin=0,0,0
for s in S:
    if s == '#':
        numc = numc + 1
    else:
        numw = numw + 1
        numc = numc - 1
        if numc < numcmin:
            numcmin = numc
print(numcmin + numw)

Submission status: Accepted

Solution code:
_ = input()
S = input()
numc = S.count('.')
numcmin = numc
for s in S:
    if s == '#':
        numc = numc + 1
    else:
        numc = numc -1
        if numc < numcmin:
            numcmin = numc
print(numcmin)

Submission status: Accepted

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

ans_all_black = 0   #全て黒くする方の解
ans_all_white = 0   #全て白くする方の解
ans_right_black = 100 #右をすべて黒くする解

#  #:黒
#  .:白
#黒の右隣に白が来ないようにしたい
#∴すべて黒
#　すべて白
#　白連続の後に黒連続
#のどれかが完成形

#一度白の全数を調べる
all_white = 0
left_white=[]

for i in range(N):
  if S[i]==".":
    all_white  = all_white + 1
    if i==0:
      left_white.append(1) 
    else:
      left_white.append(left_white[i-1]+1) 
  else:
    if i==0:
      left_white.append(0) 
    else:
      left_white.append(left_white[i-1])
    
ans_all_black = all_white     #全数黒にするには白をすべてひっくり返す
ans_all_white = N - all_white #全数白にするには黒をすべてひっくり返す＝すべての中で白出ないものをひっくり返す

#print("all_black:",ans_all_black)
#print("all_white:",ans_all_white)

ans_right_black = N

#右側全黒
for i in range(N):
  #print(i,":",i + 1 - left_white[i],":",ans_all_black - left_white[i])
  #i+1文字目において:左を全部白にするのに必要な塗り数:右を全部黒にするのに必要な塗り数
  ans_right_black = min(i + 1 - left_white[i] + ans_all_black - left_white[i] , ans_right_black)
  
ans = min(ans_all_white,ans_all_black,ans_right_black)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019/04/21
# Tenka1 Programmer Beginner Contest 2019 - C

# Input
n = int(input())
s = input()

bl = [0] * (n+1)
wl = [0] * (n+1)

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

# Calc minimum Left black and Right white
mins = 10**6
for i in range(0, n+1):
    wks = bl[i] + (wl[n] - wl[i])
    if mins > wks:
        mins = wks

# Output
print(mins)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
sm_w = [0]*(N+1)
sm_b = [0]*(N+1)
 
for i in range(N):
    s = S[i]
    sm_w[i+1] += sm_w[i]
    sm_b[i+1] += sm_b[i]
    if s == '.':
        sm_w[i+1] += 1
    else:
        sm_b[i+1] += 1
 
ans = 2**60
for i in range(N+1):
    left_b = 0
    if i!=0:
        left_b = sm_b[i]
    right_w = 0
    if i!=N:
        right_w = sm_w[N] - sm_w[i]
    ret = left_b + right_w
    ans = min(ans, ret)
print(ans)

Submission status: Accepted

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

sm_w = [0]*(N+1)
sm_b = [0]*(N+1)

for i in range(N):
    s = S[i]
    sm_w[i+1] = sm_w[i]
    sm_b[i+1] = sm_b[i]
    if s == '.':
        sm_w[i+1] += 1
    else:
        sm_b[i+1] += 1

ans = 2**60
for i in range(N+1):
    left_b = sm_b[i] - sm_b[0]
    right_w = sm_w[N] - sm_w[i]
    ret = left_b + right_w
    ans = min(ans, ret)
print(ans)

Submission status: Accepted

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

w_sum = [0]
b_sum = [0]
for i in range(len(s)):
    if s[i] == '.':
        w_sum.append(w_sum[-1] + 1) 
        b_sum.append(b_sum[-1])
    else:
        b_sum.append(b_sum[-1] + 1)
        w_sum.append(w_sum[-1])

assert len(w_sum) == len(b_sum)
assert len(w_sum) == len(s) + 1

minSum = 10 ** 5
for i in range(len(s)):
    minSum = min(minSum, b_sum[i] + (w_sum[-1] - w_sum[i+1]))
    
print(minSum)

# .##...####... ->
# ......#######