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: Accepted

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

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

Submission status: Accepted

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

E = [0] * (n + 10)
W = [0] * (n + 10)

e = 0
w = 0
for i in range(n):
    if s[i] == "E":
        e += 1
    else:
        w += 1
    W[i+1] = w
    E[i+1] = e

ans = float("inf")
for i in range(1, n+1):
    x = 0
    x += W[i-1]
    x += E[n]-E[i]
    ans = min(ans, x)

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
S = input()
e, w = 0, 0
e = S.count("E")
ans = e
for i in range(N):
    if S[i] == "E":
        e -= 1
        ans = min(ans, e+w)
    else:
        w += 1
print(ans)

Submission status: Accepted

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

W=[0 for i in range(N)]
E=[0 for i in range(N)]


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

cost=min(W[N-2],E[1])

for i in range(1,N-1):
    cost = min(cost,W[i-1] + E[i+1])

print(cost)

Submission status: Accepted

Solution code:
import sys

import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
from operator import attrgetter, itemgetter

# import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

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

    for i in range(n - 1):
        w[i + 1] = w[i]
        if s[i] == "W":
            w[i + 1] += 1

    for i in range(n - 1, 0, -1):
        e[i - 1] = e[i]
        if s[i] == "E":
            e[i - 1] += 1

    ans = INF
    for j in range(n):
        temp = w[j] + e[j]
        ans = min(ans, temp)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

look_east=[0]*N
for i in range(1, N):
    if S[i-1]=='E':
        look_east[i]=look_east[i-1]+1
    else:
        look_east[i] = look_east[i - 1]
#print(look_east)

look_west=[0]*N
for i in range(N-2, -1,-1):
    #print(i)
    if S[i+1]=='W':
        look_west[i]=look_west[i+1]+1
    else:
        look_west[i] = look_west[i + 1]
#print(look_west)

ew=[look_west[i]+look_east[i] for i in range(N)]
#print(ew)
print(N-max(ew)-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
left_w = 0
right_e = s.count('E')
ans = left_w + right_e
for c in s:
    if c == 'E':
        right_e -= 1
    ans = min(ans, left_w + right_e)
    if c == 'W':
        left_w += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import collections
from operator import mul
from functools import reduce



n=int(input())
s=input()
#v=list(map(int,input().split()))
e=[0]*(n+2)
w=[0]*(n+2)
for i in range(1,n+1):
    if s[i-1]=="W":
        e[i]=1
    e[i]+=e[i-1]
for i in range(1,n+1)[::-1]:
    if s[i-1]=="E":
        w[i]=1
    w[i]+=w[i+1]

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

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
a = [0]*(N+1)
r = [0]*(N+1)
rr = [0]*(N+1)
l = [0]*(N+1)
ll = [0]*(N+1)
a = [0]*(N+1)

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

for i in range(N+1):
    l[i] = i - r[i]
for i in range(N+1):
    ll[i] = i - rr[i] 

for i in range(N+1):
    a[i] = ll[N-i] + r[i]
    
"""
print(r)
print(rr)
print(l)
print(ll)
print(a)
"""

print(N-max(a))

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(str, input()))
a = []
cnt = s.count("E")
s.insert(0, 0)
s.append(0)
for i in range(1, n + 1):
    if s[i - 1] == "W":
        cnt += 1
    if s[i] == "E" and cnt != 0:
        cnt -= 1
    a.append(cnt)
print(min(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
lst = [0]*n

lst[0] = s[1:].count("E")
for i in range(1,n):
    lst[i] = lst[i-1]
    if s[i-1]=="W":
        lst[i]+=1
    if s[i]=="E":
        lst[i]-=1

print(min(lst))

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
sumL = [0]*N
sumL[0] = S[1:].count('E')
left = 0
right = 0
for i in range(1,N):
  if S[i-1] == 'W':
    left = 1
  else:
    left = 0
  if S[i] == 'E':
    right = 1
  else:
    right = 0
  sumL[i] = sumL[i-1] + left - right
sumL.sort()
print(sumL[0])

Submission status: Accepted

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

#毎ステップ毎にカウントする処理→TLE
# ans=10**6
# for i in range(1,N):
#   ans=min(ans,s[:i-1].count("W")+s[i+1:].count("E"))
# print(ans)
ans=[]
ans.append(s[1:].count("E"))
tmp=ans[0]
for i in range(1,n):
  if s[i]=="E":
    tmp-=1
  if s[i-1]=="W":
    tmp+=1
  ans.append(tmp)
print(min(ans))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    S = input()

    array = [0,S[1:].count('E')]
    ans = sum(array)

    for i in range(1,len(S)):
        if S[i-1]=='W':
            array[0]+=1
        if S[i]=='E':
            array[1]-=1
        ans = min(ans, sum(array))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def Attention(n , s):

    ans = 10**9
    west = [0] * (n + 1)
    east = [0] * (n + 1)

    for i in range(n):
        if s[i] == 'W':
            west[i + 1] = west[i] + 1
            east[i + 1] = east[i]
        else:
            west[i + 1] = west[i]
            east[i + 1] = east[i] + 1

    for i in range(n):
        count = west[i] + (east[-1] - east[i + 1])
        ans = min(ans , count)

    return ans

def main():
    n = int(input())
    s = list(str(input()))
    print(Attention(n , s))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

lie = [0] * (n+1)
liw = [0] * (n+1)
ans = 10**10

for i in range(n):
    if s[i] == 'E':
        lie[i+1] = lie[i] + 1
        liw[i+1] = liw[i]
    else:
        lie[i+1] = lie[i]
        liw[i+1] = liw[i] + 1

for i in range(n):
    cnte = lie[-1] - lie[i+1]
    cntw = liw[i]
    ans = min(ans, cnte + cntw)

print(ans)

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    N=int(input())
    S=list(input())
    from itertools import accumulate

    W=[0]*N
    E=[0]*N
    for i in range(N):
        if S[i]=='W':
            W[i]=1
        else:
            E[i]=1
    WW=[0]+W
    EE=[0]+E

    from itertools import accumulate

    WW = list(accumulate(WW))
    EE=list(accumulate(EE))# 累積和を格納したリスト作成


    # この問題は長さが1-Nの連続部分の和の最大値を出せというものなので以下の通り
    ans=10**10
    for i in range(1, N + 1):
        ans = min(WW[i-1]+EE[N]-EE[i],ans)
    print(ans)



resolve()

Submission status: Accepted

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

Submission status: Accepted

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

e=[0 for i in range(n)]
w=[0 for i in range(n)]

if s[0]=='E':
    e[0]+=1
else:
    w[0]+=1

#累積和
for i in range(1,n):
    if s[i]=='E':
        e[i]=e[i-1]+1
        w[i]=w[i-1]
    else:
        w[i]=w[i-1]+1
        e[i]=e[i-1]

mn = 10**10
for i in range(n):
    dire = s[i]
    if dire == "E":
        if i != 0 and i != n-1:
            ans = w[i-1] + (e[n-1] - e[i])
        elif i == 0:
            ans = e[n-1] - 1
        else:
            ans = w[n-1]
    else:
        if i != 0 and i != n-1:
            ans = w[i-1] + (e[n-1] - e[i])
        elif i == 0:
            ans = e[n-1]
        else:
            ans = w[i-1]
    if ans < mn:
        mn = ans

print(mn)

Submission status: Accepted

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

# 各地点の人について，背を向けている人を数える。無駄が多い？
back = [0] * n
wester, easter = 0, 0
for i, p in enumerate(line):
    back[i] = wester

    if p == 'W':
        wester += 1
i = n
for p in line[::-1]:
    i -= 1
    back[i] += easter

    if p == 'E':
        easter += 1

print(min(back))

Submission status: Accepted

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

right_cost = s.count("E")
left_cost = 0

ans = 10 ** 9
for i in range(n):
    if s[i] == "E":
        right_cost -= 1

    ans = min(ans, left_cost + right_cost)

    if s[i] == "W":
        left_cost += 1


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
S=list(input())
c=collections.Counter(S)
Eleft=0
Wleft=0
Eright=c["E"]
Wright=c["W"]
cnt=0
mi=float("inf")
l=[c['E'],c['W']]
for i in range(N):
    if S[i]=="E":
        Eright=Eright-1
    elif S[i]=="W":
        Wright=Wright-1
    #print(Eleft,Wleft,Eright,Wright)
    cnt=Wleft+Eright
    if cnt<mi:
        mi=cnt
    if S[i]=="E":
        Eleft=Eleft+1
    elif S[i]=="W":
        Wleft=Wleft+1
print(mi)

Submission status: Accepted

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

e_acc, w_acc = 0, 0
e_acc_list = [0]
w_acc_list = [0]
acc = {'E': 0, 'W': 0}
acc_list = {'E': [0], 'W': [0]}
for s in S:
    if s == 'E':
        acc['E'] += 1
    else:
        acc['W'] += 1
    acc_list['E'].append(acc['E'])
    acc_list['W'].append(acc['W'])

def count(l_ind, r_ind, symbol):
    # assert l_ind < r_ind
    if r_ind < 0 or l_ind > N - 1:
        return 0
    else:
        if symbol == 'E':
            return acc_list['E'][r_ind] - acc_list['E'][l_ind]
        else:
            return acc_list['W'][r_ind] - acc_list['W'][l_ind]

cost = 1e10

for i in range(N):  # ind of leader
    tmp = count(0, i, 'W') + count(i + 1, N, 'E')
    if tmp < cost:
        cost = tmp
print(cost)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
int_s = [0] * N
cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)
answers = [0] * N
for i in range(N):
    if (S[i] == 'W'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_w[i + 1] = cum_sum_w[i] + int_s[i]
for i in range(N):
    if (S[i] == 'E'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_e[i + 1] = cum_sum_e[i] + int_s[i]
for i in range(N):
    answers[i] = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
print(min(answers))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
s = input()
s_W = [0]
s_E = []

for i in range(n):
    if s[i] == "W":
        s_W.append(1)
        s_E.append(0)
    else:
        s_W.append(0)
        s_E.append(1)

W_l = np.cumsum(np.array(s_W))
E_l = np.cumsum(np.array(s_E))

ans = []
for j in range(n):
    num = W_l[j]-W_l[0]+E_l[-1]-E_l[j]
    ans.append(num)
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

i = 0
ee = []
ww = []

while i < n:
    j = i
    count = 1
    while j < n-1 and s[j] == s[j+1]:
        count += 1
        j += 1
    if s[j] == 'E':
        ee.append(count)
        ww.append(0)
    else:
        ww.append(count)
        ee.append(0)
    i = j+1
acume = [0 for _ in range(len(ee)+1)]
acumw = [0 for _ in range(len(ww)+1)]
for i, w in enumerate(ww):
    acumw[i+1] = acumw[i] + w
for i, e in enumerate(ee[::-1]):
    acume[i+1] = acume[i] + e
acume = acume[::-1]

minp = 10**6
for i in range(len(acume)):
    minp = min(minp, max(acumw[i]+acume[i], 0))
print(minp)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
c = ans = s.count("E")
for i in s:
    if i=='E':
        c-=1
    else:
        c+=1
    ans = min(c,ans)
print(ans)

Submission status: Accepted

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


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

    turn = []
    east = 0
    total = S.count("E")

    for i in range(N):
        left = i - east
        s = S[i]
        if s == "E":
            east += 1
        right = total - east
        turn.append(left + right)

    print(min(turn))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

W_accum = [0]
E_accum = [0]
for i in range(N):
    if S[i] == "W":
        W_accum += [W_accum[-1] +1]
    else:
        W_accum += [W_accum[-1]]
    E_accum += [i+1 - W_accum[-1]]

ans = N
for i in range(N):
    curr = W_accum[i] + (E_accum[-1] - E_accum[i+1])
    ans = min(curr, ans)
print(ans)

Submission status: Accepted

Solution code:
## coding: UTF-8
N = int(input())
S = input()
#s = []
#print(N)
#print(S)

#自分の手前までで左を向いている人の累積和リストをとりあえず作ってみる
sum_up_W = []
counter = 0
for i in range(N):
    sum_up_W.append(counter)
    if(S[i] == 'W'):
        counter += 1    
sum_up_W.append(counter) #最後にcounterを足さないと右端の人の影響が出ない。
#print(sum_up_W)
#右Eに振り向かせたいのはsum_up_W[i]
#リーダーより右で左を向いている人数はsum_up_W[N](全体で左を向いている人数)-sum_up_W[i+1](リーダーも含め、リーダーまでで左を向いている人数)
#左Wに振り向かせたいのは、リーダーより右で右を向いている人数： N-(i+1)（リーダーより右の全人数） - 上行の人数（リーダーより右で左を向いている人数）

ans = []
for i in range(N):
    ans.append(sum_up_W[i] + N - (i+1) - (sum_up_W[N] - sum_up_W[i+1])    )
print(min(ans))
'''
#これでもTLEします
#まず右（E）を向いている人を1、左（W）を向いている人を0に変換する
for i in range(len(S)):
    if(S[i] == 'E'):
        s.append(1)
    else:
        s.append(0)
#print(s)

#その地点までで左(W)を向いている人数を累積和するリストを作ります。要は右に振り向かせる人数リスト
E_count = []
#その地点から先で右(E)を向いている人数を累積和するリストを作ります。要は左に振り向かせる人数リスト
#W_count = []
#ans = []
for i in range(len(s)):
    E_count.append(  len(s[:i]) - sum(s[:i])  +   sum(s[i+1:]))
    #W_count.append(  sum(s[i+1:])  )
    #ans.append( E_count[i] + W_count[i])
#print(min(ans))
#print(E_count)
#print(W_count)
#for i in range(len(s)):
    #ans.append(E_count[i] + W_count[i])
print(min(E_count))
'''
'''
#これでもTLEします
def count_tern_new(s, leader):
    counter = 0
    #leaderより小さい人は右に向かせる。 左を向いている人数　＝　人数-右を向いている人数
    counter += len(s[:leader]) - sum(s[:leader])
    #leaderより大きい人には左を向かせる。 右を向いている人数
    counter += sum(s[leader+1:])
    return counter

answer_list_new = []
for i in range(len(s)):
    answer_list_new.append(count_tern_new(s, i))
print(min(answer_list_new))
'''


'''
#これだとTLEします。
def count_tern(s, leader): #文字列とリーダーのインデックスを引数にとる
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            #print("tern to E, i = {}, counter = {}".format(i,counter))
    #print("leader:{}".format(leader))
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            #print("tern to W, j = {}, counter = {}".format(j, counter))
    return counter

#answer = 1000
answer_list = []
for i in range(len(S)):
    answer_list.append(count_tern(S, i))

#for i in range(len(S)):
    #print(count_tern(S, i))
    #print()
    #if(count_tern(S, i) < answer):
        #answer = count_tern(S, i)

print(min(answer_list))
'''

Submission status: Accepted

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

E = [0] * (N + 1)
W = [0] * (N + 1)
for i, s in enumerate(S):
    E[i + 1] = E[i] + (s == 'E')
    W[i + 1] = W[i] + (s == 'W')

ans = N
for i in range(1, N + 1):
    L = W[i - 1]
    R = E[N] - E[i]
    ans = min(ans, L + R)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
east = [0] * n
west = [0] * n
before_east, before_west = 0, 0

for i in range(n):
    if s[i] == 'E':
        east[i] = before_east+1
        west[i] = before_west
        before_east = east[i]
        before_west = west[i]
    else:
        east[i] = before_east
        west[i] = before_west+1
        before_east = east[i]
        before_west = west[i]

total_east = east[-1]
total_west = west[-1]

mini = float('inf')
for i in range(n):
    if i==0:
        left_east, left_west = 0, 0
        if s[0]=='E':
            right_east = total_east-1
            right_west = total_west
        else:
            right_east = total_east
            right_west = total_west-1
    else:
        left_east = east[i-1]
        left_west = west[i-1]
        right_east = total_east-east[i]
        right_west = total_west-west[i]

    tmp = left_west+right_east

    if tmp<mini:
        mini = tmp

print(mini)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cntW=[0]*n
cntE=[0]*n
for i in range(1,n):
  if s[i-1]=='E':
    cntE[i]=cntE[i-1]+1
  else:
    cntE[i]=cntE[i-1]
for i in range(n-2,-1,-1):
  if s[i+1]=='W':
    cntW[i]=cntW[i+1]+1
  else:
    cntW[i]=cntW[i+1]
ans=10**9
ans=min(ans,n-1-cntW[0],n-1-cntE[n-1])
for i in range(1,n-1):
  ans=min(ans,n-1-cntE[i]-cntW[i])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
num=s.count("E")
length=len(s)

ans=num
for i in range(length):
    if s[i]=="W":num +=1
    elif s[i]=="E":num -=1
    ans=min(ans,num)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
sdate = [0] * (n+1)
sdatw = [0] * (n+1)
#print(sdate)
import sys
for i in range(1, n+1):
    #print(i)
    if s[i-1] == "W":
        sdatw[i] += 1
    if s[i-1] == "E":
        sdate[i] += 1
    sdate[i] += sdate[i-1]
    sdatw[i] += sdatw[i-1]

#print(sdate)
#print(sdatw)
res = 9999999999
for i in range(1, n + 1):
    res = min(res, sdatw[i-1] +  sdate[n] - sdate[i])
print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w_cnt = [0] * (n+1)
e_cnt = [0] * (n+1)
w_cnt[0], e_cnt[0] = 0, 0
for i in range(1, n+1):
    if s[i-1] == 'W':
        w_cnt[i] = w_cnt[i-1] + 1
        e_cnt[i] = e_cnt[i-1]
    else:
        w_cnt[i] = w_cnt[i-1]
        e_cnt[i] = e_cnt[i-1] + 1
#print(w_cnt)
#print(e_cnt)
ans = n
for i in range(1, n+1):
    ans = min(ans, w_cnt[i-1] + (e_cnt[n] - e_cnt[i]))
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    S = tuple(input())

    c_e = S.count('E')
    c_w = 0
    ans = n

    for s in S:
        if s == 'E':
            c_e -= 1

        ans = min(ans, c_w + c_e)

        # 自分がWの場合はmax計算の後にカウントする
        if s == 'W':
            c_w += 1

    print(ans)
if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ES, WS= [0]*(N+1), [0]*(N+1)
ES[0], WS[0] = 0, 0
for i in range(1, N+1):
  if S[i-1] == "E":
    ES[i] = ES[i-1] + 1
    WS[i] = WS[i-1]
  elif S[i-1] == "W":
    WS[i] = WS[i-1] + 1
    ES[i] = ES[i-1]
ans = 10**18
for i in range(1, N+1):
  ans = min(ans, WS[i-1] + ES[N] - ES[i])
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    S = list(input())
    min_c = n
    count_e = S.count('E')
    count_w = 0

    for s in S:
        if s == 'E':
            count_e -= 1
        min_c = min(min_c, count_e + count_w)
        if s == 'W':
            count_w += 1

    print(min_c)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

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

e_cnt = [0] * N
for i in range(0, N - 1)[::-1]:
    e_cnt[i] = e_cnt[i + 1] + (1 if S[i + 1] == "E" else 0)

ans = 10**9
for i in range(N):
    ans = min(ans, w_cnt[i] + e_cnt[i])

print(ans)

Submission status: Accepted

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

N = int(input())
S = input().rstrip('\r\n')
counts = [0] * N
count = 0
for i, s in enumerate(S):
    counts[i] += count
    if s == 'W':
        count += 1
count = 0
for i, s in enumerate(S[::-1]):
    counts[N-1-i] += count
    if s == 'E':
        count += 1
ans = min(counts)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left = 0
right = S.count("E")
ans = False
for i in range(N):
    if i > 0 and S[i - 1] == "W":
        left += 1
    if S[i] == "E":
        right -= 1
    if ans is not False:
        ans = min(ans, left + right)
    else:
        ans = left + right
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left = 0
right = S.count("E")
ans = False
for i in range(N):
    if i > 0:
        left += 1 if S[i - 1] == "W" else 0
    right -= 1 if S[i] == "E" else 0
    if ans is not False:
        ans = min(ans, left + right)
    else:
        ans = left + right
print(ans)

Submission status: Accepted

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

left = [0]*N
right = [0]*N
for i in range(1,N):
    if S[i-1]=='W':
        left[i] += left[i-1] + 1
    else:
        left[i] += left[i-1]
left = np.array(left)

for i in range(1,N):
    if S[N-i]=='E':
        right[i] += right[i-1] + 1
    else:
        right[i] += right[i-1]
right = np.array(right[::-1])

ans = (left+right).min()
print(ans)

Submission status: Accepted

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

cum_w = [0] * (N)
if S[0] == 'W':
  cum_w[0] = 1
for i in range(1,N):
  if S[i] == 'W':
    cum_w[i] = cum_w[i-1] + 1
  else:
    cum_w[i] = cum_w[i-1]
  
ans = N - 1 - cum_w[-1] + cum_w[0]
for i in range(1,N):
  ans = min(ans, cum_w[i-1] + N - i - 1 - cum_w[-1] + cum_w[i])

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
w=[0]*n
for i in range(n):
    if s[i]=="E":
        w[i]=1
from itertools import accumulate    
d=list(accumulate([0]+w))
ans=n
cnt=0
for i in range(n):
    cnt=i-d[i]+d[-1]-d[i+1]
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = [0]*n
w = [0]*n
ans = float("inf")
for i in range(n-1):
  if s[i] == "W":
    e[i+1] += 1
  e[i+1] += e[i]

for i in range(1,n):
  if s[n-i] == "E":
    w[n-i-1] += 1
  w[n-i-1] += w[n-i]

print(min([e[i] + w[i] for i in range(n)]))

Submission status: Accepted

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

wnum=[0]*n
for i in range(n):
  if s[i]=='W':
    wnum[i]+=1
  if i>0:
    wnum[i]+=wnum[i-1]

arr=[0]*n
for i in range(n):
  if i!=0:
    arr[i]+=wnum[i-1]
  arr[i]+=n-i-1-(wnum[-1]-wnum[i])

print(min(arr))

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = input()
dp = [0]*N
for i in range(N-1):
  if S[i] == "W":
    dp[i+1] = dp[i]+1
  else:
    dp[i+1] = dp[i]
dpE = [0]*N
for i in range(1,N)[::-1]:
  if S[i] == "E":
    dpE[i-1] = dpE[i]+1
  else:
    dpE[i-1] = dpE[i]
ret = float("inf")
for i in range(N):
  ret = min(ret,dp[i]+dpE[i])
print(ret)

Submission status: Accepted

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

S,T=[],[]
x,y=0,0
for i in range(n):
    if s[i]=='W':
        x+=1
        S.append(x)
    else:
        S.append(x)
    
    if t[i]=='E':
        y+=1
        T.append(y)
    else:
        T.append(y)

S=[0]+S
T=[0]+T

T.reverse()

z=3*(10**5)
for j in range(n):
    z=min(z,S[j]+T[j+1])

print(z)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
t=list(reversed(s))
#print(s,t)

S,T=[],[]
x,y=0,0
for i in range(n):
    if s[i]=='W':
        x+=1
        S.append(x)
    else:
        S.append(x)
    
    if t[i]=='E':
        y+=1
        T.append(y)
    else:
        T.append(y)

S=[0]+S
T=[0]+T

T.reverse()
#print(S,T)

z=3*(10**5)
for j in range(n):
    z=min(z,S[j]+T[j+1])
    #print(S[j],T[j+1])

print(z)

Submission status: Accepted

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


def calculate(S):
    SSS = []

    totalENum = S.count("E")

    for index, value in enumerate(S):
        if index == 0:
            obj = {}
            if value == "W":
                obj.__setitem__("W", 1)
                obj.__setitem__("E", 0)
            else:
                obj.__setitem__("E", 1)
                obj.__setitem__("W", 0)
            SSS.append(obj)
        else:
            obj = {}
            prevW = SSS[index - 1].__getitem__("W")
            prevE = SSS[index - 1].__getitem__("E")
            if value == "W":
                obj.__setitem__("W", prevW + 1)
                obj.__setitem__("E", prevE)
            else:
                obj.__setitem__("E", prevE + 1)
                obj.__setitem__("W", prevW)

            SSS.append(obj)

    resVal = len(S)
    for index,value in enumerate(S):
        if value == "W":
            resVal = min(resVal,SSS[index].__getitem__("W") - 1 + totalENum - SSS[index].__getitem__("E"))
        else:
            resVal = min(resVal, SSS[index].__getitem__("W") + totalENum - SSS[index].__getitem__("E"))

    return resVal


result = calculate(S)

print(result)

Submission status: Accepted

Solution code:
"""
まあまずは愚直にOrder（N*N）でやろう
これナイーブ計算というらしい
→無理でした。

前処理でWの累積和を計算する
そのあとにインデックス回して最小値を計算する
確かに前処理がO(N)でできるなら、
O(N)　＋　O（N）で計算可能になる！！
"""

INF = 1000000
N = int(input())
S = input()
ans = INF

sum_n = [0]
add_n = 0
# 前処理
# Westを向いている人の数を数える
for s in S:
  if s == "W":
    add_n += 1
  sum_n.append(add_n)

# リーダーの添字 = i
for i in range(N):
  # i番目より西にいる人のWを向いている人の数
  if i > 0:
    b_i = sum_n[i] - sum_n[0]
  else:
    b_i = 0

  # i番目より東にいる人のWを向いている人の数
  if i < N:
    f_i = (N - (i + 1)) - (sum_n[N] - sum_n[i+1])
  else:
    f_i = 0
  
  tmp = b_i + f_i
  ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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


A = []
for i in range(N):
  if S[i] == "W":
    A.append(1)
  elif S[i] == "E":
    A.append(-1)
    
R = [A[0]]
for i in range(1,N):
  R.append(R[-1] + A[i])


saidai = 0
for i in range(N):
  if i == 0:
    if saidai < R[N-1] - R[0]:
      saidai = R[N-1] - R[0]
      flag = i
  elif i == N-1:
    if saidai < -R[N-2]:
        saidai = -R[N-2]
        flag = i
  else:
    if saidai < (R[N-1] - R[i]) - R[i-1]:
        saidai = (R[N-1] - R[i]) - R[i-1]
        flag = i  
    
ans = A[:flag].count(1) + A[flag+1:].count(-1)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
 
we = []
west = 0
east = 0
for i in range(N):
    if S[i] == "W":
        west += 1
    elif S[i] == "E":
        east += 1
    we.append([west,east])
 
ans = 1e9
for i in range(N):
    if i == 0:
        left = 0
    else:
        left = we[i][0]
    right = we[N-1][1] - we[i][1]
    ans = min(ans,left+right)
 
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = s.count("E")
count = ans
for i in s:
  if i == "E":
    count -= 1
  else:
    count += 1
  ans = min(ans,count)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
lc, rc = 0, 0
for i in range(1, N):
  if S[i] == 'E':
    rc += 1
m = lc + rc
for i in range(N-1):
  if S[i] == 'W':
    lc += 1
  if S[i+1] == 'E':
    rc -= 1
  if lc + rc < m:
    m = lc + rc
print(m)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
A = [0]
B = [0]
for i in range(n-1):
    if s[i] == "W":
        A.append(A[i]+1)
    elif s[i] == "E":
        A.append(A[i])
    if s[n-i-1] == "E":
        B.append(B[i]+1)
    elif s[n-i-1] == "W":
        B.append(B[i])
m = n
for i in range(n):
    if A[i] + B[n-i-1] < m:
        m = A[i] + B[n-i-1]
print(m)

Submission status: Accepted

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