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:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N = int(sys.stdin.buffer.readline())
S = sys.stdin.buffer.readline().decode().rstrip()

S = np.array(list(S), dtype='U1')
wc = (S == 'W').cumsum()
ec = (S == 'E')[::-1].cumsum()[::-1]

ans = min(wc[-2], ec[1])
for i in range(1, N - 1):
    ans = min(ans, wc[i - 1] + ec[i + 1])
print(ans)

Submission status: Accepted

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

wcnt=0
for i in range(1,N):
    if S[i] == "E":
        wcnt+=1

ans=wcnt
for i in range(N-1):
    if S[i] == "E" and S[i+1]=="E":
        wcnt-=1
    if S[i] == "W" and S[i+1]=="W":
        wcnt+=1
    if ans > wcnt:
        ans=wcnt
print(ans)

Submission status: Accepted

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

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

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

enums.reverse()

ans = 10**9
for i in range(N):
    num = wnums[i] + enums[i]
    if ans > num:
        ans = num
print(ans)

Submission status: Accepted

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

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

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

ans = 10**9
for i in range(N):
    num = wnums[i] + enums[N-i-1]
    if ans > num:
        ans = num
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
lm = [0] *(n+1)
rm = [0] *(n+1)

def check_l(x):
    if x=='W':
        r = 1
    else:
        r = 0
    return r
def check_r(x):
    if x=='E':
        r = 1
    else:
        r = 0
    return r
    
for i in range(1,n):
    lm[i+1] = lm[i] + check_l(s[i-1])
for i in range(n-1,0,-1):
    rm[i] = rm[i+1] + check_r(s[i])
ans = float('inf')
for i in range(1,n+1):
    ans = min(ans,lm[i]+rm[i])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
CW = [0]*N
CS = [0]*N
cw = 0
cs = 0
for i in range(N-1):
    if S[i] == 'W':
        cw += 1
    CW[i + 1] = cw
    if S[N - i - 1] == 'E':
        cs += 1
    CS[N - i - 2] = cs

C = [0]*N
for i in range(N):
    C[i] = CW[i] + CS[i]
ans = min(C)
# print(CW, CS, C)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()
    S = Scanner.string()
    e, w = 0, 0
    e = S.count("E")
    ans = int(1e09)
    for i in range(N):
        if S[i] == "E":
            e -= 1
        ans = min(ans, e+w)
        if S[i] == "W":
            w += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
s = input().strip()
d = {"W":0,"E":0}
x=[]
for i, c in enumerate(s):
    x.append(d["W"])
    d[c]+=1
d = {"W":0,"E":0}
r = N
for i, c in enumerate(s[::-1]):
    r = min(r, x[N-i-1]+d["E"])
    d[c]+=1
print(r)

Submission status: Accepted

Solution code:
import sys
import math
import functools
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
s = ns()

now = s[1:].count("E")
ans = now
for spre, si in zip(s[:-1], s[1:]):
    if spre == "W":
        now += 1
    if si == "E":
        now -= 1
    ans = min(ans, now)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=input()
tmp = 0
for i in s[1:]:
    if i == "E":
        tmp += 1
ans = tmp
for i in range(1,N):
    if s[i] =="W" and s[i-1]=="W":
        tmp += 1
    elif s[i]=="E" and s[i-1]=="E":
        tmp -=1
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
def examC():
    N = I()
    S = SI()
    E = S.count("E"); W = N-E
    curW = 0; curE = 0
    ans = N-1
    for i in range(N):
        if S[i]=="W":
            curW +=1
        cur = N -1 - (W-curW) - curE
        if S[i]=="E":
            curE +=1
        ans = min(ans,cur)
#        print(cur)
    print(ans)
    return

def examD():
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,ALPHABET
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]
ALPHABET = [chr(ord('A') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

Submission status: Accepted

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

for i in range(N - 1):
  #E,Wが互い違いになっていると相殺されるため手数は変わらない
  #iが増える = カーソルが右に行くにつれ
  #Eが続けて増えると手数は減り(カーソルの方向へ向く人が増えるため)、
  #Wが続けて増えると手数は増える(カーソルと逆方向を向く人が増えるため)
  if S[i + 1] == "E":
    #累積和を取っていくとともに、東の方向を向いている人数を記録する
    #カーソルが西端にあるとき、Eの総数が手数となる
    ##Eが続けて増えるとき累積和が減り、Eの総数からカーソル以西のEが相殺され、
    ##カーソル以東のEだけが計上される
    e += 1
    if S[i] == "E":
      L[i + 1] = L[i] - 1
    else: 
      L[i + 1] = L[i]
  else:
    if S[i] == "W":
      L[i + 1] = L[i] + 1
    else:
      L[i + 1] = L[i]

ans = min(L) + e
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
 
N = int(input())
S = list(input())
 
A = [0]*(N+1) #N+1まで入れること忘れず
 
#リーダーの方向を向く，W->1の累積和，[0]start
for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j
#A =list(accumulate(A))
#print(A)
 
#iより左にいて西を向いている人の最小値
ans=N
for i in range(1, N+1):
    val = N-i +A[i] -A[N]
    ans = min(ans, A[i-1] +val)
 
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e_ls = [0]
w_ls = [0]
cnt = 0
for _s in s[:-1]:
    if _s == "E":
        cnt += 1
    e_ls.append(cnt)

cnt = 0
for _s in s[::-1][:-1]:
    if _s == "W":
        cnt += 1
    w_ls.append(cnt)
w_ls.reverse()

print(n - max([e + w for e, w in zip(e_ls, w_ls)]) - 1)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
lw = [0] * N
re = [0] * N
for i in range(1, N):
  lw[i] = lw[i-1] + (S[i-1] == "W")
  re[-i-1] = re[-i] + (S[-i] == "E")
ans = 10**6
for i in range(N):
  ans = min(ans, lw[i] + re[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = [0] * n
e = [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]
for i in range(n-2,-1,-1):
  if s[i+1] == "E":
    e[i] = e[i+1] + 1
  else:
    e[i] = e[i+1]
m = n
for i in range(n):
  m = min(m,e[i]+w[i])
print(m)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
p0,p1=0,ecnt-(S[0]==e)
ans=p0+p1
p=S[0]
for i in S[1:]:
  p0,p1=(p0+(p==w),p1-(i==e))
  #(l and gyaku,r and gyaku)
  ans=min(ans,p0+p1)
  p=i
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
p0,p1=0,ecnt-int(S[0]==e)
ans=p0+p1
p=S[0]
for i in S[1:]:
  p0,p1=(p0+int(p==w),p1-int(i==e))
  #(l and gyaku,r and gyaku)
  ans=min(ans,p0+p1)
  p=i
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
p0,p1=0,ecnt-int(S[0]==e)
ans=p0+p1
for i in range(1,n):
  p0,p1=(p0+int(S[i-1]==w),p1-int(S[i]==e))
  #(l and gyaku,r and gyaku)
  ans=min(ans,p0+p1)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
w,e='W','E'
if e not in S or w not in S:print(0);exit()
ecnt=len([1 for i in list(S) if i==e])
l=[0 for i in range(n)]
l[0]=(0,ecnt-int(S[0]==e))
p0,p1=l[0]
for i in range(1,n):
  l[i]=(p0+int(S[i-1]==w),p1-int(S[i]==e))
  #(l and gyaku,r and gyaku)
  p0,p1=l[i]
print(min([a+s for a,s in l]))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
e = 0
for i in s:
    if i == 'E':
        e += 1
left = 0
right = e
ans = left + right
for i in range(n):
    if s[i] == 'E':
        right -= 1
    ans = min(ans,left+right)
    if s[i] == 'W':
        left += 1
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
s = str(input())
tt = [1 if i  == "W" else 0 for i in s]
B = [0] + tt
B = list(accumulate(B))
ans = float('Inf')
#0とn+1にすることで一番左側と一番右側を選べる

for i in range(n+1):
  	#print(s[0:i],s[i:])
    #ここをprintすればどこで文字列を分けているかわかる．
	#左にいるWの数
    w = B[i]
	#右にいるEの数
    #まず右側の個数は n - i
    #次に右側のWはB[-1] - B[i]
    #WとEしかいないから
    e = n - i - B[-1] + B[i]
    ans = min(ans,w+e)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
s = str(input())
tt = [1 if i  == "W" else 0 for i in s]
B = [0] + tt
B = list(accumulate(B))
#print(B)
ans = float('Inf')
for i in range(0,n+1):
    #w = s[:i].count("W")
    ##W = sum(tt[:i])
    w = B[i]
    #print(W,w)
    #e = s[i:].count("E")
    #E = len(tt[i:]) - sum(tt[i:])
    e = n - i - B[-1] + B[i]
    #print(E,e)
    #ans = min(ans,W+E)
    ans = min(ans,w+e)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = list(input())
    S = list(map(lambda x: 0 if x == "W" else 1, S))

    E = 0
    W = sum(S[1:N])

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

    print(p)


main()

Submission status: Accepted

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

West=[0]
East=[0]
for i in range(1,N+1):
    if S[i-1]=="W":
        West.append(West[-1]+1)
        East.append(East[-1])
    else:
        West.append(West[-1])
        East.append(East[-1]+1)
ans=N
for j in range(N):
    p=West[j]+East[-1]-East[j+1]
    if ans>p:
        ans=p
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

A = [0]*(N+1) #N+1まで入れること忘れず

#リーダーの方向を向く，W->1の累積和，[0]start
for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j
#A =list(accumulate(A))
#print(A)

#iより左にいて西を向いている人の最小値
ans=N
for i in range(1, N+1):
    val = N-i +A[i] -A[N]
    ans = min(ans, A[i-1] +val)

print(ans)

Submission status: Accepted

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

west,east = [0], [0]
w,e = 0,0
for i in range(1,n):
    if s[i-1] == 'W':
        w += 1
    west.append(w)
    if s[-i] == 'E':
        e += 1
    east.append(e)

ans = 10**9
for w,e in zip(west, east[::-1]):
    if ans > w + e:
        ans = w+e
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
s=['n']+list(input())
ans=10**6
c=Counter(s)
le,lw=0,0
for i in s:
    if i=='E':
        le+=1
    elif i=='W':
        lw+=1
    ans=min(ans,lw+c['E']-le)
print(ans)

Submission status: Accepted

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

w=[0]*(N+1)
e=[0]*(N+1)
for i,s in enumerate(S):
    w[i+1]+=w[i]
    if s=="W": w[i+1]+=1
    e[i+1]+=e[i]
    if s=="E": e[i+1]+=1
ans=N
for i in range(N):
    now = w[i]+(e[N]-e[i+1])
    if now<ans: ans=now
print(ans)

Submission status: Accepted

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

A=[[0]*(N+1) for i in range(3)]

C=0
D=0
for i in range (1,N+1):
    if(S[i-1]=="W"):
        D+=1
    A[0][i]=D

C=0
D=0
for i in range (N-1,-1,-1):
    if(S[i]=="E"):
        C+=1
    A[1][i]=C

for i in range (N+1):
    A[2][i]=A[1][i]+A[0][i]


print(min(A[2]))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
A = [0 for i in range(n+2)]
A2 = [0 for i in range(n+2)]
for i in range(0,n):
  ndA = i +1
  ndA2 = n-i
  A[ndA] = A[ndA-1] + 1 if s[i]=="W" else A[ndA-1]
  A2[ndA2] = A2[ndA2+1] + 1 if s[n-i-1]=="E" else A2[ndA2+1]
A[n+1]=A[n]
A2[0]=A2[1]
#print(A)
#print(A2)
print(min([A[i-1]+A2[i+1] for i in range(1,n+1)]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, S: str):
    C = {'E': 0, 'W': 0}
    D = {'E': 0, 'W': 0}
    for c in S:
        C[c] += 1
    a = 1e20
    for c in S:
        C[c] -= 1
        a = min(a, C['E'] + D['W'])
        D[c] += 1
    return a


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    S = next(tokens)
    print(solve(N, S))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

c_e = [0]
t = 0
for c in s:
    if c == 'E':
        t += 1
    c_e.append(t)

ans = n
for leader in range(n):
    ans = min(ans, (leader - c_e[leader]) + (c_e[n] - c_e[leader + 1]))
print(ans)
# 累積和のリストは、1-indexed

Submission status: Accepted

Solution code:
N = int(input())
S = input()
cntE = [0 for _ in range(N)]
cntW = [0 for _ in range(N)]
for i in range(N):
    if S[i] == "E":
        cntE[i] = cntE[i-1]+1
        cntW[i] = cntW[i-1]
    else:
        cntE[i] = cntE[i-1]
        cntW[i] = cntW[i-1]+1
sumE = cntE[N-1]
sumW = cntW[N-1]
ans = sumE-cntE[0]
for i in range(1,N):
    ans = min(ans,cntW[i-1]+(sumE-cntE[i]))
print(ans)

Submission status: Accepted

Solution code:
#import math
#import copy
#import sys
#import bisect
#input = sys.stdin.readline
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x):#約数配列
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x):#素因数分解配列
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    #x,y = map(int,input().split())           x=1,y=2
    #a = input().split()                      a=['1','2','3',...,'n']
    #a = list(map(int,input().split()))       a=[1,2,3,4,5,...,n]
    #li = input().split('T')	              FFFTFTTFF => li=['FFF', 'F', '', 'FF']
    #複数行の「1 2 3 4」型の入力を一配列に
    #x = sorted([list(map(int, input().split())) for _ in range(n)])
    #ソート x.sort(key=lambda y:y[1])
    # print("Yes")  print("No")  x=[[0 for _ in range(n)] for _ in range(n)]
    n=int(input())
    s=list(input())
    m=n
    cnt=0
    for i in range(n-1):
        if s[i+1]=="E":
            cnt+=1
    m=min(m,cnt)
    for i in range(n-1):
        if s[i+1]=="E":
            cnt-=1
        if s[i]=="W":
            cnt+=1
        if m > cnt:
            m=cnt
    print(m)
main()

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    s = str(input().rstrip('\n'))
    e = [0] * n
    w = [0] * n
    for i in range(n):
        if s[i] == "W":
            w[i] += 1 if i == 0 else w[i-1] + 1
        else:
            w[i] += 0 if i == 0 else w[i-1]

        if s[-i-1] == "E":
            e[-i-1] += 1 if i == 0 else e[-i] + 1
        else:
            e[-i - 1] += 0 if i == 0 else e[-i]
    mt = 10 ** 10
    for i in range(n):
        mt = min(mt, e[i] + w[i] - 1)
    print(mt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

dp=[0]*N
dp[0]=S[1:].count("W")
for i in range(1,N):
  dpi=dp[i-1]
  if S[i]=="W":
    dpi-=1
  if S[i-1]=="E":
    dpi+=1
  dp[i]=dpi

print(N-max(dp)-1)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
W = S.count('W')
E = S.count('E')
counter = len(S)
cost = E
for i in range(len(S)):
  if S[i] == 'E':
    cost -= 1
    if cost < counter:
      counter = cost
  else:
    if cost < counter:
      counter = cost
    cost +=1
print(counter)

Submission status: Accepted

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

min_n = s[1:].count('E')
tmp_n = min_n
for i in range(1, n):
    tmp_n += s[i-1].count('W') - s[i].count('E')
    min_n = min(tmp_n, min_n)

print(min_n)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
count_e = [0] * N
count_w = [0] * N
for i in range(N-1):
    if S[i] == "W":
        count_w[i+1] = count_w[i] + 1
    else:
        count_w[i+1] = count_w[i]

    if S[N-i-1] == "E":
        count_e[i+1] = count_e[i] + 1
    else:
        count_e[i+1] = count_e[i]

count_e = count_e[::-1]
ans = float('inf')
for i in range(N):
    ans = min(ans, count_w[i] + count_e[i])
print(ans)

Submission status: Accepted

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

s = [0]*(N+1)

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

ans = N
for i in range(1, N+1):
    k = (N - i) - (s[N] - s[i])
    ans = min(ans, k + s[i-1])


print(ans)

Submission status: Accepted

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

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

for i in reversed(range(0, N)):
    k = 0
    if S[i] == "E":
        k = 1
    e[i] = e[i+1] + k

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


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

EW=[0]*N
for i in range(N):
  EW[i]=E[i]+W[i]
print(N-1-(max(EW)-1))

Submission status: Accepted

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

E_num=S.count('E')
W_num=0
ans=n
for i in range(n):
  if S[i]=='E':
    E_num-=1
    ans=min(ans,E_num+W_num)
  else:
    ans=min(ans,E_num+W_num)
    W_num+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E = S.count('E')
W = N - E
result = N
l = 0
r = E
for i in S:
  if i == 'E':
    r-=1
    result = min(result, l+r)
  elif i == 'W':
    result = min(result, l+r)
    l+=1

print(result)

Submission status: Accepted

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

rw = 0
re = 0
lw = 0
le = 0
ans = 10**9

for i in range(n):
    if s[i:i+1] == "W":
        rw += 1
    else:
        re += 1

if s[0:1] == "W":
    rw -= 1
else:
    re -= 1
if re+lw < ans:
    ans = re + lw

for i in range(1,n):
    if s[i:i+1] == "W":
        rw -= 1
    else:
        re -= 1
    if s[i-1:i] == "W":
        lw += 1
    else:
        le += 1
    if re+lw < ans:
        ans = re + lw
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    s = str(input().rstrip('\n'))
    w = [0] * n
    e = [0] * n
    wc = 0
    ec = 0
    for i in range(n):
        if s[i] == "W":
            wc += 1
        if s[-i-1] == "E":
            ec += 1
        w[i] = wc
        e[-i-1] = ec
    m_c = 10 ** 10
    for i in range(n):
        m_c = min(m_c, w[i] + e[i] - 1)
    print(m_c)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007



n = I()
li = S()
cnt = li[1:].count('E')
ret = cnt
for i in range(n - 1):
    if li[i + 1] == 'E':
        cnt -= 1
    if li[i] == 'W':
        cnt += 1
    ret = min(ret, cnt)



print(ret)

Submission status: Accepted

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

lW = 0
rE = S[1:N].count("E")
mi = lW + rE
for i in range(1, N):
    if S[i - 1] == "W":
        lW += 1
    if S[i] == "E":
        rE -= 1
    if lW + rE < mi:
        mi = lW + rE

print(mi)

Submission status: Accepted

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

x = [0] * N
if S[0] == "E":
    x[0] = 1

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

z = x[N-1]
if S[0] == "E":
    ans = z-1
else:
    ans = z

for i in range(N-1):
    tmp = z - 2 * x[i]
    if S[i+1] == "E":
        tmp = tmp - 1
    tmp += (i+1)
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

n=int(input())
#tmp = input().split()
#hoge = list(map(lambda a: int(a), tmp))
s=input()

sumw=[]
sumw.append(0)
for i in range(1,n+1):
	if(s[i-1]=="W"):
		tmp = sumw[i-1]+1
	else:
		tmp = sumw[i-1]
	sumw.append(tmp)
sume=[]
sume.append(0)
for i in range(1,n+1):
	if(s[n-i]=="E"):
		tmp = sume[i-1]+1
	else:
		tmp = sume[i-1]
	sume.append(tmp)

mini=sume[n-1]
for i in range(2,n+1):
	if(i == n):
		tmp=sumw[n-1]
	else:
		tmp=sumw[i-1]+sume[n-i+1]
	
	mini=min(mini,tmp)

#print(sumw)
#print(sume)
#print(n)
#print(s)
print(mini)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    l, r = 0, s[1:].count("E")  # people needed to change the direction
    cnt = l + r
    for i in range(1, n):
        if s[i - 1] == "W":
            l += 1
        if s[i] == "E":
            r -= 1
        cnt = min(cnt, l + r)
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a=[0]*n
if(s[0]=='W'):
  a[0]=1
for i in range(1,n):
  if(s[i]=='W'):
    a[i]=a[i-1]+1
  else:
    a[i]=a[i-1]
ans=min(n-a[n-1]+a[0]-1,a[n-2])
for j in range(1,n-1):
  x=a[j-1]+(n-j-1)-(a[n-1]-a[j])
  if(x<ans):
    ans=x
print(ans)

Submission status: Accepted

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

E_left = 0
W_left = 0
E_right = 0
W_right = 0

count_list = []

for i in range(N):
    if i == 0:
        E_right = S[i+1:].count('E')
        W_right = S[i+1:].count('W')
        count = E_right
        count_list.append(count)
    elif 1 <= i <= N-1:
        if S[i] == 'E':
            E_right -= 1
        if S[i] == 'W':
            W_right -= 1
        if S[i-1] == 'E':
            E_left += 1
        if S[i-1] == 'W':
            W_left += 1
        count = E_right + W_left
        count_list.append(count)


ans = min(count_list)
print(ans)
#print(count_list)
#print(E_right, E_left)
#print(W_right, W_left)

Submission status: Accepted

Solution code:
import os,re,sys,operator
from collections import Counter,deque
from operator import itemgetter
from itertools import accumulate,combinations,groupby
from sys import stdin,setrecursionlimit
from copy import deepcopy

setrecursionlimit(10**6)

n=int(stdin.readline().rstrip())
s=stdin.readline().rstrip()
cntw=[0]*(n+2);cnte=[0]*(n+2)
ans=10**6

for i in range(1,n+1):
    if s[i-1]=="W":
        cntw[i]=cntw[i-1]+1
        cnte[i]=cnte[i-1]
    elif s[i-1]=="E":
        cnte[i]=cnte[i-1]+1
        cntw[i]=cntw[i-1]
sumw,sume=cntw[n],cnte[n]
for i in range(n):
    now=cntw[i]+(sume-cnte[i+1])
    ans=min(ans,now)
print(ans)

Submission status: Accepted

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

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

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

ans = 10 ** 9
for i in range(N):
    ans = min(ans, W[i]+E[i]-1)
print(ans)
\

Submission status: Accepted

Solution code:
a = int(input())
b = input()
c = [0] * a
d = [0] * a
for i in range(a):
    if i == 0:
        if b[i] == "W":
            c[i] = 1
    else:
        if b[i] == "W":
            c[i] = c[i-1] + 1
        else:
            c[i] = c[i - 1]

    if i == 0:
        if b[-i-1] == "E":
            d[-i-1] = 1
    else:
        if b[-i-1] == "E":
            d[-i-1] = d[-i] + 1
        else:
            d[-i-1] = d[-i]
e = 10 ** 9
for i in range(a):
    e = min(c[i] + d[i] - 1, e)
print(e)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
num_e = [0]
num_w = [0]
ans = 999999999999

for i in range(n):
    num_w.append(num_w[i])
    if s[i] == "W":
        num_w[i + 1] += 1
    num_e.append(num_e[i])
    if s[i] == "E":
        num_e[i + 1] += 1

for i in range(n):
    now = num_w[i] + num_e[-1] - num_e[i + 1]
    if ans > now:
        ans = now

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E = S.count("E")
W = N - E

s = N
re = E
lw = 0
  

for i in S:
  if i == "E":
    re -= 1
    s = min(s, re + lw)
  else:
    s = min(s, re + lw)
    lw += 1
print(s)