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:
# coding: utf-8

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


def kawaru_hito(s, leader):
    left_of_leader = s[:leader]
    right_of_leader = s[leader+1:]
    return left_of_leader.count("W") + right_of_leader.count("E")

res = 1000000
for i in range(len(S)):
    res = min(res, kawaru_hito(S, i))

print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
a0 = len(S)
for i in range(N):
  A=S[0:i]
  B=S[i+1:N]
  a=A.count("W")+B.count("E")
  if a0>a:
    a0=a
print(a0)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
for i in range(0,N):
    a=S[0:i].count('E')
    b=S[i:N+1].count('W')
    ans=max(ans,a+b)
print(N-ans)

Submission status: Time Limit Exceeded

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

ans = 10**6
for i in range(N+1):
  tmp = S[:i].count('W') + S[i+1:].count('E')
  ans = min(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

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

num_eas = s.count('E')



minres = 1000000
for i in range(n):
    a = s[:i].count("W")
    res = 2*a + num_eas - i
    if res < minres:
        minres = res

print(minres)

Submission status: Time Limit Exceeded

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

num_wes = s.count('W')
num_eas = s.count('E')


res = []
for i in range(n):
    a = s[:i].count("W")
    res += [2*a + num_eas - i]
    
print(min(res))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

minres = 1000000
for i in range(n):
    res = s[:i].count("W") + s[i+1:].count("E")
    if res < minres:
        minres = res

print(minres)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S =list(input())
count = float("inf")

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(n):
    res += [s[:i].count("W") + s[i+1:].count("E")]

print(min(res))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
N = int(input())
data = list(input())
bef_ans = 0
for i in range(N-1):#リーダー
  count = 0
  for t in range(N):#向いてるか
    if i < t:
      if data[t] == 'W':
        count += 1
    if i > t:
      if data[t] == 'E':
        count += 1
  if bef_ans < count:
    bef_ans = count
print(N - bef_ans-1)

Submission status: Time Limit Exceeded

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

for i in range(N):
    number.append(S.count('W', 0, i) + S.count('E', i, N))

print(min(number))

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

min = N

for i in range(N):
    S = sum(W[0:i])+sum(E[i+1:N])
    if S < min:
        min = S
    
print(min)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    ss = list(input())

    ans = float("inf")
    for i in range(n):
        ans_temp = 0
        for j in range(n):
            if j < i and ss[j] == 'W':
                ans_temp += 1
            if j > i and ss[j] == 'E':
                ans_temp += 1
            if ans_temp >= ans:
                break
        ans = min(ans, ans_temp)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
v=0
for i,c in enumerate(s):
    if c=='W':
        v+=(2**(n-i-1))
u=(2**n)-1
m=float('inf')
for i in range(n):
    v2=bin(v)[2:].zfill(n)
    u2=bin(u)[2:].zfill(n)
    v3=int(v2[:i]+v2[i+1:],2)
    u3=int(u2[:i]+u2[i+1:],2)
    m=min(m,bin(v3^u3).count('1'))
    u>>=1
print(m)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ppl = list(input())


buff = []

for i in range(N):
    n = ppl[:i].count('W') + ppl[i+1:].count('E')
    buff.append(n)

print(min(buff))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

S = input()

min_num = N+1

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def main():
	N=int(input())
	S=input()
	ans=N
	cnt=0
	for i in range(N):
		cnt+=S[:i].count("W")
		cnt+=S[i:].count("E")
		ans=min(ans,cnt)
		cnt=0
	print(ans)
if __name__=='__main__':
	main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = list(input())
L = []
for i in range(n):
  if S[i] == "E":
    L.append(0)
  else:
    L.append(1)

if n == 2:
  if L == [1,0] or [0,1]:
    print(1)
    exit()
  else:
    print(0)
    exit()
    
ans = min(L[1:n+1].count(0),sum(L[0:n]))
if ans == 0:
  print(0)
  exit()

for i in range(1,n-2):
  ans = min(ans, L[0:i+1].count(1)+L[i+2:n].count(0))
  if ans == 0:
    print(ans)
    exit()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = list(input())
L = []
for i in range(n):
  if S[i] == "E":
    L.append(0)
  else:
    L.append(1)

if n == 2:
  if L == [1,0] or [0,1]:
    print(1)
    exit()
  else:
    print(0)
    exit()
    
ans1 = sum((1-L[i]) for i in range(1,n))
ans2 = sum(L[0:n])
ans = min(ans1,ans2)
if ans == 0:
  print(0)
  exit()

for i in range(1,n-2):
  a = sum(L[0:i+1])
  b = n-2-i-sum(L[i+2:n])
  ans = min(ans, a+b)
  if ans == 0:
    print(ans)
    exit()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
s = input()
minn = n
for i in range(n):
    l = s[:i].count("W")
    r = s[i + 1:].count("E")
    if minn > l + r:
        minn = l + r
print(minn)

Submission status: Time Limit Exceeded

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

import os
import sys


# ac_py.main
# Date: 2020/06/14
# Filename: main 
# Author: acto_mini

def main():
    N = int(input())
    S = input()
    ans = 1000000000
    for i in range(N):
        count = 0
        count += S[0:i].count("W")
        count += S[i+1:N].count("E")
        ans = min(ans, count)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = len(s)

for i in range(len(s)):
    func  = lambda x, y, v: y < x and v != "E" or y > x and v != "W"
    tmp = len([1 for j, v in enumerate(s) if func(i, j, v)])

    ans = min(tmp, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 21:52:40 2020

@author: naoki
"""

N = int(input()) # 並んでいる人数N

S = input() # 向いている方向
    
change = [0 for i in range(N)] # 向きを変えた回数を格納

for i in range(N):
    change[i] += S.count("W",0,i) # リーダーよりも西側にいるときに西に向いている人数をカウント
    change[i] += S.count("E",i+1,N) # リーダーよりも東側にいるときに東を向いている人数をカウント
change.sort() # 向きを変えた回数が少ない順に並べ替え
print(change[0])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 21:52:40 2020

@author: naoki
"""

N = int(input()) # 並んでいる人数N

S = input() # 向いている方向

direction = [0 for i in range(N)] 

for i in range(N):
    direction[i] = S[i] # 向いている方向を開裂で定義しなおす
    
count = [0 for i in range(N)] # 向きを変える回数

for i in range(N):
    for k in range(N):
        if k<i and direction[k] == "W": #リーダーよりも西側にいる場合は東に向かせる
            count[i] += 1
        elif i<k and direction[k] == "E": # リーダーよりも東側にいる場合は西に向かせる
            count[i] += 1
count.sort() # 値が小さい順番に並び替える
print(count[0])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
list=[]
for i in range(N):
    for j in range(i+1):
        if j<i:
            if S[j]=='W':
                count=count+1
    for k in range(i+1, N):
        if i<k:
            if S[k]=='E':
                count=count+1

    list.append(count)
    count=0

print(min(list))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    
    ans = 10**9
    for i in range(n):
        tmp = 0
        s_center = s[i] 
        for s1 in s[:i]:
            if s1 != 'E':
                tmp += 1
        for s2 in s[i+1:]:
            if s2 != 'W':
                tmp += 1
        ans = min(ans, tmp)
        
    print(ans)
    
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = len(S)+10
for i,s in enumerate(S):
    if cnt >S[:i].count("W")+S[i+1:].count("E"):
        cnt = S[:i].count("W")+S[i+1:].count("E")
print(cnt)

Submission status: Time Limit Exceeded

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

counts = []

for n in range(N):
    count = 0
    for s in S[0:n]:
        if s == "W":
            count += 1
    for s in S[n+1:]:
        if s == "E":
            count += 1
    counts.append(count)

print(min(counts))

Submission status: Time Limit Exceeded

Solution code:

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

total = 0

total_list = []

for i in range(N):
    to_E = S.count('W', 0, i)
    to_W = S.count('E', i + 1, N + 1)
    total_list.append(to_E + to_W)

print(min(total_list))

Submission status: Time Limit Exceeded

Solution code:

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

total = 0

for i in range(N):
    to_E = S.count('W', 0, i)
    to_W = S.count('E', i + 1, N + 1)
    total_temp = to_E + to_W

    if i == 0:
        total = total_temp
    elif total_temp < total:
        total = total_temp

print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
s = s.replace("W","1")
s = s.replace("E","0")
m = int(s[:])
box = [0]*(n)
p = 0
for i in range(1,n+1):
    box[i-1] = sum(list(map(int,s[:i])))
k = 0
full = box[n-1]
for j in range(n):
    if j == 0:
        p = n-full+box[0]
        if p > k:
            k = p
    elif j == n-1:
        p = (n-1)-full+int(s[n-1])
        if p > k:
            k = p
    else:
        p = j-box[j-1]+full-box[j]
        if p > k:
            k = p
print(n-k-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(sys.stdin.readline())

S = sys.stdin.readline()

set = [0]*N
for i in range(N):
    counter = 0
    counter = S[0:i].count("W") + S[i:N].count("E")
    set[i] = counter

print(min(set))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

Submission status: Time Limit Exceeded

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

for i in range(0, len(S)):
    left = S[:i]
    leader = S[i]
    right = S[i+1:]
    temp_cnt = 0
    temp_cnt += left.count('W')
    temp_cnt += right.count('E')
    cnt.append(temp_cnt)
    
output = min(cnt)

print(output)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
S = list(input())
cnt = []

for i in range(0,N-1):
    e = S[0:i].count('W')
    w = S[i+1:].count('E')
    cnt.append(e+w)
print(min(cnt))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
min_ans = 100000000000000000
for i in range(N):
    WtoE = S[0:i].count('W')
    EtoW = S[i+1:N].count('E')
    ans = (WtoE + EtoW)
    if min_ans > ans:
        min_ans = ans

print(min_ans)

Submission status: Time Limit Exceeded

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

ans = []

for i in range(N):
    WtoE = S[0:i].count('W')
    EtoW = S[i+1:N].count('E')
    ans.append(WtoE + EtoW)
print(min(ans))

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:
n = int(input())
S = input()
ans = float("inf")

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

S = input()
w = [0]*N
e = [0]*N
ans = N
for i in range(N):
    if S[i] == "W":
        w[i] = 1
    else:
        e[i] = 1

for i in range(N):
    d = sum(w[:i]) + sum(e[i+1:])
    ans = min(ans,d)
    
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(0,N-2):
  arr.append(int(S[:i].count("W"))+int(S[i+2:].count("E")))
print(min(arr))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = 999999999
for n in range(N):
    result = S[:n].count('W') + S[n + 1:].count('E')
    if result < r:
        r = result
print(r)

Submission status: Time Limit Exceeded

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

print(min([sum([1 for d in S[:l] if d == 'W']) + sum([1 for d in S[l+1:] if d == 'E']) for l in range(N)]))

Submission status: Time Limit Exceeded

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

tot = []

for l in range(N):
    tot.append(sum([1 for d in S[:l] if d == 'W']) + sum([1 for d in S[l+1:] if d == 'E']))
    
print(min(tot))

Submission status: Time Limit Exceeded

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

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

num_list = []

for i in range(n):
    num_list.append(s[i+1:].count('E') + s[:i].count('W'))

print(min(num_list))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
L = 0
R = S.count('E')
ans = L + R
for i in range(1, N):
    L = S[:i].count('W')
    R = S[i:].count('E')
    ans = min(ans, L+R)
print(ans)

Submission status: Time Limit Exceeded

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

m = float('inf')
for i, s in enumerate(S):
    cnt = 0
    for j in S[:i]:
        if j != "E":
            cnt += 1
    for k in S[i+1:]:
        cnt2 = 0
        if k != "W":
            cnt += 1
    m = min(m, cnt)
print(m)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

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

    ans = 10 * 6
    for i in range(N):
        tmp = S[:i].count('W')
        if i + 1< N:
            tmp += S[i+1:].count('E')
        ans = min(ans, tmp)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
#a = input().rstrip() 文字の時rstrip()で改行削除

N = int(input())
S = input().rstrip()

min = float("inf")
for i in range(N):
    cnt = 0
    for j in range(i):
        if S[j] != "E":
            cnt += 1
    for j in range(i+1, N):
        if S[j] != "W":
            cnt += 1
    if cnt < min:
        min = cnt

print(min)

Submission status: Time Limit Exceeded

Solution code:
"""
3*10**5
単純に両側の差を全探索だと間に合わないO(N*2)

情報を先に保存しておきそれを使う
"""

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

E_list = [0]
W_list = [0]

n = 0
for i in range(1,N):
    if S[i-1] == "W":
        n += 1
        W_list.append(n)
    else:
        W_list.append(n)

n = 0
for i in reversed(range(N-1)):
    if S[i+1] == "E":
        n += 1
        E_list.insert(0,n)
    else:
        E_list.insert(0,n)

ans = []
for i in range(N):
    ans.append(E_list[i] + W_list[i])
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
"""
3*10**5
単純に両側の差を全探索だと間に合わないO(N*2)

情報を先に保存しておきそれを使う
"""

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

E_list = [0]
W_list = [0]

n = 0
for i in range(1,N):
    if S[i-1] == "W":
        n += 1
        W_list.append(n)
    else:
        W_list.append(n)

n = 0
for i in reversed(range(N-1)):
    if S[i+1] == "E":
        n += 1
        E_list.insert(0,n)
    else:
        E_list.insert(0,n)

ans = 10*6
for i in range(N):
    ans = min(E_list[i] + W_list[i], ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
"""
3*10**5
O(NlogN)まで
"""

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

count = []
for i in range(N):
    count.append(S[0:i].count("W") + S[i+1:].count("E"))

print(min(count))

Submission status: Time Limit Exceeded

Solution code:
#https://atcoder.jp/contests/abc098/tasks/arc098_a
n=int(input())
s=input()

ans=100000000000
for i in range(n):
    count=0
    for j in range(n):
        if i==j:
            continue

        if (i<j)&(s[j]=="E"):
            count+=1
        elif (j<i)&(s[j]=="W"):
            count+=1

    ans=min(ans,count)

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(0, N):
    for j in range(0, N):
        if i != j:
            if j<i:
                if S[j] == "W":
                    A[i]+=1
                
            if j>i:
                if S[j] == "E":
                    A[i]+=1

tmp=A[0]
for i in range(1, N):
    if tmp > A[i]:
        tmp = A[i]

print(tmp)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cntList = []
for i in range(n):
    cnt = 0
    for j in range(i):
        if s[j] == "W":
            cnt += 1
    for k in range(i + 1, n):
        if s[k] == "E":
            cnt += 1

    cntList.append(cnt)

print(min(cntList))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

memo = np.array([0]*N)

for i in range(N):
    if S[i] == "E":
        memo[:i] -= 1
    else:
        memo[i+1:] -= 1

print(max(memo) * (-1))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
ln = list(map(int, input().replace('W', '0 ').replace('E', '1 ').split()))
sum_min = n - 1
for i in range(n):
    sum_min = min(sum_min, sum(ln[i+1:n]) + (i - sum(ln[0:i])))

print(sum_min)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
ln = list(input())
sum_min = n - 1
for i in range(n):
    # E->Wに向く人数
    #ln[i+1:n].count('E')
    # W->Eに向く人数
    #ln[0:i].count('W')
    sum_min = min(sum_min, ln[i+1:n].count('E') + ln[0:i].count('W'))

print(sum_min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
attention = []
for i in range(n):
    leader = s[i]
    west = s[0:i+1]
    east = s[i:len(s)]
    count_w = west.count("W")
    count_e = east.count("E")
    count = count_w +count_e
    attention.append(count)
print(min(attention) - 1)

Submission status: Time Limit Exceeded

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

ans = 3*(10**5)
for i in range(n):
  
  east = s.count('W', 0, i-1) if i else 0
  west = s.count('E', i+1) if not i==n-1 else 0
  ans = east+west if ans>east+west else ans
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
'''
考え方：
Eを0, Wを1と置くと、
0000000X11111111 と並ぶようにすればよい。
そのために、値を変える数が最小になるリーダーの位置を調べればよい

A. 全パターン試す
ある地点より左のWの数 + 右のEの数　の合計が最小になるパターンを求める（文字一致でカウント）
-> TLE

B. 全パターン
str.count('W')でやる
-> TLE

C. 「累積和」というアルゴリズムを使わないと解けない問題。学習して、使う！

'''

# from collections import deque # キュー、スタックに利用

# sys.stdin.readline() # 標準入力の高速化に利用
import sys
input = sys.stdin.readline

is_debug = True



def main():
    '''メイン処理'''
    n = int(input())
    s = input()

    s_list = list(s)

    sum_list = []
    for i in range(n):
        sum_list.append(s_list[:i].count('W') + s_list[i+1:].count('E'))
    
    print(min(sum_list))


if __name__ == "__main__":
    # ファイル実行時に、main()関数を実行
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
ans = 1e+6
for n in range(N):
    score = len([x for x in S[:n] if x == 'W']) + len([x for x in S[n + 1:] if x == 'E'])
    ans = min(ans, score)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
ans = 1e+6
for n in range(N):
    score = len(list(filter(lambda x: x == 'W', S[:n]))) + len(list(filter(lambda x: x == 'E', S[n + 1:])))
    ans = min(ans, score)
print(ans)

Submission status: Time Limit Exceeded

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

ans = len(S) - 1
for i in range(len(S)):
    _ans = S.count("W", 0, i) + S.count("E", i + 1, len(S))
    ans = ans if ans <= _ans else _ans

print(ans)

Submission status: Time Limit Exceeded

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

ans = len(S) - 1
for i in range(len(S)):
    _ans = 0
    for j in range(i):
        if _ans >= ans:
            break

        s = S[j]
        if s == "W":
            _ans += 1

    if _ans >= ans:
        break

    for k in range(i + 1, len(S)):
        if _ans >= ans:
            break

        s = S[k]
        if s == "E":
            _ans += 1

    ans = ans if ans <= _ans else _ans

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()
s = np.array([True if _ == 'W' else False for _ in S])
s_ = np.logical_not(s)
res = N
for i in range(N):
    a = sum(s[0:i]) + sum(s_[i+1:N])
    if a < res:
        res = a
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
min = N - 1
for i in range(N):
  cnt = S[:i].count('W') + S[i+1:].count('E')
  if cnt < min:
    min = cnt
print(min)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
min = N - 1
for i in range(N-1):
  cnt = S[:i].count('W') + S[i+1:].count('E')
  if cnt < min:
    min = cnt
print(min)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(sys.stdin.readline().rstrip())
ans = N
for i in range(N):
    t = S[0:i].count("W") + S[i + 1:].count("E")
    ans = min(ans, t)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
min_ = 10**12
for i in range(n):
    s1 =s[:i]
    s2 =s[i:]
    hantai = 0
    for k in s1:
        if k == 'W':
            hantai += 1
    for k in s2:
        if k == 'E':
            hantai += 1
    min_ = min(min_, hantai)
    
print(min_)

Submission status: Time Limit Exceeded

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

oder = min(S[1:].count('E'), S[:-1].count('W'))

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

print(oder)

Submission status: Time Limit Exceeded

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

ans =[]
for i in range(N):
  x=0
  for j in range(N):
    if i ==j:continue
    if j < i:
      if S[j] != "E":x +=1
    if j > i:
      if S[j] == "E":x +=1
  ans.append(x)
  
ans.sort()  
print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
    
def main():
    N = int(input())
    S = input()[:N]

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

    print(min_count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans_1 = S[1:].count("E")
ans_2 = N
for i in range(1, N-1):
    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
    if ans_2 > count:
        ans_2 = count
ans_3 = S[:N-1].count("W")

print(min(ans_1, ans_2, ans_3))

Submission status: Time Limit Exceeded

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

ans_1 = S[1:].count("E")
ans_2 = N
for i in range(N):
    count = S[:i].count("W") + S[i+1:].count("E")
    if ans_2 > count:
        ans_2 = count
ans_3 = S[:N-1].count("W")

print(min(ans_1, ans_2, ans_3))

Submission status: Time Limit Exceeded

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

ans = float("inf")
for i in range(N):
    if i == 0:
        count = S[1:].count("E")
    elif i < N:
        count = S[:i].count("W") + S[i+1:].count("E")
    else:
        count = S[:N-1].count("W")
    if ans > count:
        ans = count

print(ans)

Submission status: Time Limit Exceeded

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

p = 10**6
q = [0]*N
for i in range(N):
    Sl = S[:i] if i > 0 else "0"
    Sr = S[i+1:] if i < N-1 else "0"
    q[i] = Sl.count("W") + Sr.count("E")

q.sort()

print(q[0])

Submission status: Time Limit Exceeded

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

p = 10**6
q = [0]*N
for i in range(N):
    Sl = S[:i] if i > 0 else "0"
    Sr = S[i+1:] if i < N-1 else "0"
    q[i] = Sl.count("W") + Sr.count("E")

print(min(q))

Submission status: Time Limit Exceeded

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

p = 10**6

for i in range(N):
    Sl = S[:i] if i > 0 else "0"
    Sr = S[i+1:] if i < N-1 else "0"
    if p > Sl.count("W") + Sr.count("E"):
        p = Sl.count("W") + Sr.count("E")

print(p)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans =[0]*N
tmp = 0
ans[0] = N-1-S.count('E')
ans[-1] = N-S.count('W')
for i in range(1,N-1):
    A = S[:i]
    B = S[i+1:]
    tmp = (i-A.count('E'))+(N-(i+1)-B.count('W'))
    ans[i] = tmp
    tmp = 0
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
m = 10**6
for i in range(n):
  m = min(m,s[:i].count("W")+s[i+1:].count("E"))
print(m)

Submission status: Time Limit Exceeded

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

res = 1e+9
for i in range(N):
    l = S[:i]
    r = S[i+1:]
    cnt = 0
    cnt += l.count('W')
    cnt += r.count('E')
    res = min(res,cnt)
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans=10000000
for i in range(n+1):
    s1 = s[0:i]
    s2 = s[i:n]
    w=s1.count('W')
    e=s2.count('E')
    counter=w+e
    ans=min(ans,counter)
print(ans)

Submission status: Time Limit Exceeded

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

ww = [0]*n
ee = [0]*n
ww[-1] = s.count('W')
ee[-1] = s.count('E')
for i in range(n-1):
  ww[i] = s[:i+1].count('W')
  ee[i] = s[:i+1].count('E')
      
l = [0]*n
l[0] = ee[-1]
l[-1] = ww[-2]
for i in range(1,n-1):
  l[i] = ww[i-1] + ee[-1] - ee[i]
    
  
print(min(l))

Submission status: Time Limit Exceeded

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

ww = [0]*n
ee = [0]*n

for i in range(n):
  for j in range(0, i+1):
    if s[j] == 'W':
      ww[i] += 1
    else:
      ee[i] += 1
      
l = [0]*n
l[0] = ee[-1]
l[-1] = ww[-2]
for i in range(1,n-1):
  l[i] = ww[i-1] + ee[-1] - ee[i]
    
  
print(min(l))

Submission status: Time Limit Exceeded

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

ww = [0]*n
ee = [0]*n

for i in range(n):
  for j in range(0, i+1):
    if s[j] == 'W':
      ww[i] += 1
    else:
      ee[i] += 1
      
l = [0]*n
l[0] = max(ee[1:])
l[n-1] = max(ww[:n-1])
for i in range(1,n-1):
  l[i] = ww[i-1] + ee[-1] - ee[i]
    
  
print(min(l))

Submission status: Time Limit Exceeded

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

for i in range(n):
  if s[i] == 'W':
    s[i] = 0
  else:
    s[i] = 1
    
l = [0]*n
l[n-1] = n - 1 - sum(s[:n-1])

for i in range(n-1):
  w_side = len(s[:i]) - sum(s[:i])
  e_side = sum(s[i+1:])
  l[i] = w_side + e_side
  
print(min(l))

Submission status: Time Limit Exceeded

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

l = [0]*n
l[n-1] = s[:n-1].count('W')
for i in range(n-1):
  w_side = s[:i].count('W')
  e_side = s[i+1:].count('E')
  l[i] = w_side + e_side
  
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
l = []
for i in range(N):  # iはリーダーのインデックス番号
    counter = 0     # counterは向きを変える人数
    for j in range(i):
        if S[j] == "W":
            counter += 1
    for j in range(i+1,N):
        if S[j] == "E":
            counter += 1
    l.append(counter)
print(min(l))