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: Wrong Answer

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

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
ans = 0
last = False
for i in s:
    if i == "#":
        last = True
    else:
        if last:
            ans += 1
        else:
            last = False
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
print(min(S.count('.'),S.count('#')))

Submission status: Wrong Answer

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

for i,c in enumerate(S):
    if c == "#":
        break
print(S[i+1:].count("."))

Submission status: Wrong Answer

Solution code:
import queue


N = int(input())
S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]

ch = False
ans = 0
for s in S:
	if s == "#":
		ch = True
	elif ch and s == ".":
		ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
k = s.find('#')
if k == -1:
    k = len(s)
ans = 0
for i in range(k, len(s)):
    if s[i] == '.':
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
i=s.find('#')
if i>=0:
    print(s[i:].count('.'))
else:
    print(0)

Submission status: Wrong Answer

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

now = False
cnt = 0
for c in s:
	if c=='.':
		if now:
			cnt += 1
	else:
		if not(now):
			now = True

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
print(min(s.count("."), s.count("#")))

Submission status: Wrong Answer

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

count = 0
start = False
for i in S:
    if not start:
        if i == '#':
            start = True
    else:
        if i == '.':
            count += 1

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input()
ans = 0
for i in range(1, N):
    if s[i] == "#":
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, S: str):
    ret = 0
    started = False
    for c in S:
        if c == '#':
            started = True
        if started and c == '.':
            ret += 1
    print(ret)
    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: Wrong Answer

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

ans = 0

flag = False
for i in range(n):
    if s[i]=='#':
        flag = True
    if flag and s[i]=='.':
        ans += 1

print(ans)

Submission status: Wrong Answer

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

cnt = 0

for i in range(N-1):
    if S[i] ==  '#' and S[i+1] == '.':
        cnt += 1 


print(cnt)

Submission status: Wrong Answer

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

ans = 0
black_found = False
for ch in s:
    if ch == "#":
        black_found = True
    if black_found and ch == ".":
        ans += 1
print(ans)

Submission status: Wrong Answer

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

flag = False
cnt = 0
for i in S:
  if i == ".":
    if flag:
      cnt += 1
  else:
    flag = True

print(cnt)

Submission status: Wrong Answer

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
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#C
def C():
    n = I()
    s = S()
    ans = 0
    for i in range(n-1):
        if s[i] == "#" and s[i+1] == ".":
            ans += 1
            s[i+1] = "#"
    print(ans)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return


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

Submission status: Time Limit Exceeded

Solution code:
a = input()
b = input()
c = list(b)
d = 0
e = []
for i in range(len(c)+1):  #左からi個を白にする
    if i == 0: #全て黒に変える場合
        e.append(c.count("."))
        ans = e[0]
    else:
        if c[i-1] == ".":
            d = d + 1
        e.append(((i - d) + (c.count(".") - d)))
        if ans >= e[i]:
            ans = e[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
li=[]
for i in range(n+1):
    g=len(s[:i].replace(".",""))+len(s[i:].replace("#",""))
    li.append(g)
li.sort()
print(li[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
m=10000
for i in range(n+1):
    g=len(s[:i].replace(".",""))+len(s[i:].replace("#",""))
    if m>g:
        m=g
print(m)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

b = (np.array(list(S)) == '#')
w = ~b[::-1]

cumb = b.cumsum()
cumw = w.cumsum()

posb = np.arange(N)[b]
posw = np.arange(N)[w]

impb = np.diff(posw)
impw = np.diff(posb)

ans = 0
while True:
    if (np.minimum(cumb, cumw[::-1])==0).all():
        break

    if len(impw) == 0 or len(impb) == 0:
        ans += 1
        break

    
    if impw[0] < impb[0]:
        impw = impw[1:]
        cumw = np.maximum(cumw-1, 0)
    else:
        impb = impb[1:]
        cumb = np.maximum(cumb-1, 0)

    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min = S[0:N].count('#')
for i in range(0, N):
    if min > S[0:i + 1].count('#') + S[i + 1:N].count('.'):
        min = S[0:i + 1].count('#') + S[i + 1:N].count('.')
if N == 1:
    min = 0
print("{}".format(min))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
a=0
for i in range(1,n):
    if s[i-1]=='#'and s[i]=='.':
        j=i
        k=i-1
        while j+1<n and s[j+1]=='.':j+=1
        while k>0and s[k-1]=='#':k-=1
        if j-i+1<i-k:
            a+=j-i+1
            s[i:j+1]=['#']*(j-i+1)
        else:
            a+=i-k
            s[k:i]=['.']*(i-k)
print(a)

Submission status: Time Limit Exceeded

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

l = []
for i in range(n):
    if i == 0:
        l.append(1)
        continue
    if s[i] == s[i-1]:
        l[-1]+=1
    else:
        l.append(1)

if s[0]=='.':
    l = l[1:]
if s[-1]=='#':
    l = l[:-1]

bw = sum(l[1::2])

for i in range(len(l)//2+1):
    bw = min(bw, sum(l[:2*i+1:2])+sum(l[2*i+1::2]))

print(bw)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
stones = input()
# stones = "...."


def count_painted_stones(stones, length):
    repainted_times = []
    for i in range(length+1):
        left_stones = stones[:i]
        left_black_stones = left_stones.count("#")
        right_stones = stones[i:]
        right_white_stones = right_stones.count(".")
        repainted_stones = left_black_stones + right_white_stones
        repainted_times.append(repainted_stones)

    return min(repainted_times)


print(count_painted_stones(stones, len(stones)))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
a=0
for i in range(1,n):
    if s[i-1]=='#'and s[i]=='.':
        j=i
        k=i-1
        while j+1<n and s[j+1]=='.':j+=1
        while k>0and s[k-1]=='#':k-=1
        if j-i+1<=i-k:
            a+=j-i+1
            s[i:j+1]=['#']*(j-i+1)
        else:
            a+=i-k
            s[k:i]=['.']*(i-k)

print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
s = [int(i=='#') for i in input()]

idx = [i for i, x in enumerate(s)
       if x==1]

if len(idx)==0 or len(idx)==n:
  print(0)
  
else:  
  s = np.array(s)

  ans = []

  for i in idx:
    s_ = np.array([0]*(i) + [1]*(n-i))
    ans.append((s!=s_).sum())

  print(np.min(ans))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
a=0
for i in range(1,n):
    if s[i-1]=='#'and s[i]=='.':
        j=i
        k=i-1
        while j+1<n and s[j+1]=='.':j+=1
        while k>0and s[k-1]=='#':k-=1
        if j-i+1<=i-k:
            a+=j-i+1
            j=i
            s[j]='#'
            while j+1<n and s[j+1]=='.':
                s[j+1]='#'
                j+=1
        else:
            a+=i-k
            k=i-1
            s[k]='.'
            while k>0and s[k-1]=='#':
                s[k-1]='.'
                k-=1

print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for j in range(1, N+1):
    if S[-j] == '#':
        j += 1
    else:
        break

if j == 1:
    last = 0
else:
    last = j-1
i = 0
while i < N:
    if S[i:i+2] == '#.':
        ans += 1
        new_S = S[:i+1] + '#' + S[i+2:]
        S = new_S
    i += 1

print(min(ans, N-ans-last))

Submission status: Time Limit Exceeded

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

a = []
pc = "."
pi = 0
for i,c in enumerate(S):
    if pc!=c:
        a.append(i-pi)
        pi = i
        pc = c
else:
    a.append(i+1-pi)

ans = N
l = len(a)
for i in range(0,(l+1)//2):
    ans = min(ans,sum(a[1:1+i*2:2])+sum(a[2+i*2::2]))
print(ans)

Submission status: Time Limit Exceeded

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

count = 0
no_detect = True
while no_detect:
    len_c = 0
    for i in range(len(S)-1):
        gram_2 = S[i:i+2]
        if gram_2 == "#.":
            len_c += 1
            count += 1
            str_list = list(S)
            if S[i-1] == ".":
                str_list[i+1] = "."
            else:
                str_list[i+1] = "#"
            S = "".join(str_list)

    if len_c > 0:
        no_detect = False

print(count)

Submission status: Time Limit Exceeded

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

cand = []

for i in range(N+1):
    st = '.'*i + '#'*(N-i)
    temp = 0
    for s, t in zip(S, st):
        if s != t:
            temp += 1
    cand.append(temp)
    
print(min(cand))

Submission status: Time Limit Exceeded

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

length = []
cnt = 0
now = "#"

for i in S:
  if now == i:
    cnt += 1
  else:
    if now == "#":
      now = "."
    else:
      now = "#"
    length.append(cnt)
    cnt = 1
else:
  length.append(cnt)
  
if len(length) % 2 == 0:
    pass
else:
    length.append(0)
  
ans = 0
for i,v in enumerate(length[0::2]):
    ans += min(v,length[1::2][i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

"""
N = int(input())
S = input()
def main():
    
    BW_cnt= 0
    WB_cnt= 0
    tmpS = S
    BW_idx = find_index(tmpS, "#.", default=-1)
    while BW_idx != -1 and BW_idx!=N:
        tmpS=tmpS.replace("#.", "##", 1)
        BW_idx = find_index(tmpS, "#.", default=-1)
        BW_cnt+=1
    tmpS = S
    WB_idx = find_index(tmpS, "#.", default=-1)
    while WB_idx != -1 and WB_idx!=N:
        tmpS=tmpS.replace("#.", "..", 1)
        WB_idx = find_index(tmpS, "#.", default=-1)   
        WB_cnt+=1
    print(min(BW_cnt, WB_cnt))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for j in range(1, N+1):
    if S[-j] == '#':
        j += 1
    else:
        break

if j == 1:
    last = 0
else:
    last = j-1
i = 0
while i < N:
    if S[i:i+2] == '#.':
        ans += 1
        S = S[:i+1] + '#' + S[i+2:]
    i += 1

print(min(ans, N-ans-last))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
x = s.count("#")
lst = [s[:i].count("#")+s[i+1:].count(".") for i in range(n)]
x = min(x,min(lst))
print(x)

Submission status: Time Limit Exceeded

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

for j in range(1, N+1):
    if S[-j] == '#':
        j += 1
    else:
        break

if j == 1:
    last = 0
else:
    last = j-1
i = 0
while i < N:
    if S[i:i+2] == '#.':
        ans += 1
        S = S[:i+1] + '#' + S[i+2:]
    i += 1

print(min(ans, N-ans-last))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
w = [0]*N
b = [0]*N

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

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

print(m)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

"""
N = int(input())
S = input()
def main():
    
    BW_cnt= 0
    WB_cnt= 0
    tmpS = S
    BW_idx = bisect.bisect_left(tmpS, "#.")
    while BW_idx != 0 and BW_idx!=N:
        tmpS=tmpS.replace("#.", "##", 1)
        BW_idx = bisect.bisect_left(tmpS, "#.")
        BW_cnt+=1
    tmpS = S
    WB_idx = bisect.bisect_left(tmpS, "#.")
    while WB_idx != 0 and WB_idx!=N:
        tmpS=tmpS.replace("#.", "..", 1)
        WB_idx = bisect.bisect_left(tmpS, "#.")   
        WB_cnt+=1
    print(min(BW_cnt, WB_cnt))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 998244353

#C
def C():
    n = I()
    s = S()
    ans = float("inf")
    for i in range(n+1):
        m = 0
        for j in range(i):
            if s[j] == "#":
                m += 1
        for j in range(i,n):
            if s[j] == ".":
                m += 1
        ans = min(ans,m)
    print(ans)
    return

#D
def D():
    n = I()
    a = IR(n)

    return

#E
def E():
    return

#F
def F():
    return


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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
stones = [0] + [0 if x == "." else 1 for x in list(input())] + [1]

min_num = 10 ** 6

for i in range(1, n + 2):
	tmp = sum(stones[:i]) + (len(stones[i:]) - sum(stones[i:]))
	#print(tmp, sum(stones[:i]), (len(stones[i:]) - sum(stones[i:])))
	min_num = min(min_num, tmp)

print(min_num)

Submission status: Time Limit Exceeded

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

ans = 0
while True:
    ans += S.count("#.")
    S = S.replace("#.", "")
    if S == S.replace("#.", ""):
        print(ans)
        break

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict as ddic, deque
from itertools import combinations, permutations, product
import sys
sys.setrecursionlimit(10**6)
stdout = sys.stdout
rr = lambda: raw_input().strip()
rri = lambda: int(raw_input())
rrm = lambda: list(map(int, raw_input().strip().split()))


def solve(N, A):
    memo = {}
    def dp(i, color):
        if i == N: return 0
        if (i, color) in memo:
            return memo[i, color]
        if color == 1:  # black
            # no white
            ans = dp(i+1, 1) + (A[i] != color)
        else:
            ans = min(dp(i+1, 0), dp(i+1, 1)) + (A[i] != color)
        memo[i, color] = ans
        return ans

    return min(dp(0, 0), dp(0, 1))
###
N = rri()
S = rr()
A = [1 if c == '#' else 0 for c in S]
print solve(N, A)

Submission status: Time Limit Exceeded

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

answers = []

answers.append(S.count('.'))
answers.append(S.count('#'))

for i in range(1,N):
  answers.append(S[:i].count('#') + S[i:].count('.'))
  
answer = min(answers)
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
bl=0 ; wh=s.count(".")
ans=s.count("#")

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

Submission status: Accepted

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

#ans = S.count('#')

cum1=[0]
cnt=0
for i in range(N):
  if S[i]=='#':
    cnt += 1
  cum1.append(cnt)
  
cum2=[0]
cnt=0
for s in reversed(S):
  if s=='.':
    cnt += 1
  cum2.append(cnt)
  
cum2 = cum2[::-1]

print(min([cum1[i]+cum2[i] for i in range(N+1)]))

Submission status: Accepted

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

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

ans = white
b = 0
w = white
for e in s:
    if e == "#":
        b += 1

    else:
        w -= 1

    ans = min(ans, b + w)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
w = S.count(".")
b = 0

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

Submission status: Accepted

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()
'''
cnt = [0]*(N+1)
ans = N+1

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

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

print(ans)
'''

dp = dp2(0, 2, N+1)

if S[0] == '.':
    dp[1][0] = 0
    dp[1][1] = 1
else:
    dp[1][0] = 1
    dp[1][1] = 0

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

print(min(dp[N][0], dp[N][1]))

Submission status: Accepted

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

#with open('01.txt') as data:
    #lines = data.readlines()
    #N = int(lines[0].rstrip())
    #S = lines[1].rstrip()
'''
N = ii()
S = input()

w_cnt = 0
b_cnt = 0

for i in S:
    if i == '.':
        w_cnt += 1
    else:
        break

for i in S[::-1]:
    if i == '#':
        b_cnt += 1
    else:
        break

w = b = 0

for i in range(w_cnt, N - b_cnt):
    if S[i] == '#':
        b += 1
    else:
        w += 1

cnt = 0

for i in S:
    if i == '#':
        cnt += 1

#print(w_cnt, b_cnt, w, b)

print(min(w, b, cnt, N-cnt))
'''
N = ii()
S = input()

cnt = [0]*(N+1)
ans = N+1

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

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

white=[]
black=[]
w=0
b=0

for i in range(n):
    if s[i]==".":
        w+=1
    else:
        b+=1
    white.append(w)
    black.append(b)

if white==[] or black==[]:
    print(0)
    exit()

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

Submission status: Accepted

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

    B = [0]
    cnt = 0
    for s in S:
        if s == '#':
            cnt += 1
        B.append(cnt)

    W = [0]
    cnt = 0
    for s in reversed(S):
        if s == '.':
            cnt += 1
        W.append(cnt)
    W.reverse()

    min = 10 ** 9
    for w, b in zip(W, B):
        if w+b < min:
            min = w+b
    print(min)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/tenka1-2019/tasks/tenka1_2019_c

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

tot_w = s.count('.')

ret = tot_w  # 初期配置をすべて黒にした場合にして、左端から白を置いていく
w = 0
b = 0
for c in s:
    if c == '#':
        b += 1
    else:
        w += 1
    cost = b + tot_w - w
    ret = min(ret, cost)

print(ret)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import numpy as np
import collections
from functools import cmp_to_key

##  input functions for me
def rsa(sep = ''):
    if sep == '' :
        return input().split() 
    else: return input().split(sep)
def rip(sep = ''):
    if sep == '' :
        return map(int, input().split()) 
    else: return map(int, input().split(sep))
def ria(sep = ''): 
    return list(rip(sep))
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()
##
def main():
    inf = int(1e9)
    N = ri()
    S = rs()
    dp = [[inf] * N for i in range(2)]
    # forbidden : '#.'
    if S[0] == '.':
        dp[0][0] = 0
        dp[1][0] = 1
    else:
        dp[0][0] = 1
        dp[1][0] = 0
    for i in range(1, N):
        if S[i] == '.':
            dp[0][i] = min(dp[0][i], dp[0][i-1])
            dp[1][i] = min(dp[1][i], dp[0][i-1] + 1)
            dp[1][i] = min(dp[1][i], dp[1][i-1] + 1)
        else :
            dp[0][i] = min(dp[0][i], dp[0][i-1] + 1)
            dp[1][i] = min(dp[1][i], dp[0][i-1])
            dp[1][i] = min(dp[1][i], dp[1][i-1])
    print(min(dp[0][N-1], dp[1][N-1]))



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a = input()
b = input()
c = list(b)
d = 0
e = c.count(".")
f = 0
for i in range(len(c)+1): #左からi個を白にする　0<=i<=len(c)
    if i == 0:
        ans = e
    else:
        if c[i-1] == ".":
            d = d + 1 #dは左からi個のうちの白の個数を指す
        f = (i - d) + (e - d) #左側の(i-d)個の黒を白にし、右側(e-d)個の白を黒にする
        ans = min(ans, f)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
Stone = list(input())
Black_count = 0
White_count = 0
Cost = []

#全体の白石の数をかぞえる
for i in range(N):
  if Stone[i] == '.':
    White_count += 1

#"黒い石のすぐ右に白い石があるような箇所がない"状態 = j番目の点より右側は黒、左側は白になる。(j番目の点の色はどっちでもいい)
#つまり、j番目(0 ≦ j ≦ N-1)の石を境目にする時のコストをCostに記入していき最小のものを取れば良い。

for j in range(N):
  #j番目の石が白のとき
  if Stone[j] == '.':
    White_count -= 1
    Cost.append(White_count + Black_count)
  #j番目の石が黒のとき
  else:
    Cost.append(White_count + Black_count)
    Black_count += 1

print(min(Cost))

Submission status: Accepted

Solution code:
import sys,bisect,heapq

mod = 10**9+7
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(input())
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while(a%b != 0):
        a,b = b,a%b
    return b

def LCM(a,b):
    return a*b//GCD(a,b)

def main():
    N = I()
    s = S()

    cSum = [0]
    for i in range(N):
        if s[i] == "#":
            cSum.append(cSum[-1]+1)
        else:
            cSum.append(cSum[-1])

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

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

Submission status: Accepted

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

T = [0 for _ in range(N+1)]
a = 0
for i, s in enumerate(S):
    if s == '#':
        a += 1
    T[i+1] = a

ans = N
for l in range(N+1):
    a = T[l] + (N-l)-(T[-1]-T[l])
    ans = min(ans, a)


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(str(input()))
a = ans = S.count(".")
for i in S:
    if i=="#":
        a += 1
    else:
        a -= 1
    ans = min(ans,a)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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