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()

# s[i]地点までで東と西を向いている数の累積和
w = [0] * (n + 1)
e = [0] * (n + 1)
for i in range(n):
    w[i + 1] += w[i]
    if s[i] == 'W':
        w[i + 1] += 1
    e[i + 1] += e[i]
    if s[i] == 'E':
        e[i + 1] += 1

ans = 10**9
for i in range(n):
    temp = (w[i] - w[0]) + (e[n] - e[i+1])
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
DP=[0]*N
l=0
for i,x in enumerate(S):
  DP[i]+=l
  if S[i]=="W":
      l+=1
r=0
for j,y in enumerate(S[::-1]):
  DP[N-1-j]+=r
  if S[N-1-j]=="E":
      r+=1
print(min(DP))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
DP=[0]*N
l=0
for i,x in enumerate(S):
  DP[i]+=l
  if x=="E":
    l+=1
r=0
for i,y in enumerate(S[::-1]):
  DP[N-1-i]+=r
  if y=="W":
    r+=1
print(N-1-max(DP))

Submission status: Accepted

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

ruir=[0]*N
ruil=[0]*N


for i in range(1,N):
    nowr=ruir[i-1]
    if S[i-1]=="W":
        nowr+=1
    ruir[i]=nowr

    nowl=ruil[-i]
    if S[-(i)]=="E":
        nowl+=1
    ruil[-(i+1)]=nowl

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
left = [0] * n
right = [0] * n

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

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

total = [x + y for x, y in zip(left, right)]
ans = min(total)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=ss()
wcnt,ecnt=0,0
ans=10**100
wsum=arr.count("W")
esum=n-wsum

for i in range(n):
    if arr[i]=="W":
        ans=min(wcnt+esum-ecnt,ans)
        #print(wcnt+esum-ecnt)

        wcnt+=1
    else:
        ecnt+=1
        ans=min(wcnt+esum-ecnt,ans)
        #(wcnt+esum-ecnt)
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
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
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = list(input())
c = Counter(S)
w = c["W"]
e = c["E"]
al = w + e
# WWWWW -> EEEEE
# 変えるべきE
ans = e
num_w, num_e = 0, e
for i in range(N):
    sign = S[i]
    if sign == "W":
        w += 1
        al = al + 1
        num_w += 1
    elif sign == "E":
        num_e -= 1
    ans = min(ans, num_e + num_w)
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
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:
def main():
    n = int(input())
    a = [c == 'E' for c in input()]

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *acc_e, = accumulate(a)

    ans = 1 << 30
    for i in range(n):
        ans = min(ans, (i - acc_e[i]) + (acc_e[n] - acc_e[i + 1]))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
s=input()
w=list(map(lambda x: x=="W",s))
e=list(map(lambda x: x=="E",s))
w = np.array([0]+w).cumsum()
e = np.array([0]+e[::-1]).cumsum()
e = e[::-1]
ans=n
for i in range(n+1):
    tmp=w[i-1]+e[i]
    ans=min(ans,tmp)
print(ans)

Submission status: Accepted

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

L = [0]*N

l = 0
for i, x in enumerate(S):
    L[i] += l
    if x == 'E':
        l += 1
r = 0
for i, x in enumerate(S[::-1]):
    L[N-1-i] += r
    if x == 'W':
        r += 1

print(N-1 - max(L))

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
# EりーだーW
cnt = [0] * (n+1)
for i in range(1, n+1):
    cnt[i] += cnt[i-1]
    if s[i-1] == "E": cnt[i] += 1

ans = float("INF")
for i in range(1,n+1):
    a = ((i-1) - cnt[i-1]) + (cnt[-1] - cnt[i])
    if ans > a: ans = a
print(ans)

Submission status: Accepted

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

l = 0
L = []
r = 0
R = [0] * N
for i in range(N):
  if i == 0:
    L.append(0)
  else:
    if S[i-1] == 'W':
      l += 1
    L.append(l)
    
for i in range(N-1, -1, -1):
  if i == N-1:
    R[i] = 0
  else:
    if S[i+1] == 'E':
      r += 1
    R[i] = r
    
count = []
for i in range(N):
  count.append(L[i] + R[i])

print(min(count))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=[0]*n
e=[0]*n
for i in range(n):
  if s[i]=="W":
    w[i]+=1
  else:
    e[i]+=1
from itertools import accumulate
w=list(accumulate(w))
e=list(accumulate(e))
# print("w:",w)
# print("e:",e)
ans=10**18
for i in range(n):
  t=0
  if s[i]=="W":
    if i!=0:
      t+=w[i-1]
    t+=e[-1]-e[i]
  else:
    if i!=0:
      t+=w[i-1]
    t+=e[-1]-e[i]
  ans=min(t,ans)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()

cnt = Counter(s)
W = cnt['W']
E = cnt['E']
#print(E,W)

l = [[0, 0] for i in range(n + 1)]

dict = {'E':0, 'W':1}
for i in range(n):
    temp1, temp2 = l[i]
    l[i + 1] = [temp1, temp2]
    l[i + 1][dict[s[i]]] += 1
#print(l)
count = 10 ** 7
for i in range(n):
    e, w = l[i + 1]
    temp = E - e + w if s[i] != 'W' else E - e + w - 1
    if count > temp:count = temp
    #print(temp, count)
print(count)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
l=0
r=S[1:].count("E")
#print(r)
answer=N

for i in range(N-1):
    if l+r<answer:
        answer=l+r
    if S[i]=="W":
        l+=1
    if S[i+1]=="E":
        r-=1
if l+r<answer:
    answer=l+r
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
l=input()
ans=[0]*n
wc=0
ec=0
for i in range(n-1):
  if l[i]=="W":
    wc+=1
  ans[i+1]+=wc
  if l[n-1-i]=="E":
    ec+=1
  ans[n-2-i]+=ec

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
S = [int(c == 'W') for c in list(input())]
L = [0]
for i in range(n):
    L.append(L[-1]+S[i])
R = [0]
for i in range(n-1, 0, -1):
    R.append(R[-1]+1-S[i])
R.reverse()
print(min([L[i]+R[i] for i in range(n)]))

Submission status: Accepted

Solution code:
N = int(input())
S = input().strip()
E = [0 for _ in range(N+1)]
W = [0 for _ in range(N+1)]
for i in range(N):
    if S[i] == "E":
        E[i+1] = E[i]+1
        W[i+1] = W[i]
    else:
        W[i+1] = W[i]+1
        E[i+1] = E[i]
cmin = 10**6
for i in range(N):
    cmin = min(cmin,W[i]+E[-1]-E[i+1])
print(cmin)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    west = [0 for _ in range(n)]
    west[0] = 1 if s[0] == "W" else 0
    for i in range(1, n):
        if s[i] == "W":
            west[i] = west[i - 1] + 1
        else:
            west[i] = west[i - 1]
    answer = n - west[-1]
    if s[0] == "E":
        answer -= 1
    for i in range(1, n):
        answer = min(answer, west[i - 1] + n - i - west[-1] + west[i - 1])
    tmp = 0 if s[-1] == "E" else 1
    answer = min(answer, west[-1] - tmp)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

east = S.count("E")
west = 0

ans = 10 ** 18
for s in S:
    if s == "W":
        ans = min(ans, east + west)
        west += 1
    else:
        east -= 1
        ans = min(ans, east + west)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    ans = 10**9
    past_w = 0
    past_e = 0
    for s in S:
        if s == "W":
            ans = min(ans, past_w + c["E"]-past_e)
            past_w += 1
        else:
            past_e += 1
            ans = min(ans, past_w + c["E"]-past_e)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
# https://atcoder.jp/contests/arc098/tasks/arc098_a

# i番目の人をリーダーにしたときに何人向く方向を変えるかを高速に取得できれば良い
# [0,i)のWの人数+[i+1,)のEの人数がi番目の人をリーダーにしたときに変える個数
# 上記の一行目と二項目はi→i+1のときに逐次計算できる
N = read_a_int()
S = read()[:-1]
n_W = 0
n_E = S.count('E')
ans = 10**6
for s in S:
    if s == 'E':
        n_E -= 1
    ans = min(ans, n_E + n_W)
    if s == 'W':
        n_W += 1
print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    S=ST()
    
    left_d = {"E":0, "W":0}
    right_d = Counter(S[:])
    ans = inf
    for i in range(N):
        right_d[S[i]]-=1
        cost = left_d["W"] + right_d["E"]
        ans = min(ans,cost)
        left_d[S[i]]+=1
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s_li = list(input())
all_li = [[0,0]]
for s in s_li:
    if s == 'W':
        all_li.append([all_li[-1][0]+1,all_li[-1][1]])
    else:
        all_li.append([all_li[-1][0],all_li[-1][1]+1])
#print(all_li)
ans = float('inf')
for i,a in enumerate(all_li):
    if i == 0:
        continue
    tmp_count = 0
    if i-1>0:
        tmp_count += all_li[i-1][0]
    tmp_count += (all_li[-1][1]-a[1])
    if tmp_count < ans:
        ans = tmp_count
    #print(i,a,tmp_count)
print(ans)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = list(input())
    cnt_w = s.count('W')
    cnt_e = s.count('E')

    tmp_w = 0
    tmp_e = 0

    ans = []

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

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

    print(min(ans))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5
WEEWW"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """12
WEWEWEEEWWWE"""
        output = """4"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """8
WWWWWEEE"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=input()
    ans=s[1:].count("E")
    cnt=ans
    for i in range(1,n):
        if s[i-1]=="W":
            cnt+=1
        if s[i]=="E":
            cnt-=1
        ans=min(ans,cnt)
    print(ans)
if __name__=="__main__":
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

left = [0 for _ in range(N)]
cnt = 0
for i in range(0, N):
    left[i] = cnt
    if S[i] == 'W':
        cnt += 1

right = [0 for _ in range(N)]
cnt = 0
for i in range(N - 1, -1, -1):
    right[i] = cnt
    if S[i] == 'E':
        cnt += 1

total = [l + r for l, r in zip(left, right)]
print(min(total))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ecnt = [0]*n
wcnt = [0]*n
res = 0
for i in range(1,n):
    #ecnt
    if s[i-1] == 'E':res +=1
    ecnt[i] = res
res = 0
for i in range(n-2,-1,-1):
    if s[i+1] == 'W':res+=1
    wcnt[i] = res
ans = 0
for i in range(n):
    ans = max(ans,ecnt[i]+wcnt[i])
ans = n-1-ans
print(ans)

Submission status: Accepted

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

W,E = [0]*N, [0]*N

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

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

A = [0]*N
for i in range(N):
    A[i] = E[i]+W[i]

ans = min(A)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    east = [0 for _ in range(n)]
    west = [0 for _ in range(n)]
    if s[0] == "E":
        east[0] = 1
    else:
        west[0] = 1
    for i in range(1, n):
        east[i] = east[i - 1]
        west[i] = west[i - 1]
        if s[i] == "E":
            east[i] += 1
        else:
            west[i] += 1
    answer = east[-1] - east[0]
    for i in range(1, n):
        answer = min(answer, west[i - 1] + east[-1] - east[i])
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
wr = s.count('W')
er = n-wr
wl = 0
el = 0
ans = 10**10
for i in range(n):
    if s[i] == 'W':
        wr -= 1
        ans = min(ans,er+wl)
        wl += 1
    else:
        er -= 1
        ans = min(ans,er+wl)
        el += 1
print(ans)

Submission status: Accepted

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

cnt_left = 0
cnt_right = s[1:].count('E')
ret = cnt_left + cnt_right

for i in range(1, n):
    if s[i] == 'E':
        cnt_right -= 1
    if s[i-1] == 'W':
        cnt_left += 1
    ret = min(ret, cnt_left + cnt_right)

print(ret)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    S = input()

    W_list = [0 for _ in range(N)]
    E_list = [0 for _ in range(N)]
    
    for i, s in enumerate(S):
        if i == 0:
            if s == 'W':
                W_list[i] = 1
        else:
            W_list[i] = W_list[i-1]
            if s == 'W':
                W_list[i] += 1

        
    for i, s in enumerate(S[::-1]):
        if i == 0:
            if s == 'E':
                E_list[i] = 1
        else:
            E_list[i] = E_list[i-1]
            if s == 'E':
                E_list[i] += 1
    
    E_list = E_list[::-1]

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

    res = min(sum_list) -1
    print(res)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

s = S[::-1]
w = [0]*(N+1)
e = [0]*(N+1)
cnt_w = 0
cnt_e = 0

for i in range(N):
    if(S[i] == "W"):
        cnt_w += 1
        w[i+1] = cnt_w
    else:
        w[i+1] = cnt_w

for i in range(N):
    if(s[i] == "E"):
        cnt_e += 1
        e[i+1] = cnt_e
    else:
        e[i+1] = cnt_e

e = e[::-1]        
ans = 2*N

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
inv_s = s[::-1]
lst_e =[]
lst_w =[]
e = w = 0
for i in range(n):
  if s[i] == 'W':
    w += 1
  if inv_s[i] == 'E':
    e += 1
  lst_e.append(e)
  lst_w.append(w)
lst_w.reverse()


ans = [lst_e[i]+lst_w[i] for i in range(n)]
print(min(ans)-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ww = []
tmp = 0
for i in range(n):
    if s[i] == 'W':
        tmp += 1
    ww.append(tmp)
ee = []
tmp = 0
for i in range(n)[::-1]:
    if s[i] == 'E':
        tmp += 1
    ee.append(tmp)
ee = ee[::-1]
a = [ee[0]] +  [ee[i+1] + ww[i-1] for i in range(1,n-1)] + [ww[-1]]
print(min(a))

Submission status: Accepted

Solution code:
#!python3

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


def main():
    left = {"E": 0, "W": 0}
    right = {"E": S.count("E"), "W": S.count("W")}

    ans = N
    for s in S:
        right[s] -= 1
        ans = min(ans, left["W"] + right["E"])
        left[s] += 1
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

r = [0] * n #n人目をリーダーとしたときに東を向かなければいけない人数
l = [0] * n #n人目をリーダーとしたときに西を向かなければいけない人数

r[0] = 0
l[-1] = 0
for i in range(1,n):
    r[i] = r[i-1] + (s[i-1]=='W')
    l[-i-1] = l[-i] + (s[-i]=='E')

ans = n

for i in range(n):
    ans = min(ans, r[i]+l[i])

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
s = input()

l = [0] * n
for i, v in enumerate(s):
    if v == "W":
        l[i] = 1
m = [0] * n
for j, w in enumerate(s):
    if w == "E":
        m[j] = 1

l = list(accumulate(l))
m = list(accumulate(m))

ans = m[-1]
for k in range(1, n):
    ans = min(l[k-1] + m[-1] - m[k], ans)

print(ans)

Submission status: Accepted

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

num_W = [0]*N
num_E = [0]*N
for i in range(N):
    if S[i] == "W":
        num_W[i] = 1
    else:
        num_E[i] = 1

for i in range(N-1):
    num_W[i+1] += num_W[i]
    num_E[i+1] += num_E[i]

ans = num_E[N-1]

for i in range(N):
    num_E[i] = num_E[N-1] - num_E[i]

for i in range(1, N):
    num = num_W[i-1] + num_E[i] - num_E[N-1]
    ans = min(ans, num)
#    print i, num, num_W[i-1], num_E[i]- num_E[N-1]                                                    

print ans

Submission status: Accepted

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

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

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

ans = N
for nw, ne in zip(num_west, num_east):
  ans = min(ans, nw + ne)

print(ans)

Submission status: Accepted

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

W = [0]
E = [0]
for i in range(n):
    if S[i] == 'W':
        W.append(W[i]+1)
        E.append(E[i])
    else:
        W.append(W[i])
        E.append(E[i]+1)

#print(W)
#print(E)

ans = 3*10**5
for i in range(1, n+1):
    ans = min(ans, W[i-1]+E[-1]-E[i])
    #print(W[i-1]+E[-1]-E[i], W[i-1], E[-1]-E[i])
print(ans)

Submission status: Accepted

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

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

for idx, s in enumerate(S):
    if idx == 0:
        continue
    if S[idx-1] == "W":
        now += 1
    if s == "E":
        now -= 1
    ans = min(ans, now)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input().rstrip('\n'))
cumsum = [[0, 0] for i in range(n)]
cnt_1 = 0
cnt_2 = 0
cnt = [-1] * n
for i in range(n):
    if s[i] == 'E':
        cnt_1 += 1
    else:
        cnt_2 += 1
    cumsum[i][0] = cnt_1
    cumsum[i][1] = cnt_2
for i in range(n):
    if s[i] == 'E':
        cnt[i] += 1
    cnt[i] += cumsum[n - 1][0] - cumsum[i][0] + cumsum[i][1]
print(min(cnt))

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

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

ret = sum(s == 'E' for s in S[1:]) # 最初は一番左の人をリーダーに
variation = ret
for i in range(N-1):
    if S[i] == 'W':
        variation += 1
    if S[i+1] == 'E':
        variation -= 1
    if ret > variation:
        ret = variation
        
print(ret)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
count = Counter(S)
min_ = count['E'] if S[0]=='W' else count['E']-1
total = min_
for i in range(1,N):
    plus = 1 if S[i-1]=='W' else 0
    plus += -1 if S[i]=='E' else 0
    total += plus
    if total < min_:
        min_ = total
print(min_)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 1001001001
ll = [0]
rl = [0]
l = 0
r = 0
for i in range(1, len(s)):
    if s[i - 1] == 'W':
        l += 1
    ll.append(l)
    if s[-i] == 'E':
        r += 1
    rl.append(r)
rl.reverse()

# print(ll)
# print(rl)

for a, b in zip(ll, rl):
    ans = min(ans, a + b)
print(ans)

Submission status: Accepted

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

ws = []
count = 0
for v in s:
  ws.append(count)
  if v == 'W':
    count += 1
  

count = 0
es = []
for v in s[::-1]:
  es.append(count)
  if v == 'E':
    count += 1

  
es = list(reversed(es))
ans = 10**9
for w,e in zip(ws,es):
  ans = min(ans, w+e)
  
print(ans)

Submission status: Accepted

Solution code:
n, S = int(input()), input().strip()

scores = [sum(c == 'E' for c in S[1:])]
for i in range(1, n):
    scores.append(scores[i-1] + (S[i-1]=='W') - (S[i]=='E'))
print(min(scores))

Submission status: Accepted

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

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

ans = 1e9
for i in range(N):
  turn_w = cusum_w[i]
  turn_e = cusum_e[N] - cusum_e[i+1]
  ans = min(ans, turn_w + turn_e)
  #print(i, turn_w, turn_e)

print(ans)

Submission status: Accepted

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

W = list(range(N))
E = list(range(N))
w, e = 0, 0

if S[0] == 'W':
    W[0] = 1
    E[0] = 0
else:
    W[0] = 0
    E[0] = 1

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

    else:
        W[i] = W[i-1]
        E[i] = E[i-1] + 1

min_cost = float('inf')
for i in range(N):
    cost = 0
    if i-1 != 0:
        cost += W[i-1]
    cost += E[N-1] - E[i]
    min_cost = min(min_cost, cost)
    # print(cost)

print(min_cost)
# print(W)
# print(E)

Submission status: Accepted

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

W = 0
E = A.count("E")
ans = n+1

for i in A:
    if i == "W":
        W += 1
    else:
        E -= 1
    if W + E < ans:
        ans = W + E
print(min(ans, A.count("E"), A.count("W")))

Submission status: Accepted

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

left=[0]*N
right=[0]*N
to_change=[0]*N

if S[0]=="E":
    right[0]=1
else:
    left[0]=1

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

to_change[0]=right[-1]-right[0]
to_change[-1]=left[-2]

for j in range(1,N-1):
    to_change[j]=(right[-1]-right[j])+(left[j-1])
#print(to_change)
print(min(to_change))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
s = list(input())
for i in range(n):
    if s[i] == 'E':
        s[i] = 0
    else:
        s[i] = 1
cs = np.cumsum(s)
minturn = n
for i in range(n):
    if i == 0:
        turn = n-cs[n-1]+cs[0]-1
    else:
        turn = cs[i-1]+n-i-cs[n-1]+cs[i]-1
    minturn = min(turn,minturn)
print(minturn)

Submission status: Accepted

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

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

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

ans=N
for i in range(N):
    ans=min(ans,wsum[i]+esum[i]-1)
print(ans)

Submission status: Accepted

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

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

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=input()
data=[0 for i in range(0,N)]
for i in range(1,N):
    if S[i]!='W':
        data[0]+=1
for i in range(1,N):
    x=0
    y=0
    if S[i-1]!='E':
        x=1
    if S[i]!='W':
        y=1
    data[i]=data[i-1]+x-y
print(min(data))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
tmp = c['E']
best = tmp - (1 if s[0] == 'E' else 0)
for i in range(1, n):
    if s[i - 1] == 'W':
        tmp += 1
    else:
        tmp -= 1
    if tmp - (1 if s[i] == 'E' else 0) < best:
        best = tmp - (1 if s[i] == 'E' else 0)
print(best)