Problem:


Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

1 \leq N \leq 2\times 10^5

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0



Sample input:
3
#.#


Sample output:
1

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = [0]*(n+1)
count = 0
for i in range(len(s)) :
    if s[i] == "." :
        count += 1
    l[i+1] = count
siro = l[-1]
ans = 1000000000000000000000
for i in range(n+1):
    ans = min(ans,i-2*l[i]+siro)
print(ans)

Submission status: Accepted

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

MIN = N
lb = 0
rw = 0
for i in range(N):
	rw += 1 if S[i] == "." else 0

for i in range(N):
	MIN = min(lb+rw,MIN)
	lb += 1 if S[i] == "#" else 0
	rw -= 1 if S[i] == "." else 0
# Finalize
MIN = min(lb,MIN)
print(MIN)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
whites = S.count('.')
stones = [0, whites]
ans = sum(stones)
for char in S:
    if char == '.':
        stones[1] -= 1
    else:
        stones[0] += 1
    if ans > sum(stones):
        ans = sum(stones)
print(ans)

Submission status: Accepted

Solution code:
###
#main
if __name__ == "__main__":
    N = int(input())
    S = input()
#    count = 0
    countMin = 200001
    counttemp = 0
    isWhite = True
    numlist = []
    for t in S:
        if t == "#" and isWhite:
            isWhite = False
            numlist.append(counttemp)
            counttemp = 0
        if t == "." and not isWhite:
            isWhite = True
            numlist.append(counttemp)
            counttemp = 0
        counttemp += 1
    
    numlist.append(counttemp)
    if len(numlist) <= 2:
        countMin = 0
    else:
        countMin = sum(numlist[2::2])
        temp = countMin
        i = 2
        while i < len(numlist):
            temp += numlist[i-1] - numlist[i]
            countMin = temp if temp < countMin else countMin
            i += 2
#    print(numlist)           
    print(countMin)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
k = [s.count('.')]

for i in range(n):
  if s[i] == "#":
    k.append(k[i]+1)
  else:
    k.append(k[i]-1)
    
print(min(k))

Submission status: Accepted

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

r_list = []
lb = 0
rw = S.count(".")
r_list.append(lb + rw)
for i in range(N):
  if S[i] == "#":
    lb += 1
  else:
    rw -= 1
  r_list.append(lb + rw)
print(min(r_list))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = s.count('.')
num = ans
for c in s:
  if c == '.':
    num -= 1
  else:
    num += 1
  
  ans = min(ans,num)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

WHITE = '.'
BLACK = '#'

b_num = 0
w_num = S.count(WHITE)
ans = b_num + w_num
for v in S:
    if v == BLACK:
        b_num += 1
    else:
        w_num -= 1
    ans = min(ans, b_num + w_num)

print(ans)

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    S = list(input())
    # print(S)

    right_white = S.count('.')
    left_black = 0

    ans = min(S.count('.'), S.count('#'))
    for i in range(len(S)):
        if S[i] == '#':  # black
            left_black += 1
        else: # white
            right_white -= 1

        ans = min(ans, left_black + right_white)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = input()
s = list(input())
 
ans = len(s)

sharp = 0
dot = 0
for string in s:
	if(string == "#"):
		sharp += 1
	else:
		dot += 1 

left_sharp = 0
right_dot = dot

for i in range(len(s)):
	temp_ans = left_sharp + right_dot
	ans = min(ans,temp_ans)
	if(s[i] == "."):
		right_dot -= 1
	else:
		left_sharp += 1

ans = min(ans, len(s) - dot) 

print(ans)

Submission status: Accepted

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

c0 = [0] * n
c1 = [0] * n
for i in range(1, n):
  c0[i] = c0[i-1] + (s[i-1] == "#")
  c1[n-i-1] = c1[n-i] + (s[n-i] == ".")

min = n
for x, y in zip(c0, c1):
  if min > x + y:
    min = x + y
print(min)

Submission status: Accepted

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

b = 0
w = 0
for s in S:
  if s == '#':
    b += 1
  else:
    w += 1
dp = w
min = w
for s in S:
  if s == '.':
    dp -= 1
  else:
    dp += 1
    
  if dp < min:
    min = dp
print(min)

Submission status: Accepted

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

def calc():
    min_cost = n
    left_cost = 0
    right_cost = s.count('.')

    cost = left_cost + right_cost
    if cost < min_cost:
        min_cost = cost

    for i in range(n):
        if s[i] == '#':
            left_cost += 1
        else:
            right_cost -= 1
        cost = left_cost + right_cost
        if cost < min_cost:
            min_cost = cost

    return(min_cost)

print(calc())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
L = [0] * N
for i in range(N):
    if S[i] == "#":
        L[i] = 1
    else:
        L[i] = 0

leftblack = 0
rightwhite = len([x for x in L if x == 0])
ans = leftblack + rightwhite

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
LB = 0
RW = S.count(".")
ans = LB +RW
for x in range(N):
    if S[x] == "#":
        LB += 1
    else:
        RW -= 1
    count = LB +RW
    if count < ans:
        ans = count
print(ans)

Submission status: Accepted

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

white_num_r = len([val for val in S if val == "."])
black_num_l = 0

min_val = white_num_r
for val in S:
	if val == ".":
		white_num_r -= 1
	else:
		black_num_l += 1
	new_val = white_num_r + black_num_l
	min_val = min(new_val,min_val)

print(min_val)

Submission status: Accepted

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

white = 0 #にしなきゃ
black = 0 #にしなきゃ
for i in range(len(Sarray)):
    if Sarray[i] == '.':
        black += 1

a = [white + black]
for i in range(len(Sarray)):
    if Sarray[i] == '.':
        black -= 1
    else:
        white += 1
    a.append(white + black)
a.sort()
print(a[0])

Submission status: Accepted

Solution code:
def c_stones(N, S):
    black_left_of_the_boundary, white_right_of_the_boundary = 0, S.count('.')
    ans = black_left_of_the_boundary + white_right_of_the_boundary
    for c in S:
        if c == '#':
            black_left_of_the_boundary += 1
        else:
            white_right_of_the_boundary -= 1
        ans = min(ans, black_left_of_the_boundary + white_right_of_the_boundary)
    return ans

N = int(input())
S = input()
print(c_stones(N, S))

Submission status: Accepted

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

num = []
first = s[0]
end = s[-1]
pre = first
count = 0
for c in s:
    if c == pre:
        count += 1
    else:
        num.append(count)
        count = 1
        pre = c
num.append(count)

if len(num) == 1:
    print(0)
else:
    b = 0
    w = n
    offset = 0
    if first == '#':
        offset = 1
    w = sum(num[offset::2])
    minlist = [b + w]
    for i in range(len(num)):
        if i % 2 == offset:
            w -= num[i]
        else:
            b += num[i]
        minlist.append(b + w)
    print(min(minlist))

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()

l = []
now = '.'
count = 0
for i in range(n):
  if ( s[i] != now ):
    l.append(count)
    now = s[i]
    count = 0
  count += 1

if ( count > 0 ):
  l.append(count)

l.append(0)

#print(l)

ans = 99999999999
left = 0
right = 0

for j in range(int((len(l)-1)/2)):
  right += l[(j+1)*2]

for ii in range(int(len(l)/2)):
  i = ii*2

  if( i!=0):
    right -= l[i]

  count = 0
  if ( i!=0):
    left += l[i-1]
  
  count = left + right
  #print(i, count, left, right)
  


  if (ans>count):
    ans = count

if (ans == 99999999999):
  ans = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
s=input()
black=0
write=s.count(".")
koho=deque([write,s.count("#")])
for i in s:
    if i==".":
        write-=1
    else:
        black+=1
    koho.append(write+black)
print(min(koho))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

ss = '#' * n
ret = 0
for j in range(n):
  if s[j] != ss[j]:
    ret += 1

cnt = ret
for j in range(n):
  if s[j] == '.':
    cnt -= 1
  else:
    cnt += 1
  if cnt < ret:
    ret = cnt

print(ret)

Submission status: Accepted

Solution code:
def get_cost_change_black(S):
    return S.count(".")

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

indexs = [i for i, x in enumerate(S) if x == "#"]
if len(indexs) == 0:
    print(0)
else:
    answer = 10000000000
    change_white = 0
    change_black = get_cost_change_black(S[indexs[0]:])
    for i, ind in enumerate(indexs):
        x = change_white + change_black
        if x < answer:
            answer = x
        change_white = change_white + 1
        if i == len(indexs) - 1:
            if change_white < answer:
                answer = change_white
            break
        change_black = change_black - (indexs[i+1] - indexs[i] - 1)
    print(answer)

Submission status: Accepted

Solution code:
from numpy import cumsum

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

# 黒を1,白を0として　リスト化
ss = S.replace("#", "1").replace(".", "0")
* ls, = map(int, ss)

# 累積和
cum_ls = cumsum(ls)

# 左から何個目に初めてblackが出てくるか
first_black = -1
for i in range(N):
    if S[i] == "#":
        first_black = i
        break

# blackが後ろから何個まで続いているか
last_black = N
for i in reversed(range(N)):
    if S[i] == "#":
        last_black = i
    else:
        break
if first_black == -1:
    print(0)
else:
    # i番目より左をすべて白、それより右をすべて黒にしたとき何回塗り替えるか
    ans_temp = 10 ** 10
    for i in range(N):
        ans_temp = min(ans_temp, cum_ls[i] +
                       (N-(i+1) - (cum_ls[-1] - cum_ls[i])))

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

    print(ans)

Submission status: Accepted

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

import sys
input = sys.stdin.readline

def io_generator():
	return input()

#+++++++++++++++++++
	
def main(io):
	n=int(io())
	s=io()[:n]
	
	nw=len([c for c in list(s) if c=='.'])
	ret=min(nw, n-nw)
	cnw=0
	cnb=0
	nnw=nw
	nnb=n-nw
	for i in list(s):
		if i=='.':
			cnw+=1
			nnw-=1
			ret = min(cnb+nnw, ret)
		if i =='#':
			cnb+=1
			nnb-=1
			ret = min(cnb+nnw, ret)
	return ret

#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
c1 = S.count("#")
c2 = S.count(".")
r1 = [0] * N
r2 = [0] * (N + 1)
if S[0] == "#":
    r1[0] = 1
for i in range(1, N):
    if S[i] == "#":
        r1[i] = r1[i - 1] + 1
    else:
        r1[i] = r1[i - 1]
r2[N - 1] = 1 * (S[N - 1] == ".")
r2[N] = r2[N - 1]
for i in range(N - 2, -1, -1):
    if S[i] == ".":
        r2[i] = r2[i + 1] + 1
    else:
        r2[i] = r2[i + 1]
res = N
for i in range(N):
    res = min(res, r1[i] + r2[i + 1])
print(min(res, c1, c2))

Submission status: Accepted

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

def readInts():
  return [int(s) for s in input().split(" ")]

def main():
  N = readInts()[0]
  S = input()

  st = N
  for i in range(N):
    if S[i] != '.':
      st = i
      break
  ed = 0
  for i in range(N):
    if S[N - 1 - i] != '#':
      ed = N - i
      break

  if ed <= st:
    print(0)
    return

  nWhite = 0
  for i in range(st, ed):
    if S[i] == '.':
      nWhite += 1

  countBlack = 0
  countWhite = 0
  ans = N
  for i in range(st, ed):
    w = nWhite - countWhite
    b = countBlack
    ans = min(ans, w + b)
    if S[i] == '#':
      countBlack += 1
    else:
      countWhite += 1
  ans = min(ans, countBlack)
  print(ans)

if __name__ == "__main__":
  main()

Submission status: Accepted

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

ans = S.count(".")

printnum = ans

for c in S:
    if c == ".":
        printnum -= 1
    else:
        printnum += 1
    ans = min(ans,printnum)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [1 if c == '#' else 0 for c in input()]
l = [0] * (n + 1)
l2 = [0] * (n + 1)
for i in range(n):
    l[i + 1] = s[i] + l[i]
s = s[::-1]
for i in range(n):
    l2[i + 1] = 1-s[i] + l2[i]
l2 = l2[::-1]
ans = float('inf')
for i in range(n + 1):
    ans = min(ans, l[i] + l2[i])
print(ans)

Submission status: Accepted

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

A = [None]*(N+1)
A[0] = 0
cnt = 0
for i,s in enumerate(S[::-1]):
    if s=='.':
        A[i+1] = A[i]+1
    else:
        A[i+1] = A[i]-1
        cnt += 1
print(min(A)+cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
temp = []
c = 0
pitem = ''
s2 = '.' + s + 'a'
for item in s2:
    if pitem == '' or item == pitem:
        c += 1
        pitem = item
    if item != pitem:
        temp.append(c)
        c = 1
        pitem = item
        continue

# print(temp)
# print(temp2)

t = s2.count('.')
c = 0
c2 = 0
cs = []
for i in range(len(temp)):
    c2 += temp[i]
    if i % 2 == 1:
        c += temp[i]
    cs.append(c + t - (c2 - c))
print(min(cs))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
c1 = S.count("#")
c2 = S.count(".")
r1 = [0] * N
r2 = [0] * (N + 1)
r1[0] = 1 * (S[0] == '#')
for i in range(1, N):
    if S[i] == "#":
        r1[i] = r1[i - 1] + 1
    else:
        r1[i] = r1[i - 1]
r2[N - 1] = 1 * (S[N - 1] == ".")
r2[N] = r2[N - 1]
for i in range(N - 2, -1, -1):
    if S[i] == ".":
        r2[i] = r2[i + 1] + 1
    else:
        r2[i] = r2[i + 1]
res = N
for i in range(N):
    res = min(res, r1[i] + r2[i + 1])
print(min(res, c1, c2))

Submission status: Accepted

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

W = [0] * n
B = [0] * n

for i in range(n):
    if i == 0:
        if s[-1] == '.':
            W[-1] = 1
    elif s[-i-1] == '.':
        W[-i-1] = W[-i] + 1
    else:
        W[-i-1] = W[-i]

#print(W)

for i in range(n):
    if i == 0:
        B[i] = 0
    elif s[i-1] == '#':
        B[i] = B[i-1] + 1
    else:
        B[i] = B[i-1]

#print(B)

if W[0] == n:
    print(0)
else:
    ans = n
    for i in range(n):
        c = W[i] + B[i]

        ans = min(ans, c)

    ans = min(ans, s.count('#'))

    print(ans)

Submission status: Accepted

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

w=0
b=0
for i in range(n):
    if s[i]=='.':
        w+=1
    else:
        b+=1

dp=w
min=w
for i in range(n):
    if s[i]=='.':
        dp-=1
    else:
        dp+=1
    if dp<min:
        min=dp
print(min)

Submission status: Accepted

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

Submission status: Accepted

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

lb = [0]*len(s)
lw = [0]*len(s)

sb = 0
sw = 0
min = len(s)

for i in range(len(s)):
    if s[i] == '#':
        sb += 1
    if s[len(s)-(i+1)] == '.':
        sw += 1
    lw[len(s)-(i+1)] = sw
    lb[i] = sb

lb.insert(0,0)
lw.append(0)
#
# print(lb)
# print(lw)

for i in range(len(s)+1):
    if lb[i]+lw[i] < min:
        min = lb[i]+lw[i]

print(min)

Submission status: Accepted

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
s = list(ns())
count1 = 0
count2 = 0
lis1 = [0 for i in range(n)]
lis2 = [0 for i in range(n)]
for i in range(len(s)):
    if s[i] == '.':
        count1 += 1
        lis1[i] = count1
    if s[i] == '#':
        count1 -= 1
        lis1[i] = count1
idx1 = lis1.index(max(lis1))
for i in range(len(s)-1, -1, -1):
    if s[i] == '#':
        count2 += 1
        lis2[i] = count2
    if s[i] == '.':
        count2 -= 1
        lis2[i] = count2
idx2 = lis2.index(max(lis2))
ans1 = s[:idx1 + 1].count('#') + s[idx1 + 1:].count('.')
ans2 = s[:idx2].count('#') + s[idx2:].count('.')
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
tmp = S.count('.')
mini = tmp
def judge(k):
  return bool(S[k]=='.')
def check(k):
  return 1-2*judge(k)
for i in range(N):
  tmp += check(i)
  if tmp < mini:
    mini = tmp
print(mini)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 998244353
inf = float('INF')

#A
def A():
    a, b, c = LI()
    if b < a:
        a,b = b,a
    if a <= c <= b:
        print("Yes")
    else:
        print("No")
    return

#B
def B():
    n = II()
    s = input()
    a = list(set(list(s)))
    k = II()-1
    for i in a:
        if s[k] != i:
            s = s.replace(i, "*")
    print(s)
    return

#C
def C():
    n = II()
    s = S()
    dp = [0 for i in range(n)]
    a, b = 0, 0
    for num,i in enumerate(s):
        if i == "#":
            dp[num] += 1
    for i in range(1, n):
        dp[i] += dp[i - 1]
    ans = min(s.count("#"),s.count("."))
    shap = s.count("#")
    for i in range(n):
        a = dp[i] + (n - (i+1) - dp[-1] + dp[i])
        ans = min(ans, a)
    print(ans)


    """
    dp = []
    bf = s[0]
    del s[0]
    a = 
    flg = False
    if bf == ".":
        flg = True
    for i in s:
        if i == bf:
            a += 1
        else:
            dp.append(a)
            a = 1
            bf = i
    dp.append(a)
    a, b = 0, 0
    if flg:
        dp[0] = 0
    if len(s) and s[-1] == "#":
        dp[-1] = 0
    for d in range(len(dp) - 2, -1, -2):
        a += dp[d]
    for d in range(len(dp) - 1, -1, -2):
        b += dp[d]
    print(dp)
    print(min(a,b))
    """
    return

#D
def D():
    n = II()
    ai = IR(n)
    ai.sort()
    ruiseki = [0 for i in range(n)]
    for num, a in enumerate(ai):
        ruiseki[num] = a + ruiseki[num-1]
    select = list(itertools.combinations(range(n), 3))
    ans = 0
    for a,b,c in select:
        A = ruiseki[a]
        B = ruiseki[b] - ruiseki[a]
        C = ruiseki[c] - ruiseki[b]
        if A + B > C and B + C > A and A + C > B:
            ans += 6
            ans = ans % mod
    print(ans)
    
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

Solution code:
N = int(input())
S = [1 if x == '.'  else 0 for x in list(input())]

cost = sum(S)
min_cost = cost 

for i in range(N):
    if S[i] == 0:
        cost += 1
    else:
        cost -= 1

    if min_cost > cost:
        min_cost = cost
        
print(min_cost)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
S = list(input())

tmp = S.count("#")
tmp2 = len(S)-tmp
num = [0]*N
num_sharp = 0
num_peri = 0
for i in range(len(S)):
	if S[i] == "#":
		num_sharp += 1
	else:
		num_peri += 1
	num[i] = num_sharp + (len(S)-(i+1))-(tmp-num_sharp)
# print(num)
print(min(tmp2, min(num)))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
dc = S.count(".")
sc = S.count("#")
dcn = 0
scn = 0
c = 0
count = dc
ans = dc
for i, s in enumerate(S):
    if s == ".":
        dcn += 1
        count -= 1
    if s == "#":
        scn += 1
        count += 1
    ans = min(ans, count)
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop


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

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

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


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

N = int_raw()
S= input()

dp = {}

def rec(ss):
    return 0

def main():
    global S
    ans = INF
    numB = [0]*len(S)
    
    for i in range(len(S)):
        if i > 0:
            numB[i]=numB[i-1]
        if S[i]=="#":
            numB[i]+=1
    if numB[len(S)-1]==0 or numB[len(S)-1]==len(S):
        return 0        
    for i in range(len(S)):
        buf = numB[i]+len(S)-1-i-(numB[len(S)-1]-numB[i])
        ans = min(ans, buf)
    ans = min(ans, len(S)-numB[len(S)-1])
    ans = min(ans, numB[len(S)-1])
    return ans



print(main())

Submission status: Accepted

Solution code:
n = int(input())
s = input()
li = []
for i in range(n):
    if s[i] == "#":
        li.append(i)
li.append(len(s))
sum = 0
if len(li) == 1:
    print(0)
else:
    for i in range(len(li)-1):
        diff = li[i+1] - li[i] - 1
        if sum + diff >= i+1:
            sum = i+1
        else:
            sum += diff
    print(sum)

Submission status: Accepted

Solution code:
# Tenka1 Programmer Beginner Contest 2019
# https://atcoder.jp/contests/tenka1-2019-beginner
# 
# 

import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def grouping(N, S):
    g = []
    s = S[0]
    c = 1
    for i in range(1, N):
        if S[i] == s:
            c += 1
        else:
            g.append((s, c))
            c = 1
            s = S[i]
    g.append((s, c))
    return g

def main(N, S):
    g = grouping(N, S)
    gl = len(g)
    l = 0
    r = gl - 1
    if g[l][0] == '.':
        l += 1
    if g[r][0] == '#':
        r -= 1
    if r - l < 1:
        print(0)
        return
    ll = [0]
    lr = [0]
    lc = 0
    for i in range(l, r, 2):
        ll.append(g[i][1] + ll[-1])
        lr.append(g[i+1][1] + lr[-1])
        lc += 1
    ans = N
    for i in range(lc+1):
        tl = ll[i]
        tr = lr[lc] - lr[i]
        ans = min(ans, tl + tr)
    print(ans)

N = i2n()
S = i2s()
main(N, S)

Submission status: Accepted

Solution code:


def main():
    N = int(input())
    data = list(input())

    righ_siro = data.count('.')
    righ_kuro = data.count('#')

    ans = min(righ_kuro, righ_siro)

    left_kuro = 0
    left_shiro = 0

    for i in range(N):
        if data[i] == '.':
            left_shiro +=1
            righ_siro -= 1
        else:
            left_kuro += 1
            righ_kuro -= 1

        kari = left_kuro + righ_siro

        if ans > kari:
            ans = kari

    print(ans)






if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    S = input()
    aw = S.count('.')
    ab = S.count('#')
    w = S.count('.')
    b = 0
    ans = float('INF')
    for s in S:
        if s == '.':
            w -= 1
        if s == '#':
            b += 1
        ans = min(aw, ab, w + b, ans)
    print(ans)


main()

Submission status: Accepted

Solution code:
from collections import deque

_ = input()
S = input()

que = deque([])

flag = False # White
tmp = 0

for s in S:
    if flag:
        if s == "#":
            tmp += 1
        else:
            que.append(tmp)
            tmp = 1
            flag = not flag
    else:
        if s == ".":
            tmp += 1
        else:
            que.append(tmp)
            tmp = 1
            flag = not flag

que.append(tmp)

if not flag:
    que.append(0)

cost = 0
fe_cost = []
fe_cost.append(cost)
flag = True
for q in que:
    if flag:
        flag = not flag
    else:
        cost += q
        fe_cost.append(cost)
        flag = not flag
fe_cost.pop()

cost = 0
be_cost = []
be_cost.append(cost)
flag = True
for q in reversed(que):
    if flag:
        flag = not flag
    else:
        cost += q
        be_cost.append(cost)
        flag = not flag
be_cost.pop()
be_cost.reverse()
fe_cost = list(fe_cost)
be_cost = list(be_cost)

cost = [x + y for (x, y) in zip(fe_cost, be_cost)]
print(min(cost))

Submission status: Accepted

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

S_r = S[::-1]
w_num = []
b_num = []
w = 0
b = 0
for i in range(N):
  if S[i] == '.':
    w += 1
  w_num.append(w)
  if S_r[i] == '#':
    b += 1
  b_num.append(b)
b_num = b_num[::-1]
ans = [N-w_num[i]-b_num[i] for i in range(N)]
print(min(ans))

Submission status: Accepted

Solution code:
# coding:utf-8

def main():
    N = int(input())
    s = input()
    count1 = 0
    count2 = 0
    count3 = 0
    temp = ""
    patern3_white = [0]*N
    patern3_black = [0]*N
    patern3 = []
    if (not "." in s) or (not "#" in s):
        print(0)
        return

    for i in s:
        if i == ".":
            count1 += 1
        elif i == "#":
            count2 += 1

    for i in range(N-1):
        if i == 0:
            if s[0] == "#":
                patern3_white[0] = 1
                patern3_black[0] = count1
            else:
                patern3_white[0] = 0
                patern3_black[0] = count1-1
                
            patern3.append(patern3_white[0] + patern3_black[0])
        else :
            if s[i] == "#":
                patern3_white[i] = patern3_white[i-1]+1
                patern3_black[i] = patern3_black[i-1]
            else :
                patern3_white[i] = patern3_white[i-1]
                patern3_black[i] = patern3_black[i-1]-1
            patern3.append(patern3_white[i]+patern3_black[i])
    # print(patern3_black)
    # print(patern3_white)
    # print(patern3)
    # print()
    count3 = min(patern3)
    # print(count1)
    # print(count2)
    # print(count3)
    print(min(count1,count2,count3))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def solve():
  N = int(input())
  S = input()
  stones = []
  look = S[0]
  cnt = 0

  if S[0] == '.': stones.append(0)
  for s in S:
    if look == s: cnt += 1
    else:
      stones.append(cnt)
      cnt = 1
      look = s
  stones.append(cnt)
  if S[N - 1] == '#': stones.append(0)

  count = sum(stones[1::2])
  ans = count

  for i in range(0, len(stones), 2):
    count = count + stones[i] - stones[i + 1]
    if ans > count: ans = count
  return ans

print(solve())

Submission status: Accepted

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

# 最初の黒以降の白全部置き換える
ans1 = 0
found = False
for c in S:
    if c == '.' and found:
        ans1 += 1
    if c == '#':
        found = True

# 最後の白より前の黒を全部置き換える
ans2 = 0
found = False
for c in reversed(S):
    if c == '#' and found:
        ans2 += 1
    if c == '.':
        found = True

bcounts = [0 for _ in range(len(S))]
wcounts = [0 for _ in range(len(S))]
cs = 0
for i in range(len(S)):
    if S[i] == '#':
        cs += 1
    bcounts[i] = cs
cs = 0
for i in reversed(range(len(S))):
    if S[i] == '.':
        cs += 1
    wcounts[i] = cs

INF = float('inf')
ans = INF
for i in range(len(S) - 1):
    if S[i] == '#' and S[i + 1] == '.':
        # 次の黒を超えたところの wcounts
        ni = i + 1
        while ni < len(S) and S[ni] == '.':
            ni += 1
        while ni < len(S) and S[ni] == '#':
            ni += 1
        if ni < len(S):
            wc = wcounts[ni]
        else:
            if wcounts[-1] == '.':
                wc = 1
            else:
                wc = 0
        ans = min(ans, bcounts[i] + wc)

print(min(ans1, ans2, (0 if ans == INF else ans)))

Submission status: Accepted

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

b_sum = []
w_sum = []
b_num = 0
w_num = 0

#b_sum:i番目まで黒が何個か
#w_sum:i番目まで白が何個か

for i in range(n):
    if s[i] == "#":
        b_num = b_num + 1
    else:
        w_num = w_num + 1
    b_sum.append(b_num)
    w_sum.append(w_num)

#i番目まで白になるときは何個ひっくり返す？

ans = []

for i in range(n):
    ans.append(b_sum[i] + w_sum[n - 1] - w_sum[i])
ans.append(w_sum[n - 1])

print(min(ans))