Problem:


Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
import sys


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

    answer = sys.maxsize
    for i in range(0, N):
       left = S[:i:]
       center = S[i]
       right = S[i+1::]
       cnt = left.count("W") + right.count("E")

       answer = min(answer, cnt)

    print(answer)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S2 = [s == 'W' for s in S]

min_num = N
for i in range(N):
    min_num = min([min_num, sum(S2[:i]) + N - i - sum(S2[i + 1:]) - 1])

print(min_num)

Submission status: Time Limit Exceeded

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

min_num = N
for i in range(N):
    min_num = min([min_num, S[:i].count('W') + S[i + 1:].count('E')])

print(min_num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
l = [0]*n
for i in range(n):
    l[i] = s[0:i].count('W') + s[i+1:].count('E')
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
import re

def calc(people):
    ans = 10 ** 6

    for i, p in enumerate(people):
        Ws = people[0:i].count("W")
        Es = people[i+1:].count("E")

        ans = min(ans, Ws + Es)
    
    return ans


if __name__ == "__main__":
    _ = input()
    people = input()

    print(calc(people))

Submission status: Time Limit Exceeded

Solution code:
def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    persons = list(map(lambda x: x, inputs[0]))
    N = len(persons)
    counts = []
    for leader, _ in enumerate(persons):
        count = 0
        for p in range(0, leader):
            if persons[p] == "W":
                count += 1
        for p in range(leader + 1, N):
            if persons[p] == "E":
                count += 1
        counts.append(count)
    min = counts[0]
    for count in counts:
        if min > count:
            min = count

    return min


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Time Limit Exceeded

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

res = float('inf')
for i in range(N):
    len_w = S[:i].count('W')
    len_e = S[i+1:].count('E')
    res = min(res, len_w + len_e)
    
print(res)

Submission status: Time Limit Exceeded

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

ans = float('inf')
for i in range(N):
    cnt = S[0:i].count('W') + S[i+1:].count('E')
    if ans > cnt:
        ans = cnt

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
w=ans=9**9
for i in range(N):
    e=S[:i].count("W")
    w=S[i:].count("E")
    ans=min(ans,e+w)
    if w==0:break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
w=ans=9**9
for i in range(N):
    e=S[:i].count("W")
    w=S[i:].count("E")
    ans=min(ans,e+w)
    if w==0:break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=9**9
for i in range(N):
    e=S[:i].count("W")
    w=S[i:].count("E")
    ans=min(ans,e+w)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 10**10
for i in range(n):
    l = s[:i]
    r = s[i+1:]
    ans = min(ans,l.count('W')+r.count('E'))

print(ans)

Submission status: Time Limit Exceeded

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

INF = 10 ** 12
min_cnt = INF

for i in range(N+1) : 
    cnt = 0
    cnt += S[0:i].count("W")
    cnt += S[i:N].count("E")
    
    min_cnt = min(min_cnt, cnt)
    
print(min_cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ss = [0 if i == 'E' else 1 for i in s]
result = [sum(ss[:i-1])+len(ss[i:])-sum(ss[i:]) for i in range(n)]
print(min(result))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ss = [0 if i == 'E' else 1 for i in s]
result = [sum(ss[:i-1])+len(ss[i:])-sum(ss[i:]) for i in range(1, n)]
print(min(result))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ss = [0 if i == 'E' else 1 for i in s]
result = []
for i in range(1, n):
  result.append(sum(ss[:i-1])+len(ss[i:])-sum(ss[i:]))
print(min(result))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=list(input())
w=n-S.count('W')
p=[]
for i in range(n):
    s=w-i+2*S[:i].count('W')
    p.append(s)

print(min(p))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=list(input())
p=[]
for i in range(n):
    s=S[:i].count('W')+S[i+1:].count('E')
    p.append(s)

print(min(p))

Submission status: Time Limit Exceeded

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

ans = n
for i in range(n):
	ans = min((s[:i].count("W")+s[i+1:].count("E")),ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

w = deque([0])
e = deque([0])

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

# w = [0]
# e = [0]

tmp_w = 0
tmp_e = 0

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        tmp_w += 1
        w.append(tmp_w)
    else:
        w.append(tmp_w)

    if (s[j+1] == 'E'):
        tmp_e += 1
        e.appendleft(tmp_e)
    else:
        e.appendleft(tmp_e)

ans = 10 ** 6 
for i in range(n):
    ans = w[i] + e[i] if w[i] + e[i] < ans else ans

print(ans)

Submission status: Time Limit Exceeded

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

ans = 300010
for i in range(N):
    tmp = 0
    for left in range(i):
        if S[left] == 'W':
            tmp += 1
    for right in range(i + 1, N):
        if S[right] == 'E':
            tmp += 1
    ans = min(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

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

w = [0]
e = [0]

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])

    if (s[j+1] == 'E'):
        e.insert(0, e[0] + 1)
    else:
        e.insert(0, e[0])


# for j in range(n-2, -1, -1):

#     if (s[j+1] == 'E'):
#         e.insert(0, e[0] + 1)
#     else:
#         e.insert(0, e[0])


result = []

for i in range(n):
    result.append(e[i] + w[i])

print(min(result))

Submission status: Time Limit Exceeded

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

w = [0]
e = [0]

for i in range(1, n):

    if (s[i-1] == 'W'):
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])


for j in range(n-2, -1, -1):

    if (s[j+1] == 'E'):
        e.insert(0, e[0] + 1)
    else:
        e.insert(0, e[0])


result = []

for i in range(n):
    result.append(e[i] + w[i])

print(min(result))

Submission status: Time Limit Exceeded

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

result = []

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

    cnt1 = left.count('W')
    cnt2 = right.count('E')

    result.append(cnt1 + cnt2)

print(min(result))

Submission status: Time Limit Exceeded

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

    min = N
    for i in range(N):
        score = S[0:i].count('W') + S[i+1:N].count('E')
        if min > score:
            min = score

    print(min)

Submission status: Time Limit Exceeded

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

N = int(input())
S = list(input())
ans = [""] * N

for i in range(N):
    ans[i] = S[:i].count('W') + S[(i+1):].count('E')

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
p = N
for i in range(N):
    p = min(p, S[:i].count("W") + S[i + 1:].count("E"))
print(p)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input();ew=[]
ans=[]
for i in s:
	if i=='W': ew.append(0)
	else: ew.append(1)

for i in range(n):
	t=[]
	t.extend([1]*i)
	t.append(ew[i])
	t.extend([0]*(n-i-1))
	a=list(map(lambda x,y: abs(x-y), ew,t))
	ans.append(sum(a))
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=[0]*n
for i in range(n):
	for j in range(i):
		if s[j]=='W': ans[i]+=1
	for k in range(i,n):
		if s[k]=='E': ans[i]+=1
print(min(ans))

Submission status: Time Limit Exceeded

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

for i in range(N):
    ans[i]+= S[:i].count("W")
    ans[i] += S[i+1:].count("E")
    
print(min(ans))

Submission status: Time Limit Exceeded

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

e = []
w = []
for c in s:
    if c == "E":
        e.append(1)
        w.append(0)
    else:
        e.append(0)
        w.append(1)

for i in range(n):
    mn = min(mn, sum(w[:i])+sum(e[i+1:]))
print(mn)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
mn = 300000
for i in range(n):
    mn = min(mn, s[:i].count("W")+s[i+1:].count("E"))
print(mn)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
N = int(input())
S = input()
l = []
for t in range(len(S)):
    l.append(S[t])
mlist=[]
for t in range(N):
    l1 = l[0:t]
    l2 = l[t+1:len(l)]
    h = 0
    p1 = Counter(l1)
    p2 = Counter(l2)
    mlist.append(p1["W"] + p2["E"])
print(min(mlist))

Submission status: Time Limit Exceeded

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

counts = []

for i in range(n):
    left = a[:i]
    right = a[i+1:]
    counts.append((len(left) - left.count('E')) + (len(right) - right.count('W')))

print(min(counts))

Submission status: Time Limit Exceeded

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

ruiseki_w = [0]
ruiseki = 0
for i in s[:-1]:
    if i == 'W':
        ruiseki += 1
        ruiseki_w.append(ruiseki)
    else:
        ruiseki_w.append(ruiseki)

ruiseki_e = [0]
ruiseki = 0
for i in s[::-1][:-1]:
    if i == 'E':
        ruiseki += 1
        ruiseki_e.append(ruiseki)
    else:
        ruiseki_e.append(ruiseki)
        
ninzu = [ruiseki_w[i] + ruiseki_e[::-1][i] for i in range(n)]
print(min(ninzu))

Submission status: Time Limit Exceeded

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

count_w =[s[:i].count('W') for i in range(n)]
count_e =[s[::-1][:i].count('E') for i in range(n)]

ninzu = n
for i in range(n):
    ninzu = min(ninzu, count_w[i]+count_e[-(i+1)])
print(ninzu)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N = int(raw_input())
    S = raw_input() # str
    min_need_change = N
    for i in range(N):
        count = 0
        for j in range(0, i):
            if S[j] == 'W':
                count += 1
        for j in range(i+1, N):
            if S[j] == 'E':
                count += 1
        if count < min_need_change:
            min_need_change = count
    print str(min_need_change)

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
l = []
x = []
y = []
c = 0
for i in range(n):
    a = s[:i]
    b = s[i:]
    l.append(a.count("W"))
    x.append(b.count("E"))
for i in range(n):
    c = l[i] + x[i]
    y.append(c)
print(int(min(y)))

Submission status: Time Limit Exceeded

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

counts = []
mini = N
for n in range(N):
    count = 0
    for i in range(N):
        if i == n:
            continue
        if n<i and people[i]=='E':
            count +=1
        if n>i and people[i]=='W':
            count +=1
        if count >= mini:
            break
    mini = min([count, mini])
    counts.append(count)

print(min(counts))

Submission status: Time Limit Exceeded

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

counts = []
for n in range(N):
    count = 0
    for i in range(N):
        if i == n:
            continue
        if n<i and people[i]=='E':
            count +=1
        if n>i and people[i]=='W':
            count +=1
    counts.append(count)

print(min(counts))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = input()


ans = 3 * 10 ** 5
for  i in range(n):
  tmp = s[:i].count("W") + s[i+1:].count("E")
  if ans > tmp:
    ans = tmp
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
print( (lambda N, S: min( [S[:i].count('W') + S[i+1:].count('E') for i in range(N)] ))(int(input()), input()) )

Submission status: Time Limit Exceeded

Solution code:
def solve(N, S): print(min( [S[:i].count('W') + S[i+1:].count('E') for i in range(N)] ))

solve( int(input()), input() )

Submission status: Time Limit Exceeded

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

ans = float("inf")
E = S.count("E")
W = S.count("W")
for i, v in enumerate(S):
    left = S[:i]
    left_E = left.count("E")
    left_W = left.count("W")
    if v == "E":
        right_E = E - left_E - 1
    else:
        right_E = E - left_E
    ans = min(ans, left_W + right_E)


print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
w = [s[:i+1].count("W") for i in range(n)]
e = [s[:i+1].count("E") for i in range(n)]

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Apre = [1 if(S[i]=='E') else 0 for i in range(N)]
A = [sum(Apre[0:i+1]) for i in range(N)]
ans = min(N-1-A[N-2], A[N-1]-A[0])
for i in range(1,N):
  ans = min(ans, i-A[i-1]+A[N-1]-A[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
A = [1 if(S[i]=='E') else 0 for i in range(N)]
ans = min(sum(A[1:]), N-sum(A[:]))
for i in range(1,N):
  ans = min(ans, i - sum(A[:i])+ sum(A[i+1:]))
print(ans)

Submission status: Time Limit Exceeded

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

ans = []
for i in range(n):
    count = 0
    for i1 in range(i):
        if s[i1] == 'W':
            count += 1
    ans1 = count
    count = 0
    for i2 in range(i+1, n):
        if s[i2] == 'E':
            count += 1
    ans2 = count
    ans.append(ans1 + ans2)

print(min(ans))

Submission status: Time Limit Exceeded

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

Is = [1 if s == 'W' else 0 for s in S]

res = N
for i in range(0,N):
 left = Is[:max(i - 1,0)]
 right = Is[min(i + 1,N - 1):]
 tmp = sum(left) + (len(right) - sum(right))
 res = min(tmp,res)

print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min = min(S[1:].count("E"), S[:N-1].count("W"))

for i in range(1,N-1):
  p = S[:i].count("W") + S[i + 1:].count("E")
  if p < min:
    min = p
print(min)

Submission status: Time Limit Exceeded

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

cw = 0
ce = 0
for i in range(N):
  if S[i] == "W":
    cw += 1
  Lw.append(cw)
  if S[N - 1 - i] == "E":
    ce += 1
  Le.insert(0, ce)

min = min(Le[1], Lw[N-2])
for i in range(1, N-1):
  p = Le[i + 1] + Lw[i - 1]
  if p < min:
    min = p

print(min)

Submission status: Time Limit Exceeded

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

ch = n - 1
for i in range(n):
  ch = min(ch, sum(x == "W" for x in s[:i]) + sum(x == "E" for x in s[i + 1:]))
print(ch)

Submission status: Time Limit Exceeded

Solution code:
n,s=int(input()),input()
for i in range(len(s)):
  c=s[:i].count("W")+s[i+1:].count("E")  
  n=c if n>c else n
print(n)

Submission status: Time Limit Exceeded

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

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

ans = float("inf")
for i in range(n):
    cnt = 0
    for j in range(n):
        if j < i and s[j] == "W":
            cnt += 1
        if i < j and s[j] == "E":
            cnt += 1
    ans = min(ans,cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
st = []
memo = {}
for a in range(n):
    ct = 0
    tmp = 0
    if a > 0: tmp += s[:a].count('W')
    if a < len(s) - 1: tmp += s[a+1:].count('E')
    st.append(tmp)
print(min(st))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
st = []
for a in range(n):
    tmp = []
    for _ in range(a): tmp.append('E')
    tmp.append(s[a])
    for _ in range(a+1, n): tmp.append('W')
    st.append(len([x for x in range(len(tmp)) if tmp[x] != s[x]]))
print(min(st))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(N) :
    num=0
    west=S.count("W")
    if S[i]=="W" :
        west-=1
    for j in range(i) :
        if S[j]=="W" :
            num+=1
    num=2*num+N-i-1-west
    count.append(num)
print(min(count))

Submission status: Time Limit Exceeded

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

for i in range(N) :
    num=0
    for j in range(i) :
        if S[j]!="E" :
            num+=1
    for k in range(i,N) :
        if S[k]!="W" :
            num+=1
    count.append(num)
print(min(count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(lambda x: 1 if x=='E' else 0, list(input())))
att_list = []

for i in range(N):
    count = 0
    for j in range(N):
        if j < i:
            if S[j] == 1:
                continue
            else:
                count += 1
        if j > i:
            if S[j] == 0:
                continue
            else:
                count += 1
    att_list.append(count)

print(min(att_list))

Submission status: Time Limit Exceeded

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

for i in range(N):
    count = 0
    for j in range(N):
        if j < i:
            if S[j] == "E":
                continue
            else:
                count += 1
        if j > i:
            if S[j] == "W":
                continue
            else:
                count += 1
    att_list.append(count)

print(min(att_list))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
a = [(list(s)[:i]).count("W") + ((list(s)[i:])).count("E") for i in range(len(s))]
print(min(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = str(input())

c = 0
p = n

for l in range(n):
    c += a.count('W', 0, l)
    if c > p:
        break
    c += a.count('E', l+1)
    if p > c:
        p = c
    elif c == 0:
        p = c
        break
    c = 0
print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = str(input())

c = 0
p = n

for l in range(n):
    c += a.count('W', 0, l)
    c += a.count('E', l+1)
    if p > c:
        p = c
    c = 0
print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = str(input())

c = 0
p = n
for l in range(n):
    for i in range(n):
        if i < l and a[i] == 'W':
            c += 1
        elif i == l:
            pass
        elif i > l and a[i] == 'E':
            c += 1
        elif c > p:
            break
    if p > c:
        p = c
    c = 0
print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = str(input())

c = 0
p = n
for l in range(n):
    for i in range(n):
        if i < l and a[i] == 'W':
            c += 1
        elif i == l:
            pass
        elif i > l and a[i] == 'E':
            c += 1
    if p > c:
        p = c
    c = 0
print(p)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
tmp=[ s[:i].count("W")+s[i+1:].count("E") for i in range(n) ]
print(min(tmp))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
ans=3*10**5+1
for i in range(n):
    tmp=s[:i].count("W")+s[i+1:].count("E")
    #tmp=x.count("W")+y[1:].count("E")
    if ans>tmp:
        ans=tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
ans=3*10**5+1
for i in range(n):
    x,y=s[:i],s[i:]
    tmp=x.count("W")+y[1:].count("E")
    if ans>tmp:
        ans=tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = ["E"] + list(str(input())) + ["W"]
res = 10e9
for i in range(n+2) :
  res = min(res, s[:i].count("W") + s[i+1:].count("E"))
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
M = 3 * 10 ** 5
#print(M)
for i in range(N-1):
    A = S[:i]
    B = S[i+1:]
#    print(A,B,A.count("W")+B.count("E"))
    M = min(M, A.count("W")+B.count("E"))
print(M)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
result = 3*10**5
for i in range(0, N):
    count = 0
    for index, s in enumerate(S):
        if index == i:
            continue
        if index < i and s == "W":
            count += 1
        if index > i and s == "E":
            count += 1

    if result > count:
        result = count
print(result)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
l=[]
for i in range(n):
    c=0
    c+=s[:i].count("W")
    if i!=n-1:
        c+=s[i+1:].count("E")
    l.append(c)
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
N, S = int(input()), str(input())
S = S.translate(str.maketrans({'E':'0', 'W':'1'}))
cost = N
for i in range(N):
    res = '0'* (i-1) + '1' * (N-1-i)
    S_test = S[:i-1]+ S[i+1:]
    bit = int(res, 2)^int(S_test, 2)
    cost = min(cost, bin(bit).count('1'))
print(cost)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[input()]*n
c=[[0]*n]*n
ans=[0]*n
for i in range(n):
    for j in range(n):    
        if i>j and s[i][j]!='E':
            c[i][j]=1
        elif i<j and s[i][j]!='W':
            c[i][j]=1
    c[i][i]=0
    ans[i]=sum(c[0])
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
cnt=[]
for i in range(N):
    count=0
    for j in range(N):
        if j<i and S[j]=='W':
            count+=1
        elif j>i and S[j]=='E':
            count+=1
    cnt.append(count)
print(min(cnt))

Submission status: Time Limit Exceeded

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

E = [0]*N
W = [0]*N
for i in range(N):
    if S[i] == 'E':
        E[i] = 1
    else:
        W[i] = 1
    
ans = N
for l in range(N):
    num = sum(E[l+1:])
    num += sum(W[:l])
    if ans >= num:
        ans = num

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
change_direction = [0] * n
count = 0 
for i in range(len(s)):
    change_direction[i] = s[:i].count('W') + s[i+1:].count('E')
print(min(change_direction))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
change_direction = []
for i in range(len(s)):
    count = 0
    for j in range(len(s)):
        if j < i and s[j] == 'W':
            count += 1
            continue
        if i < j and s[j] == 'E':
            count += 1
            continue
    change_direction.append(count)
print(min(change_direction))

Submission status: Time Limit Exceeded

Solution code:
import sys

n = int(input())
d = list(input())

minNum = sys.maxsize

for i in range(n):
    count = 0
    if i == 0:
        count = d[i+1:].count("E")
    elif i == n-1:
        count = d[:i].count("W")
    else:
        count += d[:i].count("W")
        count += d[i+1:].count("E")
    if count < minNum:
        minNum = count

print(minNum)

Submission status: Time Limit Exceeded

Solution code:
import sys

n = int(input())
d = list(input())

minNum = sys.maxsize

for i in range(n):
    count = 0
    if i == 0:
        newD = d[i+1:]
        for j in newD:
            if j == "E":
                count += 1
    elif i == n-1:
        newD = d[:i]
        for j in newD:
            if j == "W":
                count += 1
    else:
        left = d[:i]
        right = d[i+1:]
        for p in left:
            if p == "W":
                count += 1
        for q in right:
            if q == "E":
                count += 1
    if count < minNum:
        minNum = count

print(minNum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
m=N
for i in range(N):
    a=S[0:i]
    W=a.count('W')
    b=S[i+1:]    
    E=b.count('E')
    num=W+E
    if m>num:
        m=num
print(m)

Submission status: Time Limit Exceeded

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

count_min = 10**5

for i in range(n):
    s0 = s[:i].count('W')
    s1 = s[i+1:].count('E')
    count = s0+s1
    count_min = min(count_min, count)    
 
print(count_min)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
def main(N, S):
    total = N
    for i in range(N):
        total = min(total, S[:i].count("W") + S[i+1:].count("E"))
    print(total)
    
if(__name__ == "__main__"):
    N = int(input())
    S = input()
    
    main(N, S)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = [i for i in input()]
#print(seq)
score = []
score_ = []
ans = []
for i in range(N):
    score.append(seq[i+1:].count("E"))
    score_.append(seq[:i].count("W"))
#print(score)
#print(score_)
for i in range(len(seq)):
    ans.append(score[i]+score_[i])
#print(ans)
print(ans[ans.index(min(ans))])

Submission status: Time Limit Exceeded

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

for i in range(n):
	a = s[:i].count('W')
	b = s[i:].count('E')
	answer = min(answer, a+b)
	
print(answer)

Submission status: Time Limit Exceeded

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

original_w = []
prev = 0
for i in range(N):
    if S[i] == 'W':
        prev += 1
    original_w.append(prev)

original_e = []
prev = 0
for i in range(N - 1, -1, -1):
    if S[i] == 'E':
        prev += 1
    original_e.insert(0, prev)

print(min([original_w[i - 1] + original_e[i + 1] for i in range(1, N - 1)] + [original_e[1], original_w[N - 2]]))

Submission status: Time Limit Exceeded

Solution code:
def solve(string):
    n, s = string.split()
    return str(min([s.count("W", 0, i) + s.count("E", i + 1) for i in range(int(n))]))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=1000000000000000000000
for i in range(n):
    before=s[0:i]
    after=s[i+1:]
    if before.count("W")+after.count("E")<ans:
        ans=before.count("W")+after.count("E")
print(ans)

Submission status: Time Limit Exceeded

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

ans = []
for i in range(n-1):
    w = s[:i].count('W')
    e = s[i+1:].count('E')
    ans.append(w+e)

ans.append(s[:n].count('W'))

print(min(ans))

Submission status: Time Limit Exceeded

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

def main():
	N = int(input())
	tmp = input()
	lines = list(tmp)
	
	ans_min = 100001
	for i, val in enumerate(lines):
		ans1 = lines[i:].count("E")
		ans2 = lines[:i].count("W")
		ans = ans1 + ans2
		
		if ans_min > ans:
			ans_min = ans
	
	print(ans_min)
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

def main():
	N = int(input())
	tmp = input()
	lines = list(tmp)
	
	ans_min = 100001
	for i, val in enumerate(lines):
		ans = 0
		for j, vol in enumerate(lines):
			if i == j:
				continue
			if i < j and vol == "E":
				ans += 1
			if j < i and vol == "W":
				ans += 1
		if ans_min > ans:
			ans_min = ans
	
	print(ans_min)
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = N
for i in range(N):
    count = min(count , S[:i].count("W") + S[i+1:].count("E"))
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Ss = [char for char in input()]
anss = []
for s in range(len(Ss)):
    anss.append(Ss[s:].count("E")+Ss[:s].count("W"))
print(min(anss))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Ss = [char for char in input()]
anss = []
for s in range(len(Ss)):
    anss.append(len([num for num in Ss[s:] if num=="E"])+len(([num for num in Ss[:s] if num=="W"])))
print(min(anss))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
answer = n
for i in range(n):
  total = s[i+1:].count('E') + s[:i].count('W')
  answer = min(answer, total)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min_turn = 300000
for i in range(N):
    if i == 0:
        turn = S[1:].count('E')
    elif i == (N-1):
        turn = S[0:i].count('W')
    else:
        turn = S[0:i].count('W') + S[i+1:].count('E')
    if min_turn > turn:
        min_turn = turn
print(min_turn)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
sl = list(S)
min_turn = 300000
for i in range(N):
    if i == 0:
        turn = sl[1:].count('E')
    elif i == (N-1):
        turn = sl[0:i].count('W')
    else:
        turn = sl[0:i].count('W') + sl[i+1:].count('E')
    if min_turn > turn:
        min_turn = turn
print(min_turn)

Submission status: Time Limit Exceeded

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

result = 3 * 10 ** 5
for i in range(N):
    W = S[:i]
    E = S[i+1:]
    flip_count = W.count('W') + E.count('E')
    result = min(flip_count, result)
print(result)

Submission status: Time Limit Exceeded

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

ans = 1e9
for i in range(N):
  tmp = 0
  l, r = S[:i], S[i+1:]
  tmp += l.count("W") + r.count("E")
  ans = min(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a0=[]
for i in range(n):
    left=s[:i:].count("W")
    right=s[i+1:].count("E")
    a0.append(left+right)
a=sorted(a0)
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
min=300000
for i in range(n):
    number=s[:i:].count("W")+s[i+1:].count("E")
    if min>number:
        min=number
print(min)