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: Time Limit Exceeded

Solution code:
#import numpy as np
N = int(input())
S = input()

#nums= np.zeros(N+1, dtype=int)
#for i, s in enumerate(S):
#    if s == ".":
#        nums[:i+1] += 1
#    else:
#        nums[i+1:] += 1
#
#out = nums.min()

nums = [0 for i in range(N+1)]

for i, s in enumerate(S):
    if s == ".":
        for j in range(0, i):
            nums[j] += 1
    else:
        for j in range(i+1, N+1):
            nums[j] += 1

out = min(nums)
print(out)

Submission status: Time Limit Exceeded

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())
count = 0
minn = 10 ** 9
for i in range(n + 1):
    minn = min(minn, s[:i].count('#') + s[i:].count('.'))
print(minn)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = str(raw_input())
count = N
for i in range(N+1):
	count0 = S[:i].count('#') + S[i:].count('.')
	count = min(count, count0)
print count

Submission status: Time Limit Exceeded

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

import numpy as np
arr = np.empty(N+1,dtype='object')

for i in range(len(arr)):
    arr[i] ='.'*i+  '#'*(N-i)

countlist = []
for i in range(len(arr)):
    s = arr[i]
    c = 0
    for j in range(len(s)):
        if s[j] != S[j]:
            c += 1
    countlist.append(c)

print(min(countlist))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = np.array(list(input()))

minimum = []
for i in range(0,len(S)+1):
    str1 = ["."]*i
    str2 = ["#"]*(len(S)-i)
    str3 = str1 + str2
    str3 = np.array(str3)
    x = str3==S
    minimum.append(np.sum(x==False))
print(np.min(minimum))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
t=[]

def popcnt0(n):
	global N
	c = 0
	for i in range(N):
		c += (n >> i) & 1
	return c

for i in range(len(S)):
	if S[i]=='#':
		t.append('1')
	else:
		t.append('0')
tt=''.join(t)
a=int(tt, 2)
b=popcnt0(0^a)
for i in range(N):
	tmp=((1<<(i+1)) -1) ^ a
	m=popcnt0(tmp)
	if m<b:
		b=m
print(b)

Submission status: Time Limit Exceeded

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

from sys import stdin
    
def calc(n, s):
    s2 = [ c == '#' for c in s]
    total = sum(s2)
    ans =[2*sum(s2[:i])+n-i-total for i in range(n+1)]
    return min(ans)

def main():
    n = int(stdin.readline().rstrip())
    s = stdin.readline().rstrip()
    ans = calc(n, s)
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
def check(N, S):
    ls = []
    for i in range(N - 1):
        if S[i] == "#" and S[i + 1] == ".":
                ls.append(True)
        else:
            ls.append(False)
    return ls.count(True)

def change(N, S):
    cnts = []
    for i in range(1, N):
        form = S[:i]
        latt = S[i:]
        cnt = 0
        if form.count(".") >= form.count("#"):
            for j in range(len(form)):
                if form[j] == "#":
                    form[j] = "."
                    cnt += 1
        if form.count(".") < form.count("#"):
            for j in range(len(form)):
                if form[j] == ".":
                    form[j] = "#"
                    cnt += 1
        for j in range(len(latt)):
            if latt[j] == ".":
                latt[j] = "#"
                cnt += 1
        cnts.append(cnt)
    return cnts

if check(N, S) == 0:
    print(0)
else:
    print(min(change(N, S)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = input()
ans= 0
for i in range(N):
	if l.count("#.")>0:
		ans += l.count("#.")
		l = l.replace("#.","")
	else:
		break
print(ans)

Submission status: Time Limit Exceeded

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

result = min(S.count("."),S.count("#"))
i = 0

while i < N:
    leftslice = S[:i]
    rightslice = S[i:]
    leftblack = leftslice.count("#")
    rightwhite = rightslice.count(".")
    nowresult = leftblack + rightwhite
    if result >= nowresult:
        result = nowresult
    else:
        i += (nowresult - result)
    i += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
a = input()
count = 0
for i in range(num):
  if i < num-2 and a[i] == "#" and a[i+1] == ".":
    count += 1
    a = a[:i+1] + "#" + a[i+2:]
  elif i == num-2 and a[i] == "#" and a[i+1] == ".":
    count += 1
    a = a[:i+1] + "#"
print(count)

Submission status: Time Limit Exceeded

Solution code:
argN = int(input())
argS = input()
aryS = list(argS)

idxBorder = 0
ansMin = 1000000

ans1 = aryS.count(".") #全て黒にする場合
ans2 = aryS.count("#") #全て白にする場合

for i in range(1,argN):
  #cntW1 = aryS[:i].count(".")
  cntB1 = aryS[:i].count("#")

  cntW2 = aryS[i:].count(".")
  #cntB2 = aryS[i:].count("#")
  #print(str(i) + "," + str(cntB1) + "," + str(cntW2))
  ansMin = min(ansMin,cntB1 + cntW2)

print(min(ans1,ans2,ansMin))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=[a for a in input()]
s=[a.replace('.','0') for a in s]
s=[a.replace('#','1') for a in s]
s=[int(a) for a in s]
p=[i for i, x in enumerate(s) if x == 1]
s = np.array(s)
cnt = np.sum(s)
for i in p:
    s[-1 * (n-i):] = 1 - s[-1 * (n-i):]
    _cnt = np.sum(s)
    if cnt > _cnt:
        cnt =_cnt
print(cnt)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(len(s)-1):
    if s[i] == '#' and s[i+1] == '.':
        if i == 0:
            s[i] = '.'
            count += 1
        elif i == len(s) -2:
            s[i+1] = '#'
            count += 1
        else:
            if s[i+2] == '.':
                if s[:i+1].count('#') < s[i:].count('.'):
                    s[:i] = '.' * len(s[:i])
                    count += len(s[:i])
            if s[i-1] == '.':
                s[i] = '.'
                count += 1
            else:
                s[i+1] = '#'
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0
z = 0
if N == 1:
    print("0")
    exit()
else:
    for i in range(N-1):
        x = S[i]
        y = S[i+1]
        if x == "#":
            if y == ".":
                cnt += 1
                if i != N-1:
                    mae = S[0:i+1]
                    ato = S[i+2:N]
                    S = mae + "#" + ato

print(cnt)

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
S = input()
L = [0] * N
for i in range(N):
    if S[i] == "#":
        # white
        L[i] = 1
    else:
        L[i] = 0
L = np.array(L, dtype=np.int8)
L2 = np.zeros(N, dtype=np.int8)

ans = np.abs((L - L2)).sum()
for i in range(N):
    L2[N-1-i] = 1
    ans_tmp = np.abs((L - L2)).sum()
    ans = min(ans, ans_tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

hoge=input()

huga=[]

flag=hoge[0]
kazu=0

for i in range(n):
  if(hoge[i]==flag):
    kazu+=1
  else:
    huga.append(kazu)
    kazu=1
    flag=hoge[i]

huga.append(kazu)

ans=[]
ans.append(hoge.count("."))
ans.append(hoge.count("#"))
base=0

if(hoge[0]=="#"):
  base+=huga[0]
  huga=huga[1:]
if(hoge[-1]=="."):
  base+=huga[-1]
  huga=huga[:len(huga)-1]

  
kuro=[]
siro=[]
flag="w"
for i in range(len(huga)):
  if(flag=="w"):
    siro.append(huga[i])
    flag="b"
  else:
    kuro.append(huga[i])
    flag="w"

siro=siro[1:]
kuro=kuro[:len(kuro)-1]
for i in range(len(siro)+1):
  ans.append(base+sum(siro[:i])+sum(kuro[i:]))
  
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
def read_input():
    n = int(input())
    s = input().strip()

    return n, s


def check_answer(n, s):
    zeros = n

    result = []
    min_diff = n
    for ones in range(n + 1):
        comp = '.' * (zeros - ones) + '#' * ones
        diff = get_diff(comp, s)

        if diff < min_diff:
            min_diff = diff

    return min_diff


def get_diff(s, t):
    count = 0
    for i in range(len(s)):
        if s[i] != t[i]:
            count += 1

    return count


if __name__ == '__main__':
    n, s = read_input()
    print(check_answer(n, s))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()

nums= np.zeros(N+1, dtype=int)
for i, s in enumerate(S):
    if s == ".":
        nums[:i+1] += 1
    else:
        nums[i+1:] += 1

out = nums.min()
print(out)

Submission status: Time Limit Exceeded

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

    answer = N + 1

    for i in range(0, N+1):
        ok_pattern = "."*i + "#" * (N-i)
        different_str_count = 0

        for j in range(0, len(ok_pattern)):
            if ok_pattern[j] != S[j]:
                different_str_count += 1

        if answer > different_str_count:
            answer = different_str_count

        if answer == 0:
            break

    print(answer)

Submission status: Time Limit Exceeded

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

min = n
for i in range(n+1):
    m = string[:i].count("#") + string[i:].count(".")
    if m < min:
        min = m

print(min)

Submission status: Time Limit Exceeded

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

count1 = s.count('.')
count2 = s.count('#')

count3 = 2 * 10 ** 5 + 1
for i in range(1,n):
    count3 = min(count3, s[:i-1].count("#")+s[i:].count("."))
print(min(count1, count2, count3))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Min = N
for i in range(N+1):
    cnt = S[:i].count("#") + S[i:].count(".")
    # print(S[:i].count("#") + S[i:].count("."))
    if Min > cnt:
        Min = cnt
print(Min)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=[a for a in input()]
s=[a.replace('.','0') for a in s]
s=[a.replace('#','1') for a in s]
s=[int(a) for a in s]
s = np.array(s)
cnt = np.sum(s)
for i in range(n):
    s[-1 * (i+1)] = 1 - s[-1 * (i+1)]
    _cnt = np.sum(s)
    if cnt > _cnt:
        cnt =_cnt
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

b = 0
for i in range(N):
    if S[N-i-1] == '#':
        b += (2**i)

print(min(bin((2**i-1)^b).count("1") for i in range(N+1)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
t = s.copy()
b = 0
w = 0

for i in range(n-1):
    if s[i] == "#" and s[i+1] == ".":
        s.pop(i)
        s.insert(i, ".")
        b += 1

for i in range(n-1):
    if t[i] == "#" and t[i+1] == ".":
        t.pop(i+1)
        t.insert(i+1, "#")
        w += 1

print(min(b,w))

Submission status: Time Limit Exceeded

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

a=0
S2=[]
minimum=1000000
for i in range(N):
    if(S[i]=="."):
        S2.append(-1)
    else:
        S2.append(1)

for tmp in range(N+1):
    count=0
    for i in range(N):
        if(i<tmp):
            if( S2[i]*-1 == -1):
                count+=1
        else:
            if(S2[i]*1==-1):
                count+=1
    if(count<minimum):
        minimum=count
print(minimum)

Submission status: Time Limit Exceeded

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

arr = []
for i in range(n+1):
    arr.append(string[:i].count("#") + string[i:].count("."))

print(min(arr))

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = str(input())
res = 0

for i in range(a):
    if b[i:i+2] == "#.":
        res = b[i:a].count(".")
        break
        
for i in range(a,-1,-1):
    if b[i:i+2] == "#.":
        res = min(b[0:i+2].count("#"),res)
        break

for i in range(a):
    if b[i:i+2] == "#.":
        if res == 0:
            res = 10000
        res = min(b[i:a].count(".")+b[0:i+1].count("#"),res)
        
print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s1 = s
c1 = 0
for i in range(n-1):
    if s1[i:i+2]=="#.":
        c1 += 1
        s1 = s1[:i+1]+"#"+s1[i+2:]
print(c1)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
a = input()
count = 0
for i in range(num):
  if i < num-2:
    if a[i] == "#" and a[i+1] == ".":
      count += 1
      a = a[:i+1] + "#" + a[i+2:]  
  elif i == num-2:
    if a[i] == "#" and a[i+1] == ".":
      count += 1
      a = a[:i+1] + "#"
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min = N+1

for i in range(N):
    temp_min = 0
    for j in range(N):
        if j < i:
            if S[j:j+1] == "#":
                temp_min += 1
        if j >= i:
            if S[j:j+1] ==".":
                temp_min += 1

    if temp_min < min:
        min = temp_min
        

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


print(min)

Submission status: Time Limit Exceeded

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

pattern = re.compile(r"#.")

count = 0
while True:
    S, c = re.subn(pattern, "##", S)
    if c == 0:
        break
    count += c

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 0
str_list = list(S)

while (S.find('#.') != -1):
    index = S.find('#.')

    if index == 0:
        str_list[0] = '.'
    elif index == N - 1:
        str_list[N - 1] = '#'
    elif str_list[index - 1] == '.':
        str_list[index] = '.'
    elif str_list[index - 1] == '#':
        str_list[index + 1] = '#'
    cnt += 1
    S = "".join(str_list)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
argN = int(input())
argS = input()
aryS = list(argS)

idxBorder = 0
ansMin = 1000000

for i in range(argN+1):
  if i == 0:
    #cntW1 = 0
    cntB1 = 0
  else:
    #cntW1 = aryS[:i].count(".")
    cntB1 = aryS[:i].count("#")

  if i == argN:
    cntW2 = 0
    #cntB2 = 0
  else:
    cntW2 = aryS[i:].count(".")
    #cntB2 = aryS[i:].count("#")
  #print(str(i) + "," + str(cntB1) + "," + str(cntW2))
  ansMin = min(ansMin,cntB1 + cntW2)
print(ansMin)

Submission status: Time Limit Exceeded

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

result = min(S.count("."),S.count("#"))

for i in range(N):
    leftslice = S[:i]
    rightslice = S[i:]
    leftblack = leftslice.count("#")
    rightwhite = rightslice.count(".")
    if result >= leftblack + rightwhite:
        result = leftblack + rightwhite

print(result)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
first_black = 0
count = 0
for i in range (N):
    if first_black == 0 and S[i]=='#':
        first_black = 1
    if first_black == 1 and S[i]== '.':
        count +=1
        
first_white = 0
white_count = 0
for i in range (N):
    if first_white == 0 and S[N-i-1]=='.':
        first_white = 1
    if first_white == 1 and S[N-i-1]== '#':
        white_count +=1

white_black = 10**6
for i in range (N):
    temp = S[:i].count('#') + S[i:].count('.')
    if temp<= white_black:
        white_black = temp
        
print(min(count,white_count,white_black))

Submission status: Time Limit Exceeded

Solution code:
#C
N=int(input())
S=input()
even = "#"*N
odd = "."*N
eve_c=N-S.count('#')
odd_c=N-S.count('.')
ans=[]
for i in range(N):
    ans.append(S[:i+1].count('#')+S[i+1:].count('.'))

ans.append(eve_c)
ans.append(odd_c)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    N = int(input())
    S = input()
    ans = 0
    for i in range(N-1):
        if S[i:i+2]=='#.':
            if i==0 or ((i-1)>=0 and S[i-1]=='.'):
                S = S[:i] + '.' + S[i+1:]
                ans += 1
            else:
                S = S[:i+1] + '#' + S[i+2:]
                ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
print(min(s[:i].count('#')+s[i:].count('.') for i in range(n+1)))

Submission status: Time Limit Exceeded

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

cont=0
best=n
ans=[]
for i in range(len(s)):
	for j in range(len(s)):
		if j<i and s[j]=='#':
			cont+=1
		if j>i and s[j]=='.':
			cont+=1
	ans.append(cont)
	if cont<best:
		best=cont
	if cont==0 or cont>ans[len(ans)-1]:
		break
	cont=0
print(best)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
b = [s[i:].count(".") for i in range(n)]
c = [s[:i-1].count("#") for i in range(n)]
a = [b[i]+c[i] for i in range(n)]
if "." not in s:
    a.append(0)
elif "#" not in s:
    a.append(0)
print(min(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 0
str_list = list(S)

while (S.find('#.') != -1):
    index = S.find('#.')

    if index == 0:
        str_list[0] = '.'
        cnt += 1
    elif index == N - 1:
        str_list[N - 1] = '#'
        cnt += 1
    elif str_list[index - 1] == '.':
        str_list[index] = '.'
        cnt += 1
    elif str_list[index - 1] == '#':
        str_list[index + 1] = '#'
        cnt += 1

    S = "".join(str_list)

print(cnt)

Submission status: Time Limit Exceeded

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

N = int(input())
S = str(input())
Sarray = list(S)
Sarraynum = []

#白0
for i in range(len(Sarray)):
    if Sarray[i] == '.':
        Sarraynum.append(0)
    else:
        Sarraynum.append(1)

a = 2 * len(Sarraynum)
for i in range(len(Sarraynum)):
    black2white = 0
    white2black = 0
    for j in range(i):
        black2white += Sarraynum[j]
        if black2white >= a:
            break

    for j in range(i + 1, len(Sarraynum)):
        if Sarraynum[j] == 0:
            white2black += 1
        if black2white + white2black >= a:
            break
    
    if black2white + white2black < a:
        a = black2white + white2black


print(a)

Submission status: Time Limit Exceeded

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

ans = N
left_black = [0]
right_white = [0]

for i in range(N):
    if S[i] == '#':
        left_black.append(left_black[-1] + 1)
    else:
        left_black.append(left_black[-1])
    if S[N - 1 - i] == '.':
        right_white.insert(0, right_white[0] + 1)
    else:
        right_white.insert(0, right_white[0])

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count1 = S.count("#")  #すべて白にする
count2 = S.count(".")  #すべて黒にする
A = []
for i in range(N):
	A.append(S[: i].count("#") + S[i:].count("."))
count = min(A)
print(min(count1, count2, count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
 
le=N
 
ans = 0
flag=True
for i in range(le-1):
  if S[i]=="#" and S[i+1]==".":
    ans += 1
    S = S[:i+1] + "#" + S[i+2:]
  if flag:
    if S[i]=="#":
      x = i
  
if 2*ans < N:
  print(ans)
 
else:
  print(le -ans - x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(N-1):
  if S.count('#') < S.count('.'):
    if S[-1] != '#':
      res = S.count('#')
    else:
      res = S.count('#') -1
  else:
    if S[0] != '.':
      res = S.count('.')
    else:
      res = S.count('.') - 1

print(res)

Submission status: Time Limit Exceeded

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

b = 0
for i in range(N):
    if S[N-i-1] == '#':
        b += (2**i)

print(min(bin((2**i-1)^b).count("1") for i in range(N+1)))

Submission status: Time Limit Exceeded

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

cntlis = []

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

print(min(cntlis))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

from sys import stdin
    
def calc(n, s):
    b = '#'
    w = '.'
    last = ''

    s2 = [ c == '#' for c in s]
    ans =[sum(s2[:i])+n-i-sum(s2[i:]) for i in range(n+1)]
    return min(ans)

def main():
    n = int(stdin.readline().rstrip())
    s = stdin.readline().rstrip()
    ans = calc(n, s)
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
black = [s=="#" for s in S]
ans = N - sum(black)
if sum(black)==0:
    ans = 0
for i in range(N):
    a = sum(black[:i])
    b = N-i-sum(black[i:])
    ans = min(ans,a+b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def cutw(s,n):
    t = 0
    for i in range(n):
        if s[i] == '#':
            break
        else:
            t += 1
    s = s[t:]
    n = len(s)
    return s,n

def cutb(s,n):
    t = n
    for i in range(n-1, -1,-1):
        if s[i] == '.':
            break
        else:
            t -= 1
    s = s[:t]
    n = len(s)
    return s,n


if __name__ == '__main__':
    n = int(input())
    s = input()
    res = 0
    s1 = s
    n1 = n
    res1 = 0
    while True:
        s,n = cutw(s,n)
        s,n = cutb(s,n)
        if n>0:
            s = '.'+s[1:]
            res += 1
        else:
            break
    while True:
        s1,n1 = cutw(s1,n1)
        s1,n1 = cutb(s1,n1)
        if n1>0:
            s1 = s1[:n1-1] + '#'
            res1 += 1
        else:
            break
    print(min(res,res1))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
def check_and_back_or_go_to(li, ind, N, counter):
    back = True
    # print('see {} {} and {} counter{}'.format(ind, li[ind - 1], li[ind], counter))
    if ind == 0:
        return counter
    elif ind + 1 == N:
        back = False
    # access
    if li[ind - 1] == '#' and li[ind] == '.':
        li[ind] = '#'
        if back:
            c=check_and_back_or_go_to(li, ind + 1, N, counter + 1)
            return c
        c=check_and_back_or_go_to(li, ind - 1, N, counter + 1)
        return c
    else:
        c=check_and_back_or_go_to(li, ind - 1, N, counter)
        return c


N = int(input())
S = list(input())
r = range(N)
count = check_and_back_or_go_to(S, len(S) - 1, N, 0)
print(min([count,N-count]))

Submission status: Time Limit Exceeded

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

def counting(seq, s):
  return seq.count(s)

count_list = []
for i in range(N):
  count_list.append(counting(S[:i], "#") + counting(S[i:], "."))
count_list.append(counting(S, "#")+0)
print(min(count_list))

Submission status: Time Limit Exceeded

Solution code:
import re

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

cntList = list()
for i in range(n+1):
    tmp = len(re.findall('#', s[:i]))
    tmp += len(re.findall('\.', s[i:]))
    cntList.append(tmp)

print(min(cntList))

Submission status: Time Limit Exceeded

Solution code:
#C
N=int(input())
S=input()
even = "#"*N

odd = "."*N
eve_c=0
odd_c=0
ans=[]
for i in range(N):
    ans.append(S[0:i+1].count('#')+S[i+1:].count('.'))
    if S[i]!=even[i]:
        eve_c+=1

    if S[i]!=odd[i]:
        odd_c+=1

ans.append(eve_c)
ans.append(odd_c)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ck = [0]
cs = [0]
c = 0
for item in s:
    if item != '.':
        c += 1
    cs.append(c)
c = 0
for item in s[::-1]:
    if item != '#':
        c +=  1
    ck.insert(0, c)
ck.insert(0, 0)

temp = [cs[i] + ck[i+1] for i in range(len(s)+1)]

print(min(temp))

Submission status: Time Limit Exceeded

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

ans = [0 for i in range(n + 1)]

for i in range(n):
    for j in range(n + 1):
        if j <= i:
            if s[i] == '.':
                ans[j] += 1
        else:
            if s[i] == '#':
                ans[j] += 1
print(min(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S0 = input()
S = [S0[i] for i in range(N)]
minimum = N + 1

for i in range(N+1):
    m = 0
    for k in range(i):
        if S[k] != '.':
            m += 1
    for k in range(i, N):
        if S[k] != '#':
            m += 1
    if m < minimum:
        minimum = m

print(minimum)

Submission status: Time Limit Exceeded

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

cont=0
best=n
for i in range(len(s)):
	for j in range(len(s)):
		if j<i and s[j]=='#':
			cont+=1
		if j>i and s[j]=='.':
			cont+=1
	if cont<best:
		best=cont
	if cont==0:
		break
	cont=0
print(best)

Submission status: Time Limit Exceeded

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

ans=2000000
cnt=0
for i in range(n):
  for j in range(n-i):
    if s[i+j]==".":
      cnt+=1
  for k in range(i):
    if s[k]=="#":
      cnt+=1
  if cnt<=ans:
    ans=cnt
  cnt=0

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

if abs(cnt)==len(s):
  ans=0

print(ans)

Submission status: Time Limit Exceeded

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

i_string = 0
ans = 0
for i in range(N):
    i_string = S[i:i+1]
    if i_string == "#":
        right_string = S[i+1:i+2]
        if right_string == ".":
            S = (S[:i+1] + "#" + S[i+2:])
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

def f(i, j):
	if j >= i:
		return "#"
	else:
		return "."

ans = float("inf")
for i in range(N+1):
	tmp = 0
	for j in range(N):
		if S[j] != f(i, j):
			tmp += 1
	if tmp < ans:
		ans = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S = list(S)
S2 = list(S)
ans = 0
while True:
    cnt = 0
    newS = list(S2)
    i = 0
    while i < (len(S2)-1):
        if S2[i] == '#' and S2[i+1] == '.':
            newS = newS[0:i-cnt*2] + newS[i+2 - cnt*2:]
            ans += 1
            cnt += 1
            i += 1
        i += 1
    S2 = list(newS)
    if cnt == 0:
        break

print(ans)

Submission status: Time Limit Exceeded

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

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

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

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

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

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

print(min(sum(one[:i]) + sum(zero[i+1:]) for i in range(len(one)+1)))

Submission status: Time Limit Exceeded

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

min = n

for i in range(n):
    bl = 0
    wr = 0
    for k in range(i):
        if s[k] == '#':
            bl = bl + 1

    for l in range(n - i - 1):
        if s[l + i + 1] == '.':
            wr = wr + 1

    if min > wr + bl:
        min = wr + bl


print(min)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=[a for a in input()]
x = []
for a in s:
    if a == '.':
        x.append(0)
    else:
        x.append(1)
s = np.array(x)
ans = np.zeros(n).astype(np.uint8)
cnt = np.sum(s != ans)
for i in range(n):
    ans[-1 * (i+1)] = 1
    _cnt = np.sum(s != ans)
    if cnt > _cnt:
        cnt = _cnt
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
 
ans = 0
for i in range(len(S)-1):
  if S[i]=="#" and S[i+1]==".":
    ans += 1
    S = S[:i+1] + "#" + S[i+2:]
  
if 2*ans < len(S):
  print(ans)
 
else:
  print(len(S)-ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(raw_input())
s=raw_input()
ret=n
for i in range(n+1):
    head=s[:i]
    tail=s[i:]
    temp=head.count("#")+tail.count(".")
    if temp<ret:
        ret=temp
    if temp==0:
        ret=0
        break
print(ret)

Submission status: Time Limit Exceeded

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

def add_one(x):
    return x + 1

w_counter_before = [0] * n
w_counter_all = 0
for i in range(n):
    if s[i] == '.': # white
        w_counter_before[i:] = list(map(add_one, w_counter_before[i:]))
        w_counter_all += 1

if w_counter_all == 0:
    print(0)
else:
    counters = []
    for i in range(n):
        cost1 = i - w_counter_before[i] + 1
        cost2 = w_counter_all - w_counter_before[i]
        counters.append(cost1 + cost2)
    print(min(counters))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min = N + 1

for i in range(N):
    temp_min = 0
    for j in range(i):
        if S[j:j+1] == "#":
            temp_min += 1
    for j in range(N-i):
        if S[j+i:j+i+1] == ".":
            temp_min += 1
    if temp_min < min:
        min = temp_min

temp_min = 0
for i in range(N):
    if S[i:i+1] == "#":
        temp_min += 1
        
if temp_min < min:
    min = temp_min
        
print(min)

Submission status: Time Limit Exceeded

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

moves = 2 * 10 ** 5

for i in range(len(S) + 1):
    left = S[:i]
    right = S[i:]
    moves = min(moves, left.count('#') + right.count('.'))

print(moves)