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

west = [0]*N
east = [0]*N

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

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

ans = float('inf')
for i in range(N):
    ans = min(ans,west[i]+east[i])
print (ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = S.count("E")
c = a 
 
for s in S:
  if s=="E":
    c-=1
  else:
    c+=1
  a=min(a,c)
 
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
from itertools import accumulate #list(accumulate(A))

N = ii()
S = input()
cnt = [0] * N

for i in range(N):
    if S[i] == 'W':
        cnt[i] = 1

cnt = list(accumulate(cnt))
ans = N

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

print(ans)

Submission status: Accepted

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

e_all = S.count('E')

w_cnt = 0
e_cnt = 0
ans = float('inf')
for i in range(N):
    if S[i] == 'W':
        w_cnt += 1
        ans = min(ans, w_cnt - 1 + e_all - e_cnt)
    else:
        e_cnt += 1
        ans = min(ans, w_cnt + e_all - e_cnt)

print(ans)

Submission status: Accepted

Solution code:
import math

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

eS = [0] * n
wS = [0] * n

ec = 0
for i in range(n):
    eS[i] = ec
    if s[i] == "W":
        ec += 1

wc = 0
for i in reversed(range(n)):
    wS[i] = wc
    if s[i] == "E":
        wc += 1

ans = 10000000

for i in range(n):
    ans = min(ans, eS[i] + wS[i])

print(ans)

Submission status: Accepted

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


w_cnt = 0
e_cnt = 0
w_lst = []
e_lst = []

for s in S:
    if s == 'W':
        w_cnt += 1

    else:
        e_cnt += 1

    w_lst.append(w_cnt)
    e_lst.append(e_cnt)

ans = float('inf')
e_all = e_lst[-1]
for i in range(N):
    if S[i] == 'W':
        t = w_lst[i] - 1 + e_all - e_lst[i]
    else:
        t = w_lst[i] + e_all - e_lst[i]

    ans = min(ans, t)


print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    S = ns()
    E = np.cumsum([1 if s == 'E' else 0 for s in S][::-1])[::-1]
    W = np.cumsum([1 if s == 'W' else 0 for s in S])
    ans = E + W
    print(np.min(ans) - 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
left = s[0]
ans = 10**6
left = {}
left.setdefault('W',0)
left.setdefault('E',0)
right = {}
right.setdefault('E',0)
right.setdefault('W',0)
for i in range(n):
    right[s[i]] += 1
ans = 10**6
for i in range(n):
    right[s[i]] -= 1
    ans = min(ans,left['W']+right['E'])
    #print(left,right)
    left[s[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
directed = [S[1:].count("W")]

for i in range(1, len(S)):
  d = directed[i - 1]
  if S[i - 1] == "E":
    d += 1
  if S[i] == "W":
    d -= 1
  directed.append(d)

print(N - max(directed) - 1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ss = list(input())
    num_sum = [0]*n
    west_sum = 0
    east_sum = 0
    for i in range(1, n):
        if ss[i-1] == 'W':
            west_sum += 1
        num_sum[i] += west_sum

    for i in range(n-2, -1, -1):
        if ss[i+1] == 'E':
            east_sum += 1
        num_sum[i] += east_sum

    ans = min(num_sum)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()
e = int(s.count('E'))
ans = e

a = 0
for i in range(n):
    if s[i] == 'W':
        a += 1
    b = e - ((i+1)-a)
    ans = min(ans, a+b)


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = N

left_W = 0
right_E = S[1:].count("E")

for n in range(N):
  if n == 0:
    ans = min(ans,S[1:].count("E"))
        
  elif n == N-1 :
    ans = min(ans,S[:n].count("W"))
      
  else:
    if S[n] == "E":
      right_E -= 1
    if S[n-1] == "W":
      left_W += 1
    ans = min(ans,left_W + right_E)

print(ans)

Submission status: Accepted

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

left = [0] * n
right = [0] * n

tmp = 0
for i in range(1, n):
    if s[i - 1] == "W":
        tmp += 1
        left[i] = tmp
    else:
        left[i] = tmp

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

res = 10**9
for i in range(n):
    res = min(res, left[i] + right[i])
print(res)

Submission status: Accepted

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

def count(n,s):
    cntw=0
    cnte=0
    mcntw=0
    mcnte=0
    maxn=0
    for i in range(n):
        if s[i]=="W":
            cntw+=1
        else:
            cnte+=1
        if maxn<cnte-cntw:
            maxn=cnte-cntw
            mcnte=cnte
            mcntw=cntw

    if cntw==0 or cnte==0:
        print(0)
        return

    else:
        print(mcntw+cnte-mcnte)
        return
count(n,s)

Submission status: Accepted

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

w = [0]*n
e = [0]*n
if s[0] == "W": w[0] = 1
else: 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

ans = float("inf")
for i in range(n):
  t = 0
  if i != 0: t += w[i-1]
  if i != n-1: t += e[-1] - e[i]
  if t < ans: ans = t
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    S = readline().strip()

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

    ans = INF
    for i in range(N):
        if ans > west[i] + east[N] - east[i + 1]:
            ans = west[i] + east[N] - east[i + 1]

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

counts = []
for i in range(N):
    count = 0
    counts.append(sum_W[i]+sum_E[i])

print(min(counts))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
wcnt = [0] * n
ecnt = [0] * (n)
enum = 0
wnum = 0
for i in range(n):
    if s[i] == "W":
        wnum += 1
    wcnt[i] += wnum
    if s[-i - 1] == "E":
        enum += 1
    ecnt[-i - 1] += enum
ans = 10 ** 10
for i in range(n):
    temp = 0
    if i - 1 >= 0:
        temp += wcnt[i - 1]
    if i + 1 < n:
        temp += ecnt[i + 1]
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

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

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

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

#l==0
ans = cum_e_cnt[N-1] - cum_e_cnt[0]
for l in range(1, N):
    ans = min(ans, cum_w_cnt[l-1] + cum_e_cnt[N-1] - cum_e_cnt[l])
print(ans)

Submission status: Accepted

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

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

if s[0]=='E':
  ans=E-1
else:
  ans=E
  
ans1=[]
ans1.append(ans)
  
for x in range(1,n):
  if s[x]=='E':
    if s[x-1]=='E':
      ans-=1
      
  else:
    if s[x-1]=='W':
      ans+=1
  ans1.append(ans)
  
print(min(ans1))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
a=S.count("E")
c=a
for s in S:
  if s=="E":
    c-=1
  else:
    c+=1
  a=min(a,c)
print(a)

Submission status: Accepted

Solution code:
n = int(input())
s = [(i == "W")*1 for i in list(input())]
c = [0]*(n+1)
for i in range(n):
    c[i+1] = c[i] + s[i]
ans = float("inf")
for i in range(n):
    t = c[i] + (n-i-1-c[-1]+c[i+1])
    ans = min(ans,t)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=list(str(input()))
east=S[1:].count('E')
ans_min = east
ans = east
for l in range(1,N):
  if S[l-1]=='W':
    ans+=1
  if S[l]=='E':
    ans-=1
  if ans<=0:
    ans_min = 0
    break
  if ans < ans_min:
    ans_min = ans
print(ans_min)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = s.count('E')
w = s.count('W')
ans = 1000000
e_counter = 0
w_counter = 0
for i in range(len(s)):
    if s[i] == 'E':
        e_counter += 1
    else:
        w_counter += 1
    d = w_counter
    if s[i] == 'W':
        d -= 1
    ans1 = d + (e - e_counter)
    ans = min(ans1, ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(input())
wcnt,ecnt,ans,box = 0,0,0,[]
W = a.count("W")
E = a.count("E")
for i in range(n-1):
  if a[i] == "W":
    box.append(E+wcnt)
    wcnt += 1
    W -= 1
  else:
    box.append(E+wcnt)
    ecnt += 1
    E -= 1
if a[n-1] == "W":
  W -= 1
  box.append(E+wcnt)
  wcnt += 1
else:
  E -= 1
  box.append(E+wcnt)
  ecnt += 1
print(min(box))

Submission status: Accepted

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

    wnum = s.count("W")
    enum = s.count('E')

    first = enum
    ans = first

    for i in range(n):
        if s[i] == "E":
            first -= 1
        if i >= 1:
            if s[i-1] == "W":
                first += 1
        if ans > first:
            ans = first
        #print(i, ans, first)
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

left=deque([0])
right=[0]
for i in range(N):
  if S[i]=='W':plus=1
  else:plus=0
  left.append(left[-1]+plus)
  if S[-i-1]=='E':plus=1
  else:plus=0
  right.append(right[-1]+plus)
  
left.popleft()
right = right[::-1]
right.pop()

print(min([left[i]+right[i] for i in range(N)])-1)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
W_cnt=[0]
E_cnt=[0]

for ss in s:
  w=W_cnt[-1]
  if ss=="W":
    w+=1
  W_cnt.append(w)

for ss in reversed(s):
  e=E_cnt[-1]
  if ss=="E":
    e+=1
  E_cnt.append(e)
E_cnt=E_cnt[::-1]

minc=float("inf")
for i in range(n):
  c=(W_cnt[i] + E_cnt[i+1])
  minc=min(minc,c)
print(minc)

Submission status: Accepted

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

for i in range(n):
    j=0
    if s[i]=="E":
        j=1
    else:
        j=-1
    if i==0:
        v[i]+=j
    else:
        v[i]+=j+v[i-1]

sum=-2
w=0
res=""
for i in range(n):
    if sum<v[i]:
        sum=v[i]
        wmax=w
        emax=i-w
        res=s[i]
    if s[i]=="W":
        w+=1

if res=="W":
    print(wmax+s.count("E")-emax)
else:
    print(wmax+s.count("E")-emax-1)

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect
n=int(input())
s=list(input())
ans=n
r=[0]*n
l=[0]*n
for i in range(1,n):
    if s[i-1]=="W":
        r[i]=r[i-1]+1
    else:
        r[i]=r[i-1]
    if s[n-i]=="E":
        l[n-1-i]=l[n-i]+1
    else:
        l[n-1-i]=l[n-i]
for i in range(n):
    ans=min(ans,l[i]+r[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
sE = s.count('E')
sW = s.count('W')
l = []
e, w = 0, 0
for ch in s:
    if ch == 'E':
        e += 1
    else:
        w += 1
    m = (sE-e)+w
    l.append(sE-e+w-1 if ch == 'W' else sE-e+w)
print(min(l))

Submission status: Accepted

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

from collections import Counter
count = Counter(s[1:])
x = count["E"]

temp =x
for i in range(1,n):
    if s[i-1] =="W":
        x +=1
    if s[i] =="E":
        x -=1
    temp = min(temp,x)

print(temp)

Submission status: Accepted

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

ans=s[1:].count('E')
pin=ans

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

es = [0] * (N + 1)
ws = [0] * (N + 1)

for i, c in enumerate(S):
    if c == 'E':
        es[i + 1] = es[i] + 1
        ws[i + 1] = ws[i]
    elif c == 'W':
        ws[i + 1] = ws[i] + 1
        es[i + 1] = es[i]
    
ans = INF
for i in range(N):
    cnt = ws[i] + (es[N] - es[i + 1])
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

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

count = s.count('E')
ans = s.count('E')
for i in range(len(s)):
    if s[i] == 'E':
        count -=1
    else:
        count+=1
    ans = min(count,ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
def main():
    n = int(input())
    s = list(input())
    enum = [1 if c == 'E' else 0 for c in s]
    wnum = [1 if c == 'W' else 0 for c in s]
    enuma = tuple(accumulate(enum))
    wnuma = tuple(accumulate(wnum))
    wnumm = wnuma[-1]
    wnuma2 = [wnumm - wn for wn in wnuma]
    num = max(wnuma2[0], enuma[-2])
    for i1 in range(1, n - 1):
        num = max(num, enuma[i1 - 1] + wnuma2[i1])
    r = n - num - 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = S.count("E")
c = a 

for s in S:
  if s=="E":
    c-=1
  else:
    c+=1
  a=min(a,c)

print(a)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
l = 0
r = S[1:N].count('E')
ans = l+r

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

print(ans)

Submission status: Accepted

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

ans = S.count('E')
if S[0] == 'E':
    ans -= 1

arr =[]
arr.append(ans)

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

print(min(arr))

Submission status: Accepted

Solution code:
NN = int(input())
SS = str(input())

score = [0]*NN
for i in range(1,NN):
    if SS[i] == "E":
        score[0] += 1

for pos in range(1,NN-1):
    score[pos] = score[pos-1]
    if SS[pos-1] == "W":
        score[pos] += 1
    if SS[pos] == "E":
        score[pos] -= 1

score[NN-1] = score[NN-2]
if SS[NN-2] == "W":
    score[NN-1] += 1
if SS[NN-1] == "E":
    score[NN-1] -= 1

print(min(score))

Submission status: Accepted

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

l = 0
r = sn['E']

ans = r
for i in range(n):
    if s[i] == 'W':
        l += 1
    else:
        r -= 1
    ans = min(ans, l+r)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
oE = S.count("E")
Wo = 0

L = []
for i in range(N):
    if i == 0:
        if S[i] == "E":
            oE -= 1
    else:
        if S[i-1] == "W":
            Wo += 1
        if S[i] == "E":
            oE -= 1
    L.append(Wo + oE)
    if Wo + oE == 0:
        break
print(min(L))

Submission status: Accepted

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

EP = [0]*(N+1)
WP = [0]*(N+1)
CP = [0]*N
for T in range(0,N):
    EP[N-T-1] = EP[N-T]+(S[N-1-T]=='E')
    WP[T+1] = WP[T]+(S[T]=='W')
for T in range(0,N):
    CP[T] = EP[T+1]+WP[T]
print(min(CP))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = input()

lww = 0
lee = s[1:].count("E")
ans = lww + lee

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

    ans = min(ans, lee + lww)
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
West = [0]*(N+1)
East = [0]*(N+1)
for i in range(N):
    if S[i]=="W":
        West[i+1]=West[i]+1
    else:
        West[i+1]=West[i]
for i in range(N):
    if S[i]=="E":
        East[i+1]=East[i]+1
    else:
        East[i+1]=East[i]
ans = float("inf")
for i in range(N+1):
    tmp = West[i]+(East[N]-East[i])
    if tmp<ans: ans = tmp
print(ans)

Submission status: Accepted

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

W_count = [0]*(N)#左から累積を数えていく
E_count = [0]*(N)#右からｒｙ

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

for i in range(N-2,-1,-1):
    E_count[i]=E_count[i+1]
    if S[i+1]=="E":
        E_count[i]+=1

ans = [0]*(N)
for i in range(0,N,1):
    ans[i]=W_count[i]+E_count[i]

print(min(ans))

Submission status: Accepted

Solution code:
# coding: utf-8

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

# ruisekiwa ba-jon

ruiseki_w = [0] * (N + 1)
ruiseki_e = [0] * (N + 1)

for i in range(0, len(S)):
    ruiseki_w[i+1] = ruiseki_w[i] + S[i].count("W")
    ruiseki_e[i+1] = ruiseki_e[i] + S[i].count("E")

res = 10**10
for i in range(0, len(S)):
    hidari_no_w_no_kazu = ruiseki_w[i]
    migi_no_e_no_kazu = ruiseki_e[-1] - ruiseki_e[i+1]
    res = min(hidari_no_w_no_kazu + migi_no_e_no_kazu, res)

print(res)

Submission status: Accepted

Solution code:
# coding: utf-8

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

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


res = 1000000
for i in range(len(S)):
    if S[i] == "E":
        east -= 1
    res = min(res, east + west)
    if S[i] == "W":
        west += 1

print(res)

Submission status: Accepted

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

E = [0] * n
W = [0] * n

for i in range(0, n):
    if i < n - 1:
        if S[i] == "W":
            E[i+1] = E[i] + 1
        else:
            E[i+1] = E[i]
        if S[n-i-1] == "E":
            W[n-i-2] = W[n-i-1] + 1
        else:
            W[n-i-2] = W[n-i-1]
min_so_far = n
for i in range(n):
    c = E[i]+ W[i]
    if min_so_far > c:
        min_so_far = c
print (min_so_far)

Submission status: Accepted

Solution code:
ans=float("Inf")
N=int(input())
l=list(input())
W=0
E=l.count("E")
for i in range(N):
   if l[i]=="W":
      ans=min(ans,W+E)
      W+=1
   else:
      E-=1
      ans=min(ans,W+E)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
data = []
data.append((0, 0))
for i in range(N):
    # looking left
    if S[i] == 'W':
        data.append((data[i][0]+1, data[i][1]))
    # looking right
    elif S[i] == 'E':
        data.append((data[i][0], data[i][1]+1))
#print(data)

answer = N-1
for i in range(N):
    count = 0
    count += data[N][1] - data[i+1][1]
    count += data[i][0]
    if answer > count:
        answer = count

print(answer)

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
  if i != n-1:
    w[i+1] = w[i]
    e[i+1] = e[i]

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
direction = [{"E": 0, "W": 0} for _ in range(N + 1)]  # direcion[i]["E"] ... i番目までにEを向いている人数
ans = N
for i in range(N):
    direction[i + 1]["E"] = direction[i]["E"] + (1 if S[i] == "E" else 0)
    direction[i + 1]["W"] = direction[i]["W"] + (1 if S[i] == "W" else 0)
for i in range(N):
    ans = min(ans, direction[i]["W"] + direction[N]["E"] - direction[i + 1]["E"])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iomanip>
int main()
{
    long N;
    std::cin >> N;
    std::string S;
    std::cin >> S;
    long count=0;
    std::vector<long> counters;
    for(long i=1; i<N; i++){
        if(S[i]=='E'){
            count++;
        }
    }
    counters.push_back(count);
    for(long i=1; i<N; i++){
        if(S[i]=='E'){
            count-=1;
        }
        if(S[i-1]=='W'){
            count++;
        }
        counters.push_back(count);
    }
    std::sort(counters.begin(),counters.end());
    std::cout << counters[0] << std::endl;
    return 0;
}
"""
n=int(input())
s=input()
cnt=0
for i in range(1,n):
  if s[i]=="E":cnt+=1
ans=[]
ans.append(cnt)
for i in range(1,n):
  if s[i]=="E":
    cnt-=1
  if s[i-1]=="W":
    cnt+=1
  ans.append(cnt)
print(min(ans))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
e=[int("E"==i) for i in s]
for i in range(1,n):
  e[i]+=e[i-1]

  
ans=[]
for j in range(n):
  left=j-0
  right=n-j-1
  
# leftにWは何個？
# leftのEは、
  if j-1>=0:
    etmp=e[j-1]
  else:
    etmp=0
  leftw=left-etmp
# rightにEは何個？
# rightのEは、
  eee=e[n-1]-e[j]
  ans+=(leftw+eee),
print(min(ans))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
from itertools import accumulate
def main():
    n = int(input())
    s = list(input())

    if n == 2:
        if s[0] == 'E' or s[1] == 'W':
            print(0)
            sys.exit()
        else:
            print(1)
            sys.exit()
    e = [0] * n
    w = [0] * n
    for i1 in range(n):
        e[i1] = 1 if s[i1] == 'E' else 0
        w[i1] = 1 if s[i1] == 'W' else 0
    e.insert(0, 0)
    e.append(0)
    w.insert(0, 0)
    w.append(0)

    ea = list(accumulate(e))
    wa = list(accumulate(w))
    rt = n
    for j1 in range(1, n+1):
        rt = min(rt, wa[j1 - 1] + (ea[-1] - ea[j1]))
    print(rt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# C - Attention

N = int(input())
S = list(str(input()))
e = 0
ecum = [0] * (N+1)
wcum = [0] * (N+1)
for i, s in enumerate(S):
    if s == 'E':
        e += 1
        ecum[i+1] = ecum[i] + 1
        wcum[i+1] = wcum[i]
    else:
        ecum[i+1] = ecum[i]
        wcum[i+1] = wcum[i] + 1

ans = 10**18
for i in range(1, N+1):
    # 自分より左にいる西を向いてる人＋右にいる東を向いてる人
    cost = wcum[i-1] + (e - ecum[i])
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

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

E_l=[0]*N
for n in range(1,N):
    E_l[n]=E_l[n-1]
    if A[n-1] == 'W':
        E_l[n]+=1
    
W_r=[0]*N
for n in range(N-2,-1,-1):
    W_r[n] = W_r[n+1]
    if A[n+1] == 'E':
        W_r[n]+=1

ans=10**20
for n in range(N):
    if ans > E_l[n]+W_r[n]:
        ans=E_l[n]+W_r[n]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = input()
w = [A[0] == "W"]
for i in range(1, N):  w.append(w[-1] + (A[i] == "W"))
e = [A[0] == "E"]
for i in range(1, N):  e.append(e[-1] + (A[i] == "E"))
ans = min(e[-1] - e[0], w[-2])
for i in range(1, N - 1):  ans = min(ans, w[i - 1] + e[-1] - e[i])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

num_eas = s.count('E')


res = []
res += [num_eas - s[0].count("E")]

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

Submission status: Accepted

Solution code:
N = int(input())
S =list(input())
ans = N
W = 0
E = S.count('E')

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

Submission status: Accepted

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


def main():
  N = int(input())
  A = input()[:-1]

  E = [0]
  for a in A:
    if a == 'E':
      E.append(E[-1]+1)
    else:
      E.append(E[-1])

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


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=s.count("W")
e=s.count("E")
t=0
ans=n
for i in range(n):
    if s[i]=="E":
        e-=1
    tmp=e+t
    if tmp<ans:
        ans=tmp
    if s[i]=="W":
        t+=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
lst=list(input())
#i=0
ans_lst=[lst[1:].count("E")]

for i in range(1,n):
    temp_ans=ans_lst[i-1]
    if lst[i]=="E":
        temp_ans-=1
    if lst[i-1]=="W":
        temp_ans+=1
    ans_lst.append(temp_ans)

print(min(ans_lst))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
eastNumber = S.count('E')
westNumber = 0
minimum = N
 
for i in range(N):
    if S[i] == 'E':
        eastNumber -= 1
        number = eastNumber + westNumber
        if minimum > number:
            minimum = number
    else:
        number = eastNumber + westNumber
        if minimum > number:
            minimum = number
        westNumber += 1
 
print(minimum)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
LWW = 0
LWE = 0
LEW = s[1:].count('W')
LEE = s[1:].count('E')
ans = LWW + LEE
for i in range(1,len(s)):
    if s[i] =="W":
        LEW -=1
    else:
        LEE -=1
    if s[i-1] == 'W':
        LWW +=1
    else:
        LWE +=1
    ans = min(ans,LWW+LEE)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

eastNumber = S.count('E')
westNumber = 0
minimum = N

for i in range(N):
    if S[i] == 'E':
        eastNumber -= 1
        number = eastNumber + westNumber
        if minimum > number:
            minimum = number
    else:
        number = eastNumber + westNumber
        if minimum > number:
            minimum = number
        westNumber += 1

print(minimum)