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:
n=int(input())
s=input()

ans = n

for i in range(n+1):
	w=s[:i]
	b=s[i:]
	tmp_ans = w.count("#") + b.count(".")
	ans = ans if ans <=tmp_ans else tmp_ans
print(ans)

Submission status: Time Limit Exceeded

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

# input
# 3
# #.#

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

w = S.count(".")
b = S.count("#")

if w == N or b == N:
    print(0)
    exit()


ans = len(S)
for i in range(len(S)):
    w_n = S[i:].count(".")
    b_n = S[:i].count("#")

    ans = min(ans, w_n + b_n)

    if i >= len(S)/2:
        break

print(ans)

Submission status: Time Limit Exceeded

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

# input
# 3
# #.#

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

w = S.count(".")
b = S.count("#")

if w == N or b == N:
    print(0)
    exit()


ans = len(S)
for i in range(len(S)):
    w_n = S[i:].count(".")
    b_n = S[:i].count("#")

    ans = min(ans, w_n + b_n)
    
    if i >= len(S):
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Bsum = [0] #境界より左にある黒の数
Wsum = [0] #境界より右にある白の数
for i in range(N):
    Bsum = Bsum + [Bsum[-1]+(1 if S[i]=="#" else 0) ]
    Wsum = [Wsum[0]+(1 if S[N-i-1]=="." else 0)] + Wsum
print(min([Bsum[i]+Wsum[i] for i in range(N+1)]))

Submission status: Time Limit Exceeded

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

N=int(N)
wn=[]
bn=[]
a=0
b=0

#各行間よりも前と後ろに何個白（黒）があるかをカウント
for i in range(N):
    j=N-(i+1)
    if(s[j]=="."):
        a+=1
    if(s[i]=="#"):
        b+=1
    bn.insert(0,a)
    wn.append(b)
bn.append(0)
wn.insert(0,0)
#print(bn)
#print(wn)

#各行間のscore算出
min=1000000
for i in range(N+1):
    score=bn[i]+wn[i]
    if(score<min):
        min=score

print(min)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S = [0 if si == "#" else 1 for si in S]
Bsum = [0] #境界より左にある黒の数
Wsum = [0] #境界より右にある白の数
for i in range(N):
    Bsum = Bsum + [Bsum[-1]+(1 if S[i]==0 else 0) ]
    Wsum = [Wsum[0]+(1 if S[N-i-1]==1 else 0)] + Wsum
print(min([Bsum[i]+Wsum[i] for i in range(N+1)]))

Submission status: Time Limit Exceeded

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

# input
# 3
# #.#

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

w = S.count(".")
b = S.count("#")

if w == N or b == N:
    print(0)
    exit()


ans = len(S)
for i in range(len(S)):
    w_n = S[i:].count(".")
    b_n = S[:i].count("#")

    ans = min(ans, w_n + b_n)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(raw_input())
s = str(raw_input())

l = s.strip()
memo=[]
for i in range(0,n):
    memo.append(-1)
memo[0]=0

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

print memo[n-1]

Submission status: Time Limit Exceeded

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

ans = N

for n in range(N):
	blk=S[:n].count("#")
	white=S[n+1:].count(".")
	ans=min(blk+white,ans)


print(ans)

Submission status: Time Limit Exceeded

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

white = 0
bcount = 0
wcount = 0
mins = []

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

mins.append(white)
        
for i in range(N):
    for j in range(N):
        if (j <= i and S[j] == '#'):
            bcount += 1
        elif (i < j and S[j] == '.'):
            wcount += 1
    mins.append(bcount + wcount)
    bcount = 0
    wcount = 0

print(min(mins))

Submission status: Time Limit Exceeded

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

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

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

for i in range(N):
  if S[i]!="#":
    ans_all_black = ans_all_black + 1
  if S[i]!=".":
    ans_all_white = ans_all_white + 1
  
  #print(i,":",ans_all_white,":",end="")
  
  temp_right_black = 0
  
  if i+1 <N:
    for j in range(i+1,N,1):
      if S[j]==".":
         temp_right_black = temp_right_black+1

    #print(temp_right_black)

    if temp_right_black+ans_all_white<ans_right_black:
      ans_right_black = temp_right_black + ans_all_white
    
    

ans = min(ans_all_white,ans_all_black,ans_right_black)
print(ans)

Submission status: Time Limit Exceeded

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

N=int(N)
wn=[]
bn=[]
a=0
b=0

for i in range(N):
    j=N-(i+1)
    if(s[j]=="."):
        a+=1
    if(s[i]=="#"):
        b+=1
    bn.insert(0,a)
    wn.append(b)
bn.append(0)
wn.insert(0,0)
#print(bn)
#print(wn)

#score算出
min=1000000
for i in range(N+1):
    score=bn[i]+wn[i]
    if(score<min):
        min=score

print(min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

N=int(N)
score=[]
wn=[]
bn=[]
a=0
b=0

for i in range(N):
    j=N-(i+1)
    if(s[j]=="."):
        a+=1
    if(s[i]=="#"):
        b+=1
    bn.insert(0,a)
    wn.append(b)
bn.append(0)
wn.insert(0,0)
#print(bn)
#print(wn)

#score算出
min=1000000
a=0
for i in range(N+1):
    score=bn[i]+wn[i]
    if(score<min):
        min=score

print(min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
m = n
for i in range(n + 1):
    counts = s[:i].count("#") + s[i:].count(".")
    if m > counts:
        m = counts
        
print(m)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = float("INF")

for i in range(N+1):
    left = S[0:i]
    right = S[i::]
    cnt_left = left.count("#")
    cnt_right = right.count(".")
    change_stone = cnt_left + cnt_right
    ans = min(ans, change_stone)
print(ans)

Submission status: Time Limit Exceeded

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

N=int(N)
score=[]
wn=[]
bn=[]
a=0
b=0

for i in range(N):
    j=N-(i+1)
    if(s[j]=="."):
        a+=1
    if(s[i]=="#"):
        b+=1
    bn.insert(0,a)
    wn.append(b)
bn.append(0)
wn.insert(0,0)
#print(bn)
#print(wn)

#score算出
a=0
for i in range(N+1):
    score.append(bn[i]+wn[i])

score.sort()
print(score[0])

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N-1) :
    if S[i] == "#" and S[i+1] == "." :
        S = S[:i+1] + "#" + S[i+2:]
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
m = n
for i in range(n + 1):
    if m > s[:i].count("#") + s[i:].count("."):
        m = s[:i].count("#") + s[i:].count(".")
        
print(m)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())

s = list(input())
b = 0
w = 0

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

print(min(b,w))

Submission status: Time Limit Exceeded

Solution code:
"""
python3 a.py < input.txt
"""
import math
import itertools as it
import heapq

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

T=[S[0]]
U=[1]
for si in S[1:]:
    if T[-1] == si:
        U[-1] = U[-1]+1
    else:
        T.append(si)
        U.append(1)
M=len(T)
B=[U[i] if T[i] == "#" else 0 for i in range(M)]
W=[U[i] if T[i] == "." else 0 for i in range(M)]
l=[sum(B), sum(W)]
for i in range(M):
    if T[i] == "#":
        l.append(sum(B[:i]) + sum(W[min(i+1, M-1):]))

#print(S)
#print(T)
#print(U)
#print(B)
#print(W)
#print(l)
print(min(l))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
lines = list(input())
 
ans = []
for i in range(len(lines)+1):
    left = lines[:i]
    right = lines[i:]
    ans.append(left.count('#')+right.count('.'))
print(min(ans))

Submission status: Time Limit Exceeded

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

li = list(S)
count = 2 * 10 ** 5

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
def main():
    N=int(input())
    line = list(input())
    ans=[]

    for i in range(N+1):
        a=line[:i].count("#")
        b=line[i:].count(".")
        heappush(ans,a+b)

    return heappop(ans)

print(main())

Submission status: Time Limit Exceeded

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

li = list(S)
count = 2 * 10 ** 5

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.split() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N = makeInt(input_string[0])
    S = input_string[1].rstrip()
    if len(S) == 1:
        return 0
    if S == "#.":
        return 1
    S = list("."+S)
    w = "."
    b = "#"
    c = 0
    b_c = 0
    b_b_c = 0
    for i in range(N):
        if S[i] == b:
            b_c += 1
        else:
            if b_c != 0:
                b_b_c += b_c
            b_c = 0
        if S[i] == w and S[i-1] == b:
            if b_b_c < len(S) - i:
            #b_c = 0
                w_c = 0
                flag = False
                for j in range(i, N+1):
                    if S[j] == w:
                        w_c += 1
                        if w_c > b_b_c + b_c:
                            for h in range(0, i)[::-1]:
                                if S[h] == b:
                                    S[h] = w
                                    c += 1
                            flag = True
                if flag:
                    continue
                #for k in range(0, i)[::-1]:
                #    if S[k] == b:
                #         b_c += 1
                #    else:
                #        break

                #print(w_c)
                #print(b_c)
                if w_c <= b_b_c + b_c:
                    S[i] = b
                    c += 1
                    continue
        if S[i] == b:
            if S[i-1] == w and S[i+1] == w:
                S[i] = w
                c += 1
                continue
        if i > 0 and S[i] == w:
            #print(S)
            #print(S[i])
            if S[i-1] == b:
                S[i] = b
                c += 1
    i = N
    if i == N:
        #print("a")
        #print(S)
        #print(S[i])
        if S[i-1] == b and S[i] == w:
            S[i] = b
            c += 1
    return c


def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.split() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N = makeInt(input_string[0])
    S = input_string[1].rstrip()
    if len(S) == 1:
        return 0
    if S == "#.":
        return 1
    S = list("."+S)
    w = "."
    b = "#"
    c = 0
    b_c = 0
    b_b_c = 0
    for i in range(N):
        if S[i] == b:
            b_c += 1
        else:
            if b_c != 0:
                b_b_c += b_c
            b_c = 0
        if S[i] == w and S[i-1] == b:
            if b_b_c != 0:
            #b_c = 0
                w_c = 0
                flag = False
                for j in range(i, N+1):
                    if S[j] == w:
                        w_c += 1
                        if w_c > b_b_c + b_c:
                            for h in range(0, i)[::-1]:
                                if S[h] == b:
                                    S[h] = w
                                    c += 1
                            flag = True
                if flag:
                    continue
                #for k in range(0, i)[::-1]:
                #    if S[k] == b:
                #         b_c += 1
                #    else:
                #        break

                #print(w_c)
                #print(b_c)
                if w_c <= b_b_c + b_c:
                    S[i] = b
                    c += 1
                    continue
        if S[i] == b:
            if S[i-1] == w and S[i+1] == w:
                S[i] = w
                c += 1
                continue
        if i > 0 and S[i] == w:
            #print(S)
            #print(S[i])
            if S[i-1] == b:
                S[i] = b
                c += 1
    i = N
    if i == N:
        #print("a")
        #print(S)
        #print(S[i])
        if S[i-1] == b and S[i] == w:
            S[i] = b
            c += 1
    return c


def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(n + 1):
    b = s[0:i].count('#')
    w = s[i+1:n].count('.')
    ans.append(b + w)

print(min(ans))

Submission status: Time Limit Exceeded

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

for i in range(N+1):
  R = S[i:]
  L = S[:i]
  wcount = R.count(".")
  bcount = L.count("#")
  tmp = wcount + bcount
  ans = min(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

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

N=int(N)
score=[]
bn=[]
a=0

if(s[N-1]=="."):
    a+=1
    bn.insert(0,a)
else:
    bn.insert(0,0)

for i in range(1,N):
    j=N-1-i
    if(s[j]=="."):
        a+=1
    bn.insert(0,a)
bn.append(0)
#print(bn)

#score算出
a=0
for i in range(N):
    now=bn[i]
    now+=a
    score.append(now)
    if(s[i]=="#"):
        a+=1
score.append(a)

score.sort()
print(score[0])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import itertools


def makeIntMatrix(lines):
    intMatrix = []
    for a in makeStringMatrix(lines):
        intMatrix.append([int(b) for b in a])
    return intMatrix
 
 
def makeStringMatrix(lines):
    stringMatrix = [line.split() for line in lines]
    return stringMatrix
 
 
def makeInt(line):
    return int(line.rstrip())
 
 
def makeMultiInteger(line):
    return [int(x) for x in line.rstrip().split()]


def solve(input_string):
    N = makeInt(input_string[0])
    S = input_string[1].rstrip()
    if len(S) == 1:
        return 0
    if S == "#.":
        return 1
    S = list("."+S)
    w = "."
    b = "#"
    c = 0
    b_c = 0
    b_b_c = 0
    for i in range(N):
        if S[i] == b:
            b_c += 1
        else:
            if b_c != 0:
                b_b_c += b_c
            b_c = 0
        if S[i] == w and S[i-1] == b:
            if b_b_c != 0:
            #b_c = 0
                w_c = 0
                for j in range(i, N+1):
                    if S[j] == w:
                        w_c += 1
                #for k in range(0, i)[::-1]:
                #    if S[k] == b:
                #         b_c += 1
                #    else:
                #        break

                #print(w_c)
                #print(b_c)
                if w_c > b_b_c + b_c:
                    for h in range(0, i)[::-1]:
                        if S[h] == b:
                            S[h] = w
                            c += 1
                else:
                    S[i] = b
                    c += 1
                    continue
        if S[i] == b:
            if S[i-1] == w and S[i+1] == w:
                S[i] = w
                c += 1
                continue
        if i > 0 and S[i] == w:
            #print(S)
            #print(S[i])
            if S[i-1] == b:
                S[i] = b
                c += 1
    i = N
    if i == N:
        #print("a")
        #print(S)
        #print(S[i])
        if S[i-1] == b and S[i] == w:
            S[i] = b
            c += 1
    return c


def main():
    input_lines = stdin.readlines()
    answer = solve(input_lines)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
def main():
    N=int(input())
    line = list(input())
    ans=[]

    for i in range(N+1):
        a=line[:i].count("#")
        b=line[i:].count(".")
        ans.append(a+b)
    return min(ans)

print(main())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()
x = []
xp = '#'
c = 0
for n in range(N):
    if S[n] == xp:
        c += 1
    else:
        xp = S[n]
        x.append(c)
        c = 1
x.append(c)
if len(x) % 2 != 0:
    x.append(0)
x = np.array(x)
x = np.reshape(x, (-1,2)).T
y = [sum(x[0,:n])+sum(x[1,n:]) for n in range(x.shape[1]+1)]
print(min(y))

Submission status: Time Limit Exceeded

Solution code:
#C - Stones
#黒い石のすぐ右に白い石があるような箇所がないようにしたいです。 

n = int(input())
state = input()
state_list = list(state)
state_list.append(" ")

#specify the locate causing problem
prob_locate = []
prob_num = 0
for i in range(n):
	if state_list[i] == "#" and state_list[i+1] == ".":
		prob_locate.append(i)
		prob_num += 1
if prob_num == 0:
		print(0)
		exit()

#finding optimal position
import random
while prob_num > 0:
	for i in prob_locate:
		r_0 = random.randint(0,1)
		r_1 = random.randint(0,1)
		state_list[(i + r_0)] = ["#", "."][r_1]
	prob_locate = []
	prob_num = 0
	for i in range(n):
		if state_list[i] == "#" and state_list[i+1] == ".":
			prob_locate.append(i)
			prob_num += 1

#checking the difference
state_list.remove(" ")
diff = 0
for i, j in zip(state_list, list(state)):
	if i != j: diff += 1

print(diff)

Submission status: Time Limit Exceeded

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

listS = list(S)
temp = 0
result = float('inf')
black = 0

for i in range(N):
    temp = listS[:i+1].count('#') + listS[i+1:].count('.')
    if result > temp:
        result = temp

count1 = listS[:N-1].count('#')
print(min(result, count1))

Submission status: Time Limit Exceeded

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

count = 0
while True:
    if "".join(S[:2]) == "#.":
        S[0] = "."
        count += 1
    if "".join(S[-2:]) == "#.":
        S[-1] = "#"
        count += 1
    S = S[2:-2]
    if not S:
        break
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
def a (str) :
	_ans = 200000
	for i in range(n) :
		k = 0
		if str[i] == "#" :
			for j in s[:i] :
				if j == "#" : k+=1
				if k > _ans : break
			for j in s[i:] : 
				if j == "." : k += 1
				if k > _ans : break
			if k<_ans : _ans = k
			if k == 0 : return 0
	if _ans == 200000 : return 0
	return (_ans)
ans = a(s)
print(ans)

Submission status: Time Limit Exceeded

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

trial_num = int(input())
stones = list(input())

min_turn_stones = 2 * 10 ** 5

for i in range(trial_num):
    tmp_min = stones[:i].count("#") + stones[i+1:].count(".")
    if (tmp_min < min_turn_stones):
        min_turn_stones = tmp_min

print(min_turn_stones)

Submission status: Time Limit Exceeded

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 

for i in range(len(s)):
	temp_ans = 0
	for j in range(0, i):
		if(s[j]) == "#":
			temp_ans += 1
	for j in range(i,len(s)):
		if (s[j] == "."):
			temp_ans += 1
	ans = min(ans, temp_ans, len(s) - dot)
 
print(ans)

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

Solution code:
import sys

a = []
for l in sys.stdin:
    a.append(l)

num = int(a[0])
haichi = a[1]

# 左側を全部白にする手数 (= 左側の黒の数)
split_left_count = [sum([x == '#' for x in haichi[:i]]) for i, k in enumerate(haichi)]

# 右側を全部黒にする手数 (= 右側の白の数)
split_right_count = [sum([x == '.' for x in haichi[i:]]) for i, k in enumerate(haichi)]

count_list = [split_right_count[i]+split_left_count[i] for i in range(len(split_right_count))]

print(min(count_list))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
a = 0;b = 0
flag = 0
cnt1 = 0;cnt2 = 0;cnt3 = 0

for i in range(n):
    if s[n-i-1] == "#":a += 1
    else:break
s = s[0:n-a]

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

while flag == 0:
    n = len(s);a = 0;c = 0
    if n == 0:
        break
    for i in range(n):
        if s[n-i-1] == ".":a += 1
        else:break
           
    while (s[n-a-c-1] == "#"):
        c += 1
        continue
        
        if c >= a:
            s = s[0:n-c-a]
            cnt3 += a
            flag = 0
        else:
            flag = 1

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

cnt3 += min(cnt1,cnt2)
print(cnt3)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import scipy as sp

def main():
    while True:
        try:
            il = [input() for i in range(2)]
            N = int(il[0])
            S = il[1]
            hoge = 1
            res = 0
            while hoge!=0:
                res = res + S.count('#.')
                S.replace('#.', '##')
                hoge = S.count('#.')
            print(res)
        except:
            break

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = n
for i in range(n + 1):
    c = i + s[i:].count('.') - s[:i].count('.') 
    r = min(r, c)
print(r)

Submission status: Time Limit Exceeded

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

start = s.find("#")
end = s.rfind(".")
black_index = [j for j in range(len(s[start:end+1])) if s[j] =="#"]
white_index = [j for j in range(len(s[start:end+1])) if s[j] =="."]

if len(black_index) == 0 or len(white_index) ==0:
    print(0)
else:
    black_new_index = [black_index[j] for j in range(len(black_index)) if black_index[j]+1 not in black_index]
    white_new_index = [white_index[j] for j in range(len(white_index)) if white_index[j]+1 not in white_index]
    print(min(max(len(black_new_index),1), max(len(white_new_index),1)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = input()
l = []
c = 0
d = 0
ans = 10**6

if s.count("#")==0:
    print(0)
else:
    for i in range(N):
        if s[i] == "#":
            if d != 0:
                c += 1
                d += 1
            else:
                l.append(c + s[i:N+1].count("."))
                c += 1
                d += 1
        d = 0
    print(min(l))

Submission status: Time Limit Exceeded

Solution code:
def test():
    global count, list_str
    for i, data in enumerate(list_str):
        try:
            if data == '#' and list_str[i + 1] == '.':
                list_str[i + 1] = '#'
                count += 1
        except IndexError:
            pass


N = input()
S = input()
count = 0

list_str = list(S)

for i in range(len(S)):
    test()

print(count)

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]=='#.':
            j = 1
            while i-j>=0 and S[i-j]=='#':
                j += 1
            k = 1
            while i+1+k<N and S[i+1+k]=='.':
                k += 1
            if j <= k:
                ans += j
                temp = ''
                for jj in range(j):
                    temp += '.'
                S = S[:i-j+1] + temp + S[i+1:]
            else:
                ans += k
                temp = ''
                for kk in range(k):
                    temp += '#'
                S = S[:i+1] + temp + S[i+1+k:]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt1 = 0
S1 = S
for i in range(1, N):
  for j in range(1, N):
    if S1[j-1] == "#" and S1[j] == ".":
      if S.count("#") > S.count("."):
        S1 = S1.replace(S1[j-1], ".", 1)
      else:
        S1 = S1.replace(S1[j], "#", 1)
      cnt1 += 1
print(min(cnt1,S.count("#"), S.count(".")))

Submission status: Time Limit Exceeded

Solution code:
import numpy

n = int(input())
char = input()

char_list = numpy.array(list(char),dtype=str)

bools = char_list == "#"

result = []

for i in range(n+1):
    result.append(sum(bools[:i]) + len(bools[i:]) - sum(bools[i:]))
print(min(result))

Submission status: Time Limit Exceeded

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

for i in range(N+1):
    cnt = N-(S[:i].count(".")+S[i:].count("#"))
    ans = min(ans,cnt)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for j in range(n):
  cnt = 0
  for i in range(j):
    if s[i] != ".":
      cnt += 1
      if cnt>mincnt:
          break 
  for i in range(j+1,n):
    if s[i] != "#":
      cnt += 1
      if cnt>mincnt:
          break  
      
  if cnt < mincnt:
    mincnt = cnt
  
  

print(mincnt)

Submission status: Time Limit Exceeded

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

def count_diff(str1, str2):
  return sum([1 if j != str2[i] else 0 for i, j in enumerate(str1)])

if '.' not in s or '#' not in s:
  print(0)
elif '#.' not in s:
  print(0)
else:
  if s[n-1] == '#':
    s = s[:s.rfind('.')+1]
  if '.' == s[0]:
    s = s[s.find('#'):]
  len_s = len(s)
  print(min([count_diff(s, te) for te in ['.' * i + '#' * (len_s - i) for i in range(0, len_s+1)]]))
  
  #print(min(s.count('.'), s.count('#')))

Submission status: Time Limit Exceeded

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

i = S.find('#')
if i < 0:
    print(0)
    sys.exit()

a = 0
Min = 200000
for j in range(i, n):
    if S[j:j+1] == '#':
        b = S[j:n].count('.')
        Min = min(Min, a+b)
        a += 1
        
print(Min)

Submission status: Time Limit Exceeded

Solution code:
import numpy

n = int(input())
char = input()

char_list = numpy.array(list(char),dtype=str)

bools = char_list == "#"
rev_bools = char_list == "."

result = []

for i in range(n+1):
    result.append(sum(bools[:i]) + sum(rev_bools[i:]))
print(min(result))

Submission status: Time Limit Exceeded

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

w = S.count(".")
b = S.count("#")


ans = 0
for i in range(min(w,b)):
    ans += S.count("#.")
    S = S.replace("#.","")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(str(input()))
lis = []
for i in range(1,n):
    lis.append(s[:i:].count("#") + s[i::].count("."))
lis.append(s.count("#"))
lis.append(s.count("."))
lis.sort()
print(lis[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
S_list = list(S)
i = 0
count = 0
for i in range(N-1):
    if S_list.count("#") >= S_list.count("."):
        if(S_list[i] == "#" and S_list[i+1] == "."):
            S_list[i+1] = "#"
            count += 1
            i += 1
        else:
            i += 1
    else:
        if(S_list[i] == "#" and S_list[i+1] == "."):
            S_list[i+1] = "."
            count += 1
            i += 1
        else:
            i += 1        
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [i for i in input()]

flg = 0
idx = 0
for i in S:
    if i == '#' and flg == 0:
        flg = 1
        break
    idx += 1

if flg == 1:
    a = S[idx+1:].count('.')
    b = S[idx:].count('#')
    if S[-1] == '#':
        for ct in range(1, N+1):
            if S[-ct] == '.':
                break
        b -= ct-1
    ans = min(a, b)
else:
    ans = 0
key = [0]
S = S[idx:]
for i in range(1, len(S)):
    if S[i] == '#' and S[i-1] == '.':
        key.append(i)
if len(key) > 2:
    for i in range(len(key)-1):
        c = S[:key[i+1]].count('#') + S[key[i+1]:].count('.')
        ans = min(ans, c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = n
if s.count('#') == n or s.count('.') == n:
    ans = 0
    print(ans)
else:
    for i in range(n):
        num = s[:i].count('#') + s[i:].count('.')
        if num < ans:
            ans = num
    print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

    return n, s


def split_sentence(s):
    temp = s[0]
    result = []

    for i in range(1, len(s)):
        if temp[-1] == '.' and s[i] == '#':
            result.append(temp)
            temp = s[i]
        else:
            temp += s[i]

    result.append(temp)

    return result


def count_white(t):
    return sum([1 for c in t if c == '.'])

def count_black(t):
    return sum([1 for c in t if c == '#'])

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

    min_c = n

    for i in range(n + 1):
        left = s[:i]
        right = s[i:]

        left_b = count_black(left)
        right_w = count_white(right)



        if left_b + right_w < min_c:
            min_c = left_b + right_w

    print(min_c)