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

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

L = {}
L[0] = -1 if S[0] == "W" else -1
for i in range(1,N):
    L[i] = L[i-1] - 1 if S[i] == "W" else L[i-1] + 1

R = {}
R[N-1] = -1 if S[N-1] == "W" else -1
for i in range(N-2, -1 , -1):
    R[i] = R[i+1] -1 if S[i] == "W" else R[i+1] + 1   

#print(L)
#print(R)

ans_key = 0
for i in range(N-1):
    if L[i] - R[i + 1] >= ans_key:
        ans_key = i
if L[N-1] > ans_key:
    ans_key = L[N-1]
elif R[0] > ans_key:
    ans_key = R[0]

L_count = 0
for i in range(ans_key+1):
    if S[i] == "W":
        L_count += 1
R_count = 0
for i in range(ans_key+1,N):
    if S[i] == "E":
        R_count += 1

print(L_count + R_count)

Submission status: Wrong Answer

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

L = {}
L[0] = -1 if S[0] == "W" else -1
for i in range(1,N):
    L[i] = L[i-1] - 1 if S[i] == "W" else L[i-1] + 1

R = {}
R[N-1] = -1 if S[N-1] == "W" else -1
for i in range(N-2, -1 , -1):
    R[i] = R[i+1] -1 if S[i] == "W" else R[i+1] + 1   

#print(L)
#print(R)

ans_key = 0
for i in range(N-1):
    if L[i] - R[i + 1] >= ans_key:
        ans_key = i
if L[N-1] > ans_key:
    ans_key = L[N-1]
elif R[0] > ans_key:
    ans_key = R[0]

L_count = 0
for i in range(ans_key+1):
    if S[i] == "W":
        L_count += 1
R_count = 0
for i in range(ans_key,N):
    if S[i] == "L":
        R_count += 1

print(L_count + R_count)

Submission status: Wrong Answer

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

L = {}
L[0] = -1 if S[0] == "W" else -1
for i in range(1,N):
    L[i] = L[i-1] - 1 if S[i] == "W" else L[i-1] + 1

R = {}
R[N-1] = -1 if S[N-1] == "W" else -1
for i in range(N-2, -1 , -1):
    R[i] = R[i+1] -1 if S[i] == "W" else R[i+1] + 1   

#print(L)
#print(R)

ans_key = 0
for i in range(N-1):
    if L[i] - R[i + 1] >= ans_key:
        ans_key = i
if L[N-1] > ans_key:
    ans_key = L[N-1]
elif R[0] > ans_key:
    ans_key = R[0]

ans = 0
l_num = (ans_key - (-1 * L[ans_key-1]) if L[i-1] < 0 else 0) //2 + (-1 * L[ans_key-1]) if L[i-1] < 0 else 0
r_num = ((N - ans_key -1 +  R[ans_key + 1] if  R[ans_key + 1] > 0 else 0 )) // 2  + R[ans_key + 1] if  R[ans_key + 1] > 0 else 0 

#print(ans_key)
print(l_num + r_num)

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
S=list(input())
#print(N,S)
num=1000000
"""
for i in range(N):
	tmp=0
	for j in range(N):
		if i is not j:
			if i > j:
				if S[j] is 'W':
					tmp += 1
			else:
				if S[j] is 'E':
					tmp += 1
		#print(i,j,num,tmp)
	if num > tmp:
		num=tmp
print(num)
"""
tmp=0
for i in range(N):
	if i is 0:
		for j in range(N):
			if S[j] is 'E':
				tmp += 1	
		num=tmp
	else:	
		if S[i-1] is 'W':
			tmp += 1
		if S[i] is 'E':
			tmp -= 1
		if num > tmp:
			num=tmp
print(num)

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
S=list(input())
#print(N,S)
num=1000000
"""
for i in range(N):
	tmp=0
	for j in range(N):
		if i is not j:
			if i > j:
				if S[j] is 'W':
					tmp += 1
			else:
				if S[j] is 'E':
					tmp += 1
		#print(i,j,num,tmp)
	if num > tmp:
		num=tmp
print(num)
"""
tmp=0
for i in range(N):
	if i is 0:
		for j in range(N):
			if S[j] is 'E':
				tmp += 1	
		num=tmp
	else:	
		if S[i-1] is 'W':
			tmp += 1
		else:
			tmp -= 1
		if num > tmp:
			num=tmp
print(num)

Submission status: Wrong Answer

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

numlist=[1 for i in range(N)]

if S=="E"*N or S=="W"*N:
    numlist[0]=0
else:
    num_0=0
    for i in range(1,N):
        if S[i]=="E":
            num_0+=1
    numlist[0]=num_0

for i in range(1,N):
    p=numlist[i-1]
    if S[i-1]=="W":
        p+=1
    if S[i]=="E":
        p-=1
    numlist[i]=p

print(min(numlist))

Submission status: Wrong Answer

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

N=int(input())
S=input()
S=list(S)
East_number=0
East=[]
for i in S:
  if i=='E':
    East_number+=1
  East.append(East_number)
number=10e9
ans=[]
for i in range(N):
  if i >0:
    number+=i-East[i-1]
    number+=East[N-1]-East[i]
  ans.append(number)
  number=0
print(min(ans))

Submission status: Wrong Answer

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

N=int(input())
S=input()
S=list(S)
East_number=0
East=[]
for i in S:
  if S=='E':
    East_number+=1
  East.append(East_number)
number=1e9
ans=[]
for i in range(N):
  if i >0:
    number+=i-East[i-1]
  if i<N-1:
    number+=East[N-1]-East[i]
  ans.append(number)
  number=0
print(min(ans))

Submission status: Wrong Answer

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

N=int(input())
S=input()
S=list(S)
number=0
ans=0
for i in range(N):
  Left=S[:i]
  Right=S[i+1:]
  number+=Left.count('W')
  number+=Right.count('E')
  if ans>number:
    ans=number
  number=0
print(number)

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    s = input()

    lw = [0]*n
    le = [0]*n
    rw = [0]*n
    re = [0]*n

    for i in range(n-1):
        if s[i] == 'W':
            lw[i+1] = lw[i] + 1
            le[i+1] = le[i]
        else:
            lw[i+1] = lw[i]
            le[i+1] = le[i] + 1

    for i in range(n-1, 0, -1):
        if s[i] == 'W':
            rw[i-1] = rw[i] + 1
            re[i-1] = re[i]
        else:
            rw[i-1] = rw[i]
            re[i-1] = re[i] + 1

    ans = INF

    for i in range(n): 
        if s[i] == 'W':
            ans = min(ans, lw[i] + re[i])
        else:
            ans = min(ans, le[i] + rw[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    s = input()

    lw = [0]*n
    le = [0]*n
    rw = [0]*n
    re = [0]*n

    for i in range(n-1):
        if s[i] == 'W':
            lw[i+1] = lw[i] + 1
            le[i+1] = le[i]
        else:
            lw[i+1] = lw[i]
            le[i+1] = le[i] + 1

    for i in range(n-1, 0, -1):
        if s[i] == 'W':
            rw[i-1] = rw[i] + 1
            re[i-1] = re[i]
        else:
            rw[i-1] = rw[i]
            re[i-1] = re[i] + 1

    ans = INF
    for j in range(n):
        if s[i] == 'W':
            ans = min(ans, lw[i]+re[i])
        else:
            ans = min(ans, le[i] + rw[i])

    print(ans - 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
S=list(input())

all=len(S)
all_E=S.count("E")
all_W=S.count("W")
E=0
W=0

eva=10**12
ans=0
for index,item in enumerate(S):
    temp=W+all_E-E
    eva=min(eva,temp)
    if eva==temp:
        ans=index
    if item=="E":
        E+=1
    else:
        W+=1
    

print(eva)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
arr = [0]*N
for i in S:
    if i == 'E':
        arr[0] += 1
for i in range(1, N):
    arr[i] = arr[i-1]
    if S[i-1] == 'W':
        arr[i] += 1
    if S[i] =='E':
        arr[i] -= 1
print(min(arr))

Submission status: Wrong Answer

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

e=[0]*n
w=[0]*n

for i in range(n-1):
    if l[i]=='E':
        e[i+1]=e[i]+1
    if l[n-1-i]=='W':
        w[n-2-i]=w[n-1-i]+1
ew=[0]*n
for i in range(n):
    ew[i]=e[i]+w[i]
print(n-1-max(ew))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
way = [0]*(n+1)
for i in range(n):
    if s[i] == "E":
        way[i+1] = way[i]+1
    else:
        way[i+1] = way[i]
key = way[-1]
ans = 10000000
for j in range(n):
    sub = j - way[j]*2 + key
    if sub < ans:
        ans = sub
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
answer = n
left_count = 0
right_count = s.count('E')
for i in range(n):
  if i != 0 and s[i-1] == 'W':
    left_count += 1
    if s[i] == 'E':
      right_count -= 1
      total = left_count + right_count
      answer = min(answer, total)
print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
w = [0] + []
e = [] + [0]
w_cnt = 0
e_cnt = 0
for i in range(n):
    if s[i] == "W":
        w_cnt += 1
    if s[n - i - 1] == "E":
        e_cnt += 1
    w.append(w_cnt)
    e.append(e_cnt)
e = e[::-1]
ans = 10 ** 9
for i in range(n-1):
    ans = min(ans, w[i]+e[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
li = list(input())
w,e = [],[]
for i in range(n):
    if li[i] == "W":
        w.append(1)
    elif li[i] == "E":
        w.append(0)
for i in range(n-1):
    w[i+1] += w[i]
    
for i in range(n):
    if li[i] == "E":
        e.append(1)
    elif li[i] == "W":
        e.append(0)
for i in range(n-1):
    e[i+1] += e[i]    
#print(w,e,li)
w1 = []
for i in range(1,n):
    w1.append(e[n-1]-e[i] + w[i-1]-w[0])
print(min(w1))

Submission status: Wrong Answer

Solution code:
N = int(input())  # N人の人
S = str(input())  # 人の状態

S = 'E' + S + 'WE'

l, r = -1, -1  # E W がもっとも連続する場所のインデックス
cnt = 0
window = 0
for i in range(1, len(S)):
    if S[i-1] == 'W' and S[i] == 'E':
        if cnt > window:
            window = cnt
            r = i
            l = r - window
        cnt = 1
    else:
        cnt += 1
ans = 0
for i in range(0, l):
    if S[i] == 'W':
        ans += 1
for i in range(r, len(S)-1):
    if S[i] == 'E':
        ans += 1

print(ans)

Submission status: Wrong Answer

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

scr = S.count('E')
ans = scr

for i in range(1, N):
    if S[i-1]=='W':
        scr += 1
    if S[i]=='E':
        scr -= 1
    ans = min(ans, scr)

print(ans)

Submission status: Wrong Answer

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

east = [0]*N
west = [0]*N
for i in range(N):
    if S[i] == 'E':
        east[i] = 1
    else:
        west[i] = 1

sum_east = [0]*(N+1)
sum_west = [0]*(N+1)
for i in range(N):
    sum_west[i+1] = sum_west[i] + west[i]
    sum_east[N-i-1] = sum_east[N-i] + east[N-i-1]
print(sum_west, sum_east)

ans = 10**6
for i in range(N):
    ans = min(ans, sum_west[i] + sum_east[i+1])
    #print(ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n = int(input())
    s = input()
    cnt = s.count('E')
    m = cnt
    for i in s:
        if i == 'E':
            cnt -= 1
        else:
            cnt += 1
        print(cnt)
        m = min(m, cnt)
    print(m)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
S = input()
e_sum = [0] * n
w_sum = [0] * n
ew_sum = [0] * n
for i in range(1, n + 1):
    e_sum[-i] = e_sum[-i + 1] + 1 if S[-i + 1] == 'E' else e_sum[-i + 1]
    ew_sum[-i] = e_sum[-i]
for i in range(1, n):
    w_sum[i] = w_sum[i - 1] + 1 if S[i - 1] == 'W' else w_sum[i - 1]
    ew_sum[i] += w_sum[i]

print(min(ew_sum))

Submission status: Wrong Answer

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

ss1 = S[1:].count('E')
ss = ss1

for i in range(1, N):
  ss1 = ss1 + (S[i-1]=='W') - (S[i]=='E')
  if ss1 < ss:
    ss = ss1
  
print(max(ss, 1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

res = s[1:].count('E')
temp = res

for i in range(1, n):
    if s[i-1] == 'W':
        temp += 1
    elif s[i-1] == 'E':
        temp -= 1
    if temp < res:
        res = temp
print(res)

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


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


def solve(inputs):
    persons = inputs[0]
    see_east = [0]
    see_west = [0]

    for i in range(0, len(persons)):
        see_east.append(see_east[-1] + (1 if persons[i] == 'E' else 0))
        see_west.append(see_west[-1] + (1 if persons[i] == 'W' else 0))

    min_count = -1
    for i in range(1, len(persons)):
        count = see_east[-1] - see_east[i]
        count += see_west[i-1]
        if min_count == -1 or min_count > count:
            min_count = count

    return min_count


def string_to_int(string):
    return list(map(int, string.split()))


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

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


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


def solve(inputs):
    persons = inputs[0]
    see_east = [0]
    see_west = [0]

    for i in range(0, len(persons)):
        see_east.append(see_east[-1] + (1 if persons[i] == 'E' else 0))
        see_west.append(see_west[-1] + (1 if persons[i] == 'W' else 0))

    min_count = -1
    for i in range(1, len(persons)-1):
        count = see_east[-1] - see_east[i]
        count += see_west[i-1]
        if min_count == -1 or min_count > count:
            min_count = count

    return min_count


def string_to_int(string):
    return list(map(int, string.split()))


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

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


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


def solve(inputs):
    persons = inputs[0]
    see_east = [0]
    see_west = [0]

    for i in range(0, len(persons)):
        see_east.append(see_east[-1] + (1 if persons[i] == 'E' else 0))
        see_west.append(see_west[-1] + (1 if persons[i] == 'W' else 0))
    print(see_east)
    print(see_west)

    min_count = -1
    for i in range(1, len(persons)-1):
        count = see_east[-1] - see_east[i]
        count += see_west[i-1]
        if min_count == -1 or min_count > count:
            min_count = count

    return min_count


def string_to_int(string):
    return list(map(int, string.split()))


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

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


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


def solve(inputs):
    persons = inputs[0]
    see_east = deque([0])
    see_west = deque([0])

    for i in range(0, len(persons)):
        see_east.append(see_east[-1] + (1 if persons[i] == 'E' else 0))
        see_west.append(see_west[-1] + (1 if persons[i] == 'W' else 0))

    min_count = -1
    for i in range(1, len(persons)):
        count = see_east[-1] - see_east[i+1]
        count += see_west[i-1]
        count -= 1
        if min_count == -1 or min_count > count:
            min_count = count

    return min_count


def string_to_int(string):
    return list(map(int, string.split()))


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

Submission status: Wrong Answer

Solution code:
N=int(input())
text=input()
ls=[0 for i in range(N)]
if text[0]=="W":
  ls[0]=0
else:
  ls[0]=1
for i in range(1,N):
  if text[i]=="W":
    ls[i]=ls[i-1]
  else:
    ls[i]=ls[i-1]+1
print(ls)
ans=min([ls[N-1]-ls[0],N-1-ls[N-2]])
for i in range(1,N-1):
  x=0
  x+=i-ls[i-1]
  x+=ls[N-1]-ls[i]
  if x<ans:
    ans=x
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


"""考え方
「右側のE」+「左側のW」の合計が一番小さくなるインデックスを探索する。
"""
left_w = 0
right_e = S.count('E')
ans = 10**6
## 西の端から東に向かって確認していく
for n in range(0,N):
  cnt = left_w + right_e
  ans = min(ans,cnt)
  
  if S[n] == "W":
    left_w += 1
  else:
    right_e -= 1
  

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
s=input()
east=[0]*n
west=[0]*n
east[0]=(1 if s[0]=='E' else 0)
west[0]=(1 if s[n-1]=='W' else 0)
for i in range(1,n):
  east[i]=east[i-1]+(1 if s[i]=='E' else 0)
  west[i]=west[i-1]+(1 if s[n-i-1]=='W' else 0)
cnt=max(east[n-2],west[n-2])
totl=0
if n==2:
  if (east[0]==1 and west[0]==1) or (east[1]==1 and west[1]==1):
    print(0)
    sys.exit()
  elif east[0]==1 or east[1]==1:
    print(1)
    sys.exit()
  elif west[0]==1 or west[1]==1:
    print(1)
    sys.exit()
  else:
    print(1)
    sys.exit()
for j in range(n-2):
  if east[j]+west[n-j-3]>cnt:
    cnt=east[j]+west[n-j-3]
    totl=n-1-cnt
print(totl)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
s=input()
east=[0]*n
west=[0]*n
east[0]=(1 if s[0]=='E' else 0)
west[0]=(1 if s[n-1]=='W' else 0)
for i in range(1,n):
  east[i]=east[i-1]+(1 if s[i]=='E' else 0)
  west[i]=west[i-1]+(1 if s[n-i-1]=='W' else 0)
cnt=max(east[n-2],west[n-2])
totl=0
if n==2:
  if (east[0]==1 and west[0]==1) or (east[1]==1 and west[1]==1):
    print(0)
    sys.exit()
  elif east[0]==1 or east[1]==1:
    print(1)
    sys.exit()
  elif west[0]==1 or west[1]==1:
    print(1)
    sys.exit()
  else:
    print(2)
for j in range(n-2):
  if east[j]+west[n-j-3]>cnt:
    cnt=east[j]+west[n-j-3]
    totl=n-1-cnt
print(totl)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
east=[0]*n
west=[0]*n
east[0]=(1 if s[0]=='E' else 0)
west[0]=(1 if s[n-1]=='W' else 0)
for i in range(1,n):
  east[i]=east[i-1]+(1 if s[i]=='E' else 0)
  west[i]=west[i-1]+(1 if s[n-i-1]=='W' else 0)
cnt=max(east[n-2],west[n-2])
totl=0
for j in range(n-2):
  if east[j]+west[n-j-3]>cnt:
    cnt=east[j]+west[n-j-3]
    totl=n-1-cnt
print(totl)

Submission status: Wrong Answer

Solution code:
n, s = int(input()), input()
w = [1 if s[0] == "W" else 0] + [0]*(n-1)
e = [0]*(n-1) + [1 if s[-1] == "E" else 0]
for i in range(1, n):
    w[i] = w[i-1]+1 if s[i] == "W" else w[i-1]
    e[n-i-1] = e[n-i]+1 if s[n-i-1] == "E" else e[n-i]
w = [0] + w + [0]
e = [0] + e + [0]
print(min(w[i-1]+e[i+1] for i in range(n)))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N = int(input())
S = input()
ans = S.count('E')
tmp = ans
for i in range(N-1):
    if S[i] == 'W':
        tmp += 1
    if S[i + 1] == 'E':
        tmp -= 1
    ans = min(tmp, ans)
print(str(ans))

Submission status: Wrong Answer

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

west = [0] * (n + 2)
east = [0] * (n + 2)

for i,v in enumerate(s):
    west[i + 1] = west[i]
    if v == 'W':
        west[i + 1] += 1

for i,v in enumerate(reversed(s)):
    east[n - i] = east[n + 1 - i]
    if v == 'E':
        east[n - i] += 1

min_count = None
for i in range(n):
    if not min_count:
        min_count = west[i] + east[i + 2]
    else:
        min_count = min(min_count, west[i] + east[i + 2])

print(min_count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
num = s[1:].count('E')
ans = [num]

for i in range(1,n):
    if s[i] == 'E':
        num -= 1
    elif s[i-1] == 'W':
        num += 1
    ans.append(num)
print(min(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
num = s[1:].count('E')
ans = [num]

for i in range(1,n):
    if s[i] == 'E':
        num -= 1
    elif s[i] == 'W':
        num += 1
    ans.append(num)
print(min(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
left_attention = [0 for i in range(n)]
right_attention = [0 for i in range(n)]

for i in range(1,n):
    left_attention[i]=left_attention[i-1]
    if(s[i-1]=='W'):
        left_attention[i]+=1

print(left_attention)

for i in range(n-2,-1,-1):
    right_attention[i]=right_attention[i+1]
    if(s[i+1]=='E'):
        right_attention[i]+=1

print(right_attention)

ans=n
for i in range(n):
    ans = min(ans, left_attention[i]+right_attention[i])


print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

lst = []
lst.append({"W": 0, "E": 0})
for i in range(1,len(S)+1):
  res = {"W": 0, "E": 0}
  if S[i-1] == "W":
    res["W"] += lst[i-1]["W"] + 1
    res["E"] += lst[i-1]["E"]
  else:
    res["W"] += lst[i-1]["W"]
    res["E"] += lst[i-1]["E"] + 1
  lst.append(res)

ans = N
for i in range(1,len(lst)):
  n = 0
  if (i != len(lst)-1):
    n = lst[i-1]["W"] + (lst[-1]["E"] - lst[i+1]["E"])
  else:
    n = lst[i-1]["W"]
  if (n < ans):
    ans = n
print(ans)

Submission status: Wrong Answer

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

sumW = [0 for _ in range(N + 1)]

for i, s in enumerate(S) :
    sumW[i + 1] = sumW[i]
    if s == 'W' :
        sumW[i + 1] += 1

ans = float('inf')
for i, s in enumerate(S) :
    count = sumW[i - 1] + (N - i - 1 - (sumW[-1] - sumW[i + 1]))
    ans = min(ans, count)

if N == 1 :
    print('0')
else :
    print(ans)

Submission status: Wrong Answer

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

sumW = [0 for _ in range(N + 1)]

for i, s in enumerate(S) :
    sumW[i + 1] = sumW[i]
    if s == 'W' :
        sumW[i + 1] += 1

ans = float('inf')
for i, s in enumerate(S) :
    count = sumW[i - 1] + (N - i - 1 - (sumW[-1] - sumW[i + 1]))
    ans = min(ans, count)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
ws, es = [0] * (N + 1) , [0] * (N + 1)
count_ws, count_es = 0, 0
for i in range(N):
    if S[i] == "W":
        count_ws += 1
        ws[i+1] = count_ws
        es[i+1] = count_es
    elif S[i] == "E":
        count_es += 1
        es[i+1] = count_es
        ws[i+1] = count_ws

ans = float('inf')
min_index = 0
for i in range(N):
    res = ws[i] + es[-1] - es[i]
    if res < ans:
        ans = res
        min_index = i
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

count = sys.maxsize
ans = S[1:].count("E")

for i in range(N-1):
    count = min(ans, count)
    if S[i] == "W":
        if S[i+1] == "W":
            ans += 1
    else:
        if S[i+1] == "E":
            ans -= 1

print(count)

Submission status: Wrong Answer

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

# print(N, S)
ans = N

f = S[0]
s = S[1:]

nw = 0
ne = 0

for w in s:
    if w != 'W':
        ne += 1

for i, h in enumerate(range(1, N+1)):
    # skipflg = False
    f = S[0: i]
    l = S[i]
    s = S[i+1:]
    # print(f, l, s)
    # nw = f.count('W')
    # ne = s.count('E')
    # print(nw, ne)

    if S[i-1] != 'E':
        nw += 1

    if S[i] != 'W':
        ne -= 1
    # print(nw, ne)
    ans = min(ans, nw+ne)

print(ans)

Submission status: Wrong Answer

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

# print(N, S)
ans = N

f = S[0]
s = S[1:]

nw = 0
ne = 0

for w in s:
    if w != 'W':
        ne += 1

for i, h in enumerate(range(1, N)):
    # skipflg = False
    # f = S[0: i]
    # l = S[i]
    # s = S[i+1:]
    # print(f, l, s)
    # nw = f.count('W')
    # ne = s.count('E')
    # print(nw, ne)

    if S[i-1] != 'E':
        nw += 1

    if S[i] != 'W':
        ne -= 1
    # print(nw+ne)
    ans = min(ans, nw+ne)

print(ans)

Submission status: Wrong Answer

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

# print(N, S)
ans = N

f = S[0]
s = S[1:]

nw = 0
ne = 0

for w in s:
    if w != 'W':
        ne += 1

for i, h in enumerate(range(N)):
    # skipflg = False
    # f = S[0: i]
    # l = S[i]
    # s = S[i+1:]
    # print(f, l, s)
    # nw = f.count('W')
    # ne = s.count('E')
    # print(nw, ne)

    if i-1 >= 0:
        if S[i-1] != 'E':
            nw += 1

    if S[i] != 'W':
        ne -= 1
    # print(nw+ne)
    ans = min(ans, nw+ne)

print(ans)

Submission status: Wrong Answer

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

# print(N, S)
ans = N

for i, h in enumerate(range(N)):
    skipflg = False
    f = S[0: i]
    l = S[i]
    s = S[i+1:]
    # print(f, l, s)
    nw = 0
    ne = 0
    # nw = f.count('W')
    # ne = s.count('E')
    print(nw, ne)
    for r in f:
        if r != 'E':
            nw += 1
            if nw >= ans:
                skipflg = True
                break
            # print('not e')

    if skipflg:
        break

    for w in s:
        if w != 'W':
            ne += 1
            if nw+ne >= ans:
                skipflg = True
                break
            # print('not w')

    if skipflg:
        break

    ans = min(ans, nw+ne)

print(ans)

Submission status: Wrong Answer

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

# print(N, S)
ans = N

for i, h in enumerate(range(N)):
    skipflg = False
    f = S[0: i]
    l = S[i]
    s = S[i+1:]
    # print(f, l,  s)
    nw = 0
    ne = 0
    for r in f:
        if r != 'E':
            nw += 1
            if nw >= ans:
                skipflg = True
                break
            # print('not e')

    if skipflg:
        break

    for w in s:
        if w != 'W':
            ne += 1
            if nw+ne >= ans:
                skipflg = True
                break
            # print('not w')

    if skipflg:
        break

    ans = min(ans, nw+ne)

print(ans)

Submission status: Wrong Answer

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

# print(N, S)
ans = N

for i, h in enumerate(range(N)):
    skipflg = False
    f = S[0: i]
    l = S[i]
    s = S[i+1:]
    # print(f, l,  s)
    nw = 0
    ne = 0
    for r in f:
        if r != 'E':
            nw += 1
            if nw > ans:
                skipflg = True
                break
            # print('not e')

    if skipflg:
        break

    for w in s:
        if w != 'W':
            ne += 1
            if nw+ne > ans:
                skipflg = True
                break
            # print('not w')

    if skipflg:
        break

    ans = min(ans, nw+ne)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
p=[0]*n
p[0]=s[1:].count('E')
k = {'W':1,'E':-1}
print(min([p[0]]+[p[i-1]+k[s[i]]for i in range(1, n)]))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
p=[0]*n
p[0]=s[1:].count('E')
k = {'W':1,'E':-1}
print(max([p[0]]+[p[i-1] + k[s[i]]for i in range(1, n)]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
leader = [0] * (N)
sum_e = 0
sum_w = 0
for i in range(N):
  leader[i] += sum_e
  leader[N-i-1] += sum_w
  if S[i] == 'E':
    sum_e += 1
  if S[N-i-1] == 'W':
    sum_w += 1

print(N-max(leader))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.add.accumulate(e)
ans = []
for i in range(N):
    num = 0
    ans.append(num)
print(min(ans))

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
ans = []
for i in range(N):
    num = 0
    ans.append(num)
print(min(ans))

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
s = input()
ans = []
for i in range(N):
    num = 0
    ans.append(num)
print(min(ans))