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 sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    n = int(stdin.readline().rstrip())
    s = str(stdin.readline().rstrip())
    #n,m = map(int, stdin.readline().rstrip().split())
    #l = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    ans = n
    dp = [0 for j in range(n+1)]
    for i in range(n):
        if s[i] == "W":
            dp[i+1] = dp[i] + 1
        else:
            dp[i+1] = dp[i]
    for i in range(n):
        ans = min (ans, dp[i] + ( (n-i-1) - (dp[n] - dp[i+1]) ))
    print(ans)


main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
import collections
c = collections.Counter(s)
e = c["E"]
w = 0
ans = float("Inf")
for i in range(n):
    if s[i] == "E":
        ans = min(ans,e+w-1)
    else:
        ans = min(ans,e+w)
    if s[i] == "W":
        w += 1
    else:
        w -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
EE = S.count("E")
WE = S.count("W")
EW = 0
WW = 0
ans = N

for i in range(N):
  if S[i] == "W":
    ans = min(ans,EE+WW)
    WE -= 1
    WW += 1
  else:
    ans = min(ans,EE+WW-1)
    EE -= 1
    WE += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count = S.count('E')

if S[0] == 'E':
	count-=1

countmin = count

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

print(countmin)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
li = []
a = s[1:].count("E")
li.append(a)

for i in range(n-1):
    if s[i] == "E" and s[i+1] == "E":
        a -= 1
        li.append(a)
    elif s[i] == "W" and s[i+1] == "W":
        a += 1
        li.append(a)
print(min(li))

Submission status: Accepted

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

tmp = 0

for s in S[1:]:
  if s == 'E':
    tmp += 1

rlt = tmp

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

Submission status: Accepted

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

ans = [0]*n
temp = 0
for i in range(1,n):
    if S[i-1] == "W":
        temp += 1
    ans[i] += temp
    
S.reverse()
ans.reverse()

temp = 0
for i in range(1,n):
    if S[i-1] == "E":
        temp += 1
    ans[i] += temp

ans.reverse()
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())

s = input()
l = [1 if i=="W" else 0 for i in s]
ac = [0]*(N+1)

for i in range(1, N+1):
    ac[i] = ac[i-1]+l[i-1]
ans = N
for i in range(1, N+1):
    lw = ac[i-1]
    le = i-1-lw
    rw = ac[N]-ac[i]
    re = N-i-rw
    ans = min(ans, lw+re)

print(ans)

Submission status: Accepted

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

w_left = 0
e_right = S[1:].count("E")
ans = float('inf')
for i in range(N):
    if i != 0:
        if S[i] == "E":
            e_right -= 1
        if S[i-1] == "W":
            w_left += 1
    ans = min(ans, w_left + e_right)
print(ans)

Submission status: Accepted

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

from collections import deque
w=[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]
e=[0]*n

for j in range(n-2,-1,-1):
    if s[j+1]=='E':
        e[j]=e[j+1]+1
    else:
        e[j]=e[j+1]

ans=10**9

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

print(ans)

Submission status: Accepted

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

m = S[1:].count("E")
a = [m]

for i in range(N- 1):
  if(S[i] == "E" and S[i+ 1] == "E"):
    m-= 1
    a.append(m)
  if(S[i] == "W" and S[i+ 1] == "W"):
    m+= 1
    a.append(m)
    
print(min(a))

Submission status: Accepted

Solution code:
def main():
  _, s = open(0).read().split()
  w, res = 0, 0
  for i in s:
    if i == 'W':
      w += 1
    elif w:
      w -= 1
      res += 1
    else:
      pass
  return res


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

Submission status: Accepted

Solution code:
_, s = open(0).read().split()
left, right = [0], []
lw, re = 0, 0
for i in s:
  if i == 'W':
    lw += 1
  left += [lw]
for i in s[::-1]:
  if i == 'E':
    re += 1
  right += [re]
print(min(i + j for i, j in zip(left[:-1], right[-2::-1] + [0])))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
s = input()
e_cnt_list = [0 for _ in range(n)]
w_cnt_list = [0 for _ in range(n)]
e_cnt = 0
w_cnt = 0
for i in range(1, n):
    if s[i-1] == 'W':
        w_cnt += 1
    w_cnt_list[i] = w_cnt
    if s[-i] == 'E':
        e_cnt += 1
    e_cnt_list[-i-1] = e_cnt
print(np.min(np.array(w_cnt_list) + np.array(e_cnt_list)))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    r = []
    wCount = 0
    eCount = s.count("E")
    for v in s:
        if v == "W":
            wCount += 1
        r.append(wCount)
        r[-1] += eCount
        if v == "E":
            eCount -= 1
    #print(r)
    print(min(r)-1)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
w = []
e = []
x = 0
y = 0
ans = 1000000

#西と東の累積和を取る
for c in s:
    if c == "W":
        x += 1
    else:
        y += 1
    w.append(x)
    e.append(y)

for i in range(n):
    if i == 0:
        z = e[n-1] - e[i]
    elif i == n-1:
        z = w[i-1]
    else:
        z = w[i-1] + e[n-1] - e[i]
    if z < ans:
        ans = z

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

RE = S.count("E")
RW = N - RE
LE = 0
LW = 0

def f(x):
  global RE, RW
  if x == "E":
    RE -= 1
  else:
    RW -= 1

def g(x):
  global LE, LW
  if x == "E":
    LE += 1
  else:
    LW += 1

for i in S:
  f(i)
  ans = min(ans, LW+RE)
  g(i)

print(ans)

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
# @lru_cache(None)






n=l()[0]
s=input()

A=[0 for i in range(n+1)]
B=[0 for i in range(n+1)]
for i in range(n):
    if(s[i]=="W"):
        A[i+1]=A[i]+1
    else:
        A[i+1]=A[i]
for i in range(n-1,-1,-1):
    B[i]=B[i+1]+(s[i]=="E")
ans=min(A[i]+B[i] for i in range(n+1))
print(ans)

Submission status: Accepted

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

E=[0]*n
W=[0]*n
if s[-1]=="E": E[-1]=1
if s[0]=="W" : W[0]=1


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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    n_E = S.count("E")

    ans = N
    n_W = 0
    for i in range(N):
        if S[i] == "E":
            n_E -= 1
        num = n_E + n_W
        if num < ans:
            ans = num
        if S[i] == "W":
            n_W += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

sumW=s[1:].count("W")
sumE=s[1:].count("E")

p=[]
e=sumE
w=0
p.append(e)
for i in range(n-1):
    
    if s[i]=="W":
        w+=1
    if s[i+1]=="E":
        e-=1
    p.append(w+e)
    
print(min(p))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E = [0]*N
W = [0]*N
if S[0] == 'E':
    E[0] = 1
else:
    W[0] = 1
for i in range(1,len(S)):
    if S[i] == 'E':
        E[i] = E[i-1]+1
        W[i] = W[i-1]
    else:
        W[i] = W[i-1]+1
        E[i] = E[i-1]
MIN = float('inf')
for i in range(N):
    if i == 0:
        MIN = min(MIN,E[N-1]-E[0])
    elif i == N-1:
        MIN = min(MIN,W[N-2])
    else:
        MIN = min(MIN,W[i-1]+E[N-1]-E[i])
print(MIN)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
tmpMin = len(S.replace('W',''))
tmpIdx = -1
tmp = tmpMin
for i in range(N):
    if S[i] == 'E':
        tmp -= 1
    else:
        tmp += 1
    if tmp < tmpMin:
        tmpMin = tmp
        tmpIdx = i

print(tmpMin)

Submission status: Accepted

Solution code:
def answer(s):
    dic = {
        'left' : { 'W': 0, 'E': 0 },
        'right' : { 'W': 0, 'E': 0 } 
    }
    for i in range(1, len(s)):
        dic['right'][s[i]] += 1
    leader = s[0]
    _min = dic['right']['E']
    for i in range(1, len(s)):
        leader = s[i]
        dic['left'][s[i-1]] += 1
        dic['right'][leader] -= 1
        _min = min(_min, dic['left']['W']+dic['right']['E'])
    return _min

n = int(input())
s = input()
print(answer(s))

Submission status: Accepted

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


W = [[] for _ in range(N)]
E = [[] for _ in range(N)]

prev_w = 0
prev_e = 0
for i, s in enumerate(S):
    if s == 'W':
        W[i] = prev_w + 1
    else:
        W[i] = prev_w
    prev_w = W[i]

for i, s in enumerate(S[::-1]):
    if s == 'E':
        E[-i] = prev_e + 1
    else:
        E[-i] = prev_e
    prev_e = E[-i]
        
ans = len(S)
for i in range(N):
    if i == 0:
        cost = E[i+1]
    elif i == N-1:
        cost = W[i-1]
    else:
        cost = W[i-1] + E[i+1]
    ans = min([cost, ans])
print(ans)

Submission status: Accepted

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

    rE = S.count('E')  # リーダーより右にいる人の数
    rW = N - rE

    lE = 0
    lW = 0

    c = N

    for s in S:
        if s == 'E':
            rE -= 1
            f = 1
        else:
            rW -= 1
            f = -1

        candidate = rE + lW
        # print(candidate)

        if candidate < c:
            c = candidate

        if f == 1:
            lE += 1
        else:
            lW += 1

    print(c)


solver()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

mini=10**8
for i in range(n):
    mini=min(mini,E[i]+W[i])

print(mini)

Submission status: Accepted

Solution code:

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

w_num = S.count('W')
e_num = S.count('E')

rev_num = S[1:].count('E')
rev_num_list = [rev_num]

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

print(min(rev_num_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
nsums=[0]*N

right =0
for s in S:
  if s=='E':right+=1

left =0
min_sum=N
for i in range(N):
  if S[i] == 'E':right-=1
  nsums[i] = right + left
  if S[i]=='W':left+=1

  min_sum = min(min_sum,nsums[i])
print(min_sum)

Submission status: Accepted

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

Submission status: Accepted

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

sum_w = [0]
sum_e = [0]

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

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

#print(len(sum_e), len(sum_w))
sumu = [x+y for x, y in zip(sum_w, sum_e)]

print(min(sumu))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
s = list(map(str, input().rstrip()))

dp_e = np.array([0] + [int(x == "E") for x in s])
dp_e = (dp_e.sum() - dp_e.cumsum())[1:]
dp_s = np.array([0] + [int(x == "W") for x in s])
dp_s = dp_s.cumsum()[:-1]

ans = (dp_e + dp_s).min()

print(ans)

Submission status: Accepted

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

R = len( S.replace("W", "") ) 
L = 0
ret = L + R 
for i in range( N ):
  if S[ i ] == "E":
    R -= 1
  ret = min( ret, L + R )
  if S[ i ] == "W":
    L += 1

print( ret )

Submission status: Accepted

Solution code:
input();s=input();m=c=s.count("E")
for t in s:c-=t<"W";m=min(m,c);c+=t>"E"
print(m)

Submission status: Accepted

Solution code:
from itertools import*;input();s=input();print(min(w+e for w,e in zip(*[([0]+list(accumulate(map(int,s[::a].translate(str.maketrans("EW","01"[::a])))))[:-1])[::a]for a in[-1,1]])))

Submission status: Accepted

Solution code:
from itertools import*
input();s = input()
b, c = [([0]+list(accumulate(map(int, s[::a].translate(str.maketrans("EW","01"[::a])))))[:-1])[::a] for a in [-1,1]]
print(min(x+y for x, y in zip(b, c)))

Submission status: Accepted

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

left_E, left_W = 0, 0
right_E, right_W = list_S.count("E"), list_S.count("W")
ans = pow(10, 18)

for i in range(n):
    if list_S[i] == "E":
        right_E -= 1
        ans = min(ans, right_E + left_W)
        left_E += 1
    elif list_S[i] == "W":
        right_W -= 1
        ans = min(ans, right_E + left_W)
        left_W += 1

print(ans)

Submission status: Accepted

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

west_count = 0
east_count = 0
ans = 10**18
adj = 0

for i in s:
    if i == 'W':
        west_count += 1
    else:
        east_count += 1

for i in range(n):
    tmp = s[i]
    if tmp == 'W':
        west_count -= 1
    else:
        east_count -= 1

    if ans > east_count + adj:
        ans = east_count + adj

    if tmp == 'W':
        adj += 1

print(ans)

Submission status: Accepted

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

E, W = [], []
e, w = 0, 0
for s in S:
    if s == 'E':
        e += 1
    else:
        w += 1
    E.append(e)
    W.append(w)

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

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
W = [0]
for i in range(n):
    if s[i] == "W":
        W.append(W[-1]+1)
    else:
        W.append(W[-1])

ans = n

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

Submission status: Accepted

Solution code:
def myAnswer(N:int,S:str) -> int:
   rightE = S.count("E")
   leftW = 0
   ans = 10**10
   for s in S:
      if(s == "W"):
         ans = min(ans,rightE + leftW)
         leftW += 1
      else:
         rightE -= 1
         ans = min(ans,rightE + leftW)
   return ans

def modelAnswer():
   return
def main():
   N = int(input())
   S = (input())
   print(myAnswer(N,S))
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
def myAnswer(N:int,S:list) -> int:
   rightW = S.count("W")
   rightE = N - rightW
   leftW = 0
   leftE = 0
   ans = 10**10
   for s in S:
      if(s == "W"):
         rightW -= 1
         ans = min(ans,rightE + leftW)
         leftW += 1
      else:
         rightE -= 1
         ans = min(ans,rightE + leftW)
         leftE += 1
   return ans

def modelAnswer():
   return
def main():
   N = int(input())
   S = list(input())
   print(myAnswer(N,S))
if __name__ == '__main__':
   main()

Submission status: Accepted

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

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

P = [0 for i in range(N)]

w_c = S.count('W')
e_c = S.count('E')
if S[0] == 'W':
    P[0] = {'W_l':0, 'W_r':w_c-1, 'E_l':0, 'E_r':e_c}
else:
    P[0] = {'W_l':0, 'W_r':w_c, 'E_l':0, 'E_r':e_c-1}


for i in range(1, N):
    b = P[i-1]
    if S[i] == 'W':
        if S[i-1] == 'W':
            P[i] = {'W_l':b['W_l']+1, 'W_r':b['W_r']-1, 'E_l':b['E_l'], 'E_r':b['E_r']}
        else:
            P[i] = {'W_l':b['W_l'], 'W_r':b['W_r']-1, 'E_l':b['E_l']+1, 'E_r':b['E_r']}

    else: # S[i] == 'E'
        if S[i-1] == 'W':
            P[i] = {'W_l':b['W_l']+1, 'W_r':b['W_r'], 'E_l':b['E_l'], 'E_r':b['E_r']-1}
        else:
            P[i] = {'W_l':b['W_l'], 'W_r':b['W_r'], 'E_l':b['E_l']+1, 'E_r':b['E_r']-1}


ans = 10**9
for i in range(N):
    ans = min(P[i]['W_l']+P[i]['E_r'], ans)
print(ans)

Submission status: Accepted

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

t = [0] * n
for i in range(n):
    if s[i] == 'E':
        t[i] = 1
    if i == 0:
        continue
    t[i] += t[i-1]

ans = 10000000000

for i in range(n):
    if i == 0:
        a = 0
    else:
        a = i - t[i-1]
    b = t[n-1] - t[i]
    c = a + b

    ans = min(ans, c)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
l = [0] * n
r = [0] * n
for i in range(1, n):
    if s[i-1] == 'W':
        l[i] = l[i-1] + 1
    else:
        l[i] = l[i-1]

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

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    S = rl().rstrip()
    
    accw, acce = [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        accw[i + 1] = accw[i] + (S[i] == 'W')
        acce[i + 1] = acce[i] + (S[i] == 'E')
    
    ans = 10 ** 6
    for i in range(N):
        ans = min(ans, acce[-1] - acce[i + 1] + accw[i])
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

result=10**6
hantei=0

num_E=S.count('E')
num_W=S.count('W')

pr_E=0
pr_W=0

for i in range(N):
    if i==0:
        hantei=num_E-(S[i]=='E')*1
        pr_E+=(S[i]=='E')*1
        pr_W+=(S[i]=='W')*1
    elif i==N-1:
        hantei=num_W-(S[i]=='W')*1
    else:
        hantei=pr_W+num_E-(pr_E+(S[i]=='E')*1)
        pr_E+=(S[i]=='E')*1
        pr_W+=(S[i]=='W')*1
    
    result=int(min(result,hantei))

print(result)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
c = S.count("E") - S.startswith("E")
ans = c
for i in range(N-1):#リーダーをsの右のヤツに変える
    s = S[i]
    if i<N-1 and S[i+1] == "E":
        c -= 1
    if s == "W":
        c += 1
    ans = min(c,ans)
print(ans)

Submission status: Accepted

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

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

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

ans=float('inf')
for i in range(n):
    ans=min(ans,W[i]+E[i])

print(ans)

Submission status: Accepted

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

l = [0]*n
l[0] = s[1:].count("E")

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

print(min(l))

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 = str(input())

line = [0]*(n+1)

for i in range(n):
    if s[i] == 'W':
        line[0] += 1
        line[i+1] -= 1
    if s[i] == 'E':
        line[i] += 1
        line[n] -= 1

# print(l)

for i in range(n-1):
    line[i+1] += line[i]

# print(line)
print(len(s)-max(line))

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = cnt = s.count("E")
for i in range(n):
    cnt -= s[i] == 'E'
    ans = min(ans, cnt)
    cnt += s[i] == 'W'
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
string = input()
es = [0]
ws = [0]
for s in string:
    if s == 'E':
        es.append(es[-1]+1)
        ws.append(ws[-1])
    else:
        es.append(es[-1])
        ws.append(ws[-1]+1)
ans = float('inf')
for i in range(n):
    i += 1
    score = ws[i-1] + es[-1] - es[i]
    ans = min(score, ans)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
w = [0 for _ in range(n+1)]
e = [0 for _ in range(n+1)]

for i in range(n):
    if s[i] == "W":
        w[i+1] = w[i]+1
        e[i+1] = e[i]
    else:
        e[i+1] = e[i]+1
        w[i+1] = w[i]
# print(w)
# print(e)
ans = float('inf')
for i in range(n):
    a = w[i+1]
    b = e[n] - e[i+1]
    if s[i] == 'W':
        a-=1

    ans = min(ans,a+b)
    # print(ans,a,b)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a=[0]
for i in range(n):
	a.append(a[i]+(s[i]=='W'))
p=n
for i in range(n):
	j=a[i]
	k=n-i-(a[-1]-a[i+1])-1
	#print(i,j,k)
	p=min(p,j+k)
print(p)

Submission status: Accepted

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

E_count = [0] * (N+1)
W_count = [0] * (N+1)

for i in range(N):
    E_count[i + 1] = E_count[i] + int(S[i] == "E")
    W_count[i + 1] = (i + 1) - E_count[i + 1]

ans = 10 ** 6
for i in range(1, N + 1):
    change_dir_cnt = (W_count[i - 1] - W_count[0]) + (E_count[-1] - E_count[i])
    ans = min(change_dir_cnt, ans)
print(ans)

Submission status: Accepted

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

E_count = [0] * (N+1)
for i in range(N):
    E_count[i+1] = E_count[i] + int(S[i] == "E")

W_count = [0] * (N+1)
for i in range(N):
    W_count[i+1] = W_count[i] + int(S[i] == "W")

ans = 10 ** 6
for i in range(1, N + 1):
    change_dir_cnt = (W_count[i - 1] - W_count[0]) + (E_count[-1] - E_count[i])
    ans = min(change_dir_cnt, ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    S = readline().decode('utf-8')
    L = len(S)
    A = [0]*L
    for i in range(L):
        if S[i] == 'E':
            if i == 0:
                A[i] = 1
            else:
                A[i]=A[i-1]+1
        else:
            if i == 0:
                continue
            else:
                A[i]=A[i-1]
    ans = INF
    for i in range(N):
        tmp = 0
        if i>0:
            tmp += i - A[i-1]
        if i<N-1:
            tmp += A[N-1]-A[i]
        ans = min(ans,tmp)
    print(ans)






if __name__ == '__main__':
    main()

Submission status: Accepted

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

w_l=[0]
s_l=[0]
for c in s:
    if c=='W':
        w_l.append(w_l[-1]+1)
        s_l.append(s_l[-1])
    else:
        w_l.append(w_l[-1])
        s_l.append(s_l[-1]+1)
ans=1145141919
for i in range(n):
    ans=min(ans, w_l[i]+s_l[-1]-s_l[i+1])
print(ans)

Submission status: Accepted

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

# 案の定TLE
# ans = N+1
# for i in range(N):
#     ans = min(ans,S[:i].count("W") + S[i+1:].count("E"))

left = 0
right = S.count("E")
ans = N+1
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:
n = int(input())
s = input()
e = s[1:].count("E")
w = 0
ans = e
for i in range(1,n):
    if s[i-1] == "W":
        w += 1
    if s[i] == "E":
        e -= 1
    ans = min(ans,w+e)
    
print(ans)

Submission status: Accepted

Solution code:
a = int(input())
ar = input()
n = ar.count("E")
if ar[0] == "E":
    n -= 1
mi = n
for i in range(1,a):
    if ar[i] == "E":
        n -= 1
    if ar[i-1] == "W":
        n += 1
    if n < mi:
        mi = n
print(mi)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions

import copy

# import heapq
# from collections import deque
# import decimal

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    s = list(input())

    w_in_left_side_of_me = [0 for _ in range(n)]
    e_in_right_side_of_me = [0 for _ in range(n)]

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

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

    ans = sys.maxsize
    for i in range(n):
        ans = min(ans, w_in_left_side_of_me[i] + e_in_right_side_of_me[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l=[0]*n
east=0#右を向いてる人の数
for i in range(n):
    if s[i]=='E':
        east+=1
    l[i]=east
#print(l)
ans=3*10**5
for i in range(n-1,-1,-1):
    if i==n-1:
        R=0
    else:
        R=l[-1]-l[i]
    if i==0:
        L=0
    else:
        L=i-l[i-1]
    ans=min(ans,R+L)
print(ans)

Submission status: Accepted

Solution code:
import sys

n = int(input())
s = input()
e = [0] * n
w = [0] * (n + 1)
e[0] = 1 if s[0] == 'E' else 0
w[0] = 0
w[1] = 1 if s[0] == 'W' else 0
for i in range(1, n):
    if s[i] == 'E':
        e[i] = e[i - 1] + 1
        w[i + 1] = w[i]
    else:
        e[i] = e[i - 1]
        w[i + 1] = w[i] + 1
min = sys.maxsize
for i in range(n):
    if min > e[-1] - e[i] + w[i]:
        min = e[-1] - e[i] + w[i]
print(min)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
tmp = s.count('E')
ans = 10**6
for i in range(n):
    if s[i]=='E':
        tmp -= 1
    if i>0 and s[i-1]=='W':
        tmp += 1
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

counts = Counter(s)

cnt_W, cnt_E, ans, cnt = 0, 0, counts['E'], 0
for i in range(n):
    if s[i] == 'E':
        cnt_E += 1
    
    cnt = cnt_W + (counts['E'] - cnt_E)
    ans = min(ans, cnt)
    
    if s[i] == 'W':
        cnt_W += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
easts = [0] * (N + 1)
wests = [0] * (N + 1)

for i, c in enumerate(input()):
    if c == 'E':
        easts[i + 1] += easts[i] + 1
        wests[i + 1] = wests[i]
    else:
        wests[i + 1] += wests[i] + 1
        easts[i + 1] = easts[i]

ans = 1 << 31
for i in range(N):
    ans = min(ans, easts[N] - easts[i + 1] + wests[i] - wests[0])
print(ans)