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=list(input())
minn=5
ban=n
count=0
for i in range(n):
    if s[i]=='W':
        count=count+1
    else:
        count=count-1
    
    if count<minn:
        minn=count
        ban=i
ans=0

for j in range(n):
    if j<ban and s[j]=='W':
        ans=ans+1
    if j>ban and s[j]=='E':
        ans=ans+1


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
e_ = [0 for i in range(N)]
w_ = [0 for i in range(N)]

e_c = S.count('E')
w_c = 0
for i in range(N):
    if S[i] == 'E':
        e_c -= 1
    e_[i] = e_c

    w_[i] = w_c
    if S[i] == 'W':
        w_c += 1


# print(e_)
# print(w_)
ans = 10**9
for i in range(N):
    ans = min(ans, w_[i]+e_[i])
print(ans)

Submission status: Accepted

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

ans = 99999999

cnt_w = 0
cnt_e = 0
i = 0
for j in range(i):
    if S[j] == 'W':
        cnt_w += 1
for j in range(i,N):
    if S[j] == 'E':
        cnt_e += 1
ans = cnt_w + cnt_e

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
leader = [0] * (N)
sum_e = 0
sum_w = 0
for i in range(N):
  leader[i] += sum_e
  leader[N-i-1] += sum_w
  if S[i] == 'E':
    sum_e += 1
  if S[N-i-1] == 'W':
    sum_w += 1
print(N-max(leader)-1)

Submission status: Accepted

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

lst_ans=[]
num_e= s.count("E")
ans = num_e
for i in range(n):
    if s[i] == "E":ans -= 1
    if i != 0:
        if s[i-1]=="W":ans += 1
    lst_ans.append(ans)
print(min(lst_ans))

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
n_W,n_E=0,0
l_W,l_E=[0],[0]
for i in s:
    if i=="W":n_W+=1
    else:n_E+=1
    l_W.append(n_W)
    l_E.append(n_E)
ans=10**9+7
for i in range(n):
    tmp=l_W[i]+l_E[-1]-l_E[i+1]
    if tmp<ans:ans=tmp
print(ans)

Submission status: Accepted

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

current_and_left_w=0
ans=current_and_right_e=S.count("E")

for i in S:
    if i=="E":
        current_and_right_e-=1
    else: 
        current_and_left_w+=1
        
    ans=min(ans,current_and_right_e+current_and_left_w)
print(ans)

Submission status: Accepted

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

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

w = wcnt[N-1]
e = N-w
ans = 10**9
for j in range(N):
    tmp = wcnt[j]
    ans = min(e+2*wcnt[j]-(S[j]=='W')*1-1-j, ans)
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [0 if a == 'E' else 1 for a in input()]
Sp = [S[0]]
for i in range(1, N):
    Sp.append(Sp[-1]+S[i])

max = Sp[-1]
ans = N - max
for i in range(1, N):
    ans = min(abs(Sp[i-1]) + abs(N-(i+1)-max+Sp[i]), ans)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = [0]
w = [0]
a = []

for i in range(n):
    if s[i] == 'W':
        w.append(w[i] + 1)
        e.append(e[i])
    else:
        e.append(e[i] + 1)
        w.append(w[i])

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

Submission status: Accepted

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

c = [0] * (n+1)
ans = []
#c[i] == i番目がreaderの時のc[i]より左のEの数
for i in range(len(st)):#i == num_reader
  c[i+1] = c[i]
  if st[i] == "E":
    c[i+1] += 1
for i in range(n):
  e_left = c[i]
  e_right = c[n] - c[i+1]
  ans.append(i-e_left + e_right)
print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = n
w = [0 for i in range(n)]
e = [0 for i in range(n)]
m = [0 for i in range(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]
for i in range(n):
    m[i] = e[i] + w[i]
print(min(m))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.cumsum(w)
# w = np.add.accumulate(w)
# e = e[::-1]
e = np.cumsum(e[::-1])[::-1]
# e = np.add.accumulate(e)
# e = e[::-1]
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
print(min(ans))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.cumsum(w)
# w = np.add.accumulate(w)
e = e[::-1]
e = np.cumsum(e)
# e = np.add.accumulate(e)
e = e[::-1]
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
print(min(ans))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = e[::-1]
e = np.add.accumulate(e)
e = e[::-1]
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
print(min(ans))

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
s = input()
w = map(lambda x: 1 if x == 'W' else 0, s)
e = list(map(lambda x: 1 if x == 'E' else 0, s))
e.reverse()
w = list(accumulate(w))
e = list(accumulate(e))
e.reverse()
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
s = input()
w = list(map(lambda x: 1 if x == 'W' else 0, s))
e = list(map(lambda x: 1 if x == 'E' else 0, s))
e.reverse()
w = list(accumulate(w))
e = list(accumulate(e))
e.reverse()
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

Submission status: Accepted

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

#Eが出てくる回数の累積和を取る(O(n))
l=[0]
l_tmp=0
for i in range(n):
    if s[i]=="E":
        l_tmp+=1
        l.append(l_tmp)
    else:
        l.append(l_tmp)

#クエリ(O(n))

ans=n
for j in range(1,n+1):
    #j番目より左のWの数は(j-1)-l[j-1]
    #j番目より右のEの数はl[-1]-l[j]
    ans=min(ans,(j-1)-l[j-1]+l[-1]-l[j])
    
print(ans)

Submission status: Accepted

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

LtoR=[0]
RtoL=[0]
tmpLtoR=0
tmpRtoL=0
for i in range(N):
    #逆向きの人の累積和
    if S[i]=='W':
        tmpLtoR+=1
    LtoR.append(tmpLtoR)
    if S[N-1-i]=='E':
        tmpRtoL+=1
    RtoL.append(tmpRtoL)
LtoR.append(0)
RtoL.append(0)
RtoL.reverse()

total=N
for i in range(1,N+1):
    #print(i,LtoR[i-1]+RtoL[i+1])
    total=min(total,LtoR[i-1]+RtoL[i+1])
print(total)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
lp = [0] * N
rp = [0] * N
for i in range(N):
    if i != 0:
        lp[i] = lp[i-1]
        if s[i-1] == 'W':lp[i]+=1
for j in range(N-1,-1,-1):
    if j != N-1:
        rp[j] = rp[j+1]
        if s[j+1] == 'E':rp[j]+=1
g = 9999999999999999
for i in range(N):g=min(g,lp[i]+rp[i])
print(g)

Submission status: Accepted

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input())
    S = list(_ for _ in input())
    l = S.count('E')
    r = 0
    ans = min(S.count('E'),S.count('W'))
    for i in range(N):
        if i != N-1:
            if S[i] == 'E':
                l = l - 1
        if i != 0:                        
            if prev == 'W':
                r = r + 1
        prev = S[i]
        ans = min(ans,l+r)
        # print(ans,l,r)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = s[1:].count("E")
tmp = ans

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

Submission status: Accepted

Solution code:
N = int(input())
s = input().split()
s = list(s[0])
right = 0
left = 0
for i in range(1,N):
    if s[i] == "E":
        right += 1
ans = right
for i in range(1, N):
    if s[i] == "E":
        right -= 1
    if s[i-1] == "W":
        left += 1
    if ans > right + left:
        ans = right + left
print(ans)

Submission status: Accepted

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

#累積和を用いる
from itertools import accumulate

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

x = [0]+[1 if s[i] == 'W' else 0 for i in range(n)]
y = [1 if s[i] == 'E' else 0 for i in range(n)] + [0]

mx = list(accumulate(x))
my = list(accumulate(y[::-1]))
my = my[::-1]
mi = float('inf')
for i in range(n+1):
    a = mx[i]
    b = my[i]
    mi = min(mi,a+b)
print(mi)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = [s[1:].count('E')]

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

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
L = [0]
R = [0]
for i in range(1, N):
	if S[i-1] == 'W':
		L.append(L[-1]+1)
	else:
		L.append(L[-1])
	if S[-i] == 'E':
		R.append(R[-1]+1)
	else:
		R.append(R[-1])
R.reverse()
min = 5000000000000000
for i in range(0, N):
	if L[i]+R[i] < min:
		min = L[i]+R[i]
print(min)

Submission status: Accepted

Solution code:
import sys


fin = sys.stdin.readline
N = int(fin())
S = fin().rstrip("\n")
num_wests_from_left = [0] * N
cur_num_wests = 0
for i, d in enumerate(S):
    cur_num_wests += 1 if d == 'W' else 0
    num_wests_from_left[i] = cur_num_wests

num_easts_from_right = [0] * N
cur_num_easts = 0
for i in reversed(range(N)):
    d = S[i]
    cur_num_easts += 1 if d == 'E' else 0
    num_easts_from_right[i] = cur_num_easts

min_num_changing = N - 1
for i in range(N):
    num_changing = (num_wests_from_left[i - 1] if i > 0 else 0) + (num_easts_from_right[i + 1] if i < N - 1 else 0)
    min_num_changing = min(min_num_changing, num_changing)
print(min_num_changing)

Submission status: Accepted

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

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

ans = n
for i in range(n):
    e_left = cs[i]
    e_right = cs[n] - cs[i+1]
    # print(i, e_left, e_right)
    ans = min(ans, (i - e_left) + e_right)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a=b=s[1:].count('E')

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

Submission status: Accepted

Solution code:
n = int(input())
S = input()
NE = [0 for i in range(n)]
NE[0] = 1 if S[0] == "W" else 0
for i in range(1,n):
    NE[i] = NE[i-1] + (1 if S[i] == "W" else 0)
#print(NE)
ans = 10**10
for i in range(n):
    nw = NE[i-1] if i>0 else 0
    ne = n-i-(NE[n-1]-nw) + (-1 if S[i]=="E" else 0)
    ans = min(ans, nw+ne)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
sList = list(input())
total = 0
for i in range(1, n):
    if sList[i] == "E":
        total+= 1
minNum = total
for i in range(1, n):
    if sList[i-1] == "W":
        total += 1
    if sList[i] == "E":
        total -= 1
    if total < minNum:
        minNum = total
print(minNum)

Submission status: Accepted

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

e = [0]


for i in range(n):
    if s[i]=="E":
        e.append(e[i]+1)
    else:
        e.append(e[i])

w = [0]

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



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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
acc_E = [0] * (N + 1)
for i in range(N):
    acc_E[i + 1] = acc_E[i] + (S[i] == 'E')
result = N
for leader in range(N):
    left_W = leader - acc_E[leader]
    right_E = acc_E[N] - acc_E[leader + 1]
    result = min(result, left_W + right_E)
print(result)

Submission status: Accepted

Solution code:
from collections import Counter

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

count_E = Counter(S)['E']
count_W = Counter(S)['W']

right = 0
left = count_E
tmp = N
for i in range(N):
    if S[i] == 'E':
        left -= 1
    if right + left < tmp:
        tmp = right + left
    if S[i] == 'W':
        right += 1
print(tmp)

Submission status: Accepted

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

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

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

    if count < min:
        min = count

print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 写経

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


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


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
hdg = S[1:N].count('E')
hdg_chgd = hdg
hdg_chg_nrs =[]
for i in range(1,N):
    if S[i] == 'E':
        hdg_chgd -= 1
    if S[i-1] == 'W':
        hdg_chgd += 1
    hdg_chg_nrs.append(hdg_chgd)
hdg_chgd_f = min(hdg_chg_nrs)
print(min(hdg, hdg_chgd_f))

Submission status: Accepted

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

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

calc = ans

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC098C - Attention (ARC098C)
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()
d = []

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

d.append(num_first)

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

print(min(d))

Submission status: Accepted

Solution code:
N = int(input())
S = str(input()) + 'W'

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(min(ans))

Submission status: Accepted

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

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

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

L = []  
for i in range(1, N+1):
    num = w[i-1] + e[N] - e[i]
    L.append(num)

print(min(L))

Submission status: Accepted

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

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

# それぞれをリーダーとして試算する
for i in range(1,n):
    # リーダーより左のWの数を累積していく
    if s[i]=="E":
        mincount-=1
    if s[i-1]=="W":
        mincount+=1
    ans=min(ans,mincount)
print(ans)

Submission status: Accepted

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

w = [0]*N
for i in range(1,N):
    #リーダーがi番目にいると仮定
    #リーダーよりにwestにいる人が何人いるか
    if S[i-1] == 'E':
        w[i] = w[i-1]+1
    else:
        w[i] = w[i-1]

e = [0]*N
for i in reversed(range(N)):
    #リーダーがi番目にいると仮定
    #リーダーよりにwestにいる人が何人いるか
    if i==N-1:
        continue

    if S[i+1] == 'W':
        e[i] = e[i+1]+1
    else:
        e[i] = e[i+1]

ans = 1
for i in range(N):
    ans = max(ans, e[i] + w[i]+1) #自分を含める

print(N - ans)

Submission status: Accepted

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

L = 0
R = s.count("E")
ans = L + R

for i in range(N):
    if s[i] == "W":
        L += 1
    if s[i] == "E":
        R -= 1
    
    if L + R < ans:
        ans = L + R

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  s=S()

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

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

  # print(a)
  # print(b)

  mn=inf
  for i in range(n):
    mn=min(mn,a[i]+b[i]-1)

  return mn

print(main())

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=[0]*(n+1)
e=[0]*(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]
l=[]
for i in range(1,n+1):
    l.append(w[i-1]+e[n]-e[i])
print(min(l))

Submission status: Accepted

Solution code:

N = int(input())

S = input()

S_re = "".join(list(reversed(S)))

left = []
right = []

now = 0
for i in range(N):

    left.append(now)

    if S[i] == "W":
        now += 1

now = 0
for i in range(N):

    right.append(now)

    if S_re[i] == "E":
        now += 1

right.reverse()


ans = N
for i in range(N):

    ans = min(ans, left[i] + right[i] )
        

print (ans)

Submission status: Accepted

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

cnt_right = S[1:].count('E')
cnt_left = 0
ans = cnt_right + cnt_left

for ichi in range(len(S)-1):
    if S[ichi] == 'W':
        cnt_left += 1
    if S[ichi+1] == 'E':
        cnt_right -= 1
    ans = min(ans, cnt_right + cnt_left)
    
print(ans)

Submission status: Accepted

Solution code:
# 入力
N = input()

A1 = [ n=='E' for n in input()]


minimum = sum(A1[1:])
count = minimum

for i in range(1,int(N)):
    if(not A1[i-1]):
        count += 1
    if(A1[i]):
        count -= 1
    if(count<minimum):
        minimum = count
print(minimum)

Submission status: Accepted

Solution code:
import numpy as np
import sys
buf = sys.stdin.buffer
N = int(buf.readline())
S = np.frombuffer(buf.read(N), dtype='S1')
is_E = (S == b'E')
is_W = ~is_E

left_E = is_E.cumsum() - is_E
left_W = np.arange(N) - left_E
right_E = is_E.sum() - left_E - is_E
right_W = is_W.sum() - left_W - is_W

answer = (left_W + right_E).min()
print(answer)

Submission status: Accepted

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

N = int(input())
S = list(input().strip())
t = [[0, 0] for _ in range(N)]

w = 0
e = 0
for i in range(0, N):
    if i != 0 and S[i] == "E":
        e += 1
        
    if i != N - 1 and S[i] == "W":
        w += 1
        
t[0][1] = e
t[-1][0] = w

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

ans = 10 ** 18
for v in t:
    ans = min(ans, v[0] + v[1])
    
print(ans)

Submission status: Accepted

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

higashi=[0]*(n+1)
nishi=[0]*(n+1)

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

ans=[0]*(n+1)    
for i in range(n+1):
    ans[i]=nishi[i]+higashi[i]

print(min(ans))

Submission status: Accepted

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

L = 0
R = s[1:N].count("E")
ans = L + R

for i in range(N-1):
    if s[i] == "W":
        L += 1
    if s[i+1] == "E":
        R -= 1
    
    if L +  R < ans:
        ans = L + R

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
move=S[1:].count("E")
ans=[move]

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()

east = []
west = []
tmp_e = 0
tmp_w = 0
for i in range(N):
    if S[i] == 'W':
        tmp_e += 1
    east.append(tmp_e)
    if S[N-1-i] == 'E':
        tmp_w += 1
    west.append(tmp_w)

mn = N
for i in range(N):
    e = east[i-1] if i else 0
    w = west[N-1-i] if i != N-1 else 0
    if e + w < mn:
        mn = e + w
print(mn)

Submission status: Accepted

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

l = [0]*n
wl = [0]*n
el = [0]*n

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

for i in reversed(range(1,n)):
  if i == n:
    el[i-1] = S[i].count("E")
  else:
    el[i-1] = S[i].count("E")+el[i]
    
for i in range(n):
  l[i] = wl[i] + el[i]
  
print(min(l))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
we = []
w = 0
e = 0

for i in range(N):
    if S[i] == "W":
        w += 1
    else:
        e += 1
    we.append([w,e])

e_count = we[-1][1]
ans = min(e_count,we[-1][0])
for i in range(N):
    ans = min(we[i][0] + abs(we[i][1] - e_count), ans)
print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8

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

line = []
E = 0
W = 0
error = 0

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

counter = 0

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

    line.append(counter)

print(min(line))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E = S.count('E')
W = 0
res = E
for s in S:
    if s == 'E':
        E -= 1
    else:
        W += 1
    res = min(res,W+E)
print(res)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = input()
ans_list = [0]*n
e_num = Counter(s[1:])['E']
ans = e_num
for i in range(1, n):
    if s[i] == 'E':
        e_num -= 1
    if s[i-1] == 'W':
        e_num += 1
    ans = min(e_num, ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s_min = s[1:].count("E")
s_now = s[1:].count("E")

for i in range(1,n):
    if s[i-1] == "E" and s[i] == "E":
        s_now -= 1
    if s[i-1] == "W" and s[i] == "W":
        s_now += 1
    s_min = min(s_min, s_now)
print(s_min)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=list(S())

  l1=[0]*n
  for i in range(n):
    if l[i]=='E':
      l1[i]+=1

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

  l2=[]
  for i in range(n):
    l2.append(i+1-l1[i])

  mn=inf
  for i in range(n):
    if l[i]=='W':
      a=l2[i]-1
    else:
      a=l2[i]
    mn=min(mn,l1[-1]-l1[i]+a)

  return mn

print(main())

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E = S.count('E')
res = E
W = 0
for s in S:
    if s == 'E':
        E -= 1
    else:
        W += 1
    res = min(res,W+E)
print(res)

Submission status: Accepted

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

left_w = [0 for i in range(n + 2)]
right_e = [0 for i in range(n + 2)]

for idx, ch in enumerate(s, 1):
    if ch == 'E':
        right_e[idx] = 1
    else:
        left_w[idx] = 1

for i in range(1, n + 2):
    left_w[i] += left_w[i - 1]

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

min_turn = n
for i in range(1, n + 1):
    turn = left_w[i - 1] + right_e[i + 1]
    min_turn = min(min_turn, turn)

print(min_turn)

Submission status: Accepted

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

left_w = [0 for i in range(n + 2)]
right_e = [0 for i in range(n + 2)]

for idx, ch in enumerate(s, 1):
    if ch == 'E':
        right_e[idx] = 1
    else:
        left_w[idx] = 1

for i in range(1, n + 2):
    left_w[i] += left_w[i - 1]

for i in range(n, 0, -1):
    right_e[i] += right_e[i + 1]

min_turn = n
for i in range(1, n + 1):
    turn = left_w[i - 1] + right_e[i + 1]
    min_turn = min(min_turn, turn)

print(min_turn)

Submission status: Accepted

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

tobi = lambda x: 1 if x=='E' else 0
S = list(map(tobi, list(S)))

ans = [sum(S[1:])]
for i in range(1, N):
    check1, check2 = 0, 0
    if S[i] == 1:
        check1 = 1
    if S[i-1] == 0:
        check2 = 1
    ans.append(ans[-1] - check1 + check2)
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
total = [0] * (N + 1)
for i in range(N):
    if S[i] == 'W':
        total[i + 1] = total[i] + 1
    else:
        total[i + 1] = total[i]

min_num = min(total[i] + (N - i - 1) - (total[N] - total[i + 1]) for i in range(N))

print(min_num)

Submission status: Accepted

Solution code:
import math
import numpy as np
def main(N, S):
    e_cum_list = []
    w_cum_list = []
    e_sum = 0
    w_sum = 0
    for char in S:
        if char == 'E':
            e_sum +=1
        else:
            w_sum +=1
        e_cum_list.append(e_sum)
        w_cum_list.append(w_sum)

    min_val = 10**6
    for i in range(N):
        
        if i - 1 < 0:
            w_cum = 0
        else:
            w_cum = w_cum_list[i - 1]
        
        e_cum = e_cum_list[-1] - e_cum_list[i]

        ew_sum = e_cum + w_cum
        if ew_sum < min_val:
            min_val = ew_sum

    print(min_val)




if __name__ == "__main__":
    N = int(input())
    S = input()
    
    main(N, S)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
es = s.count('E')
e_cnt = 0
l = [0]*n
for i in range(n):
    left = i-e_cnt
    if s[i] == 'E':
        e_cnt +=1
    right = es - e_cnt
    l[i] = right+left

print(min(l))

Submission status: Accepted

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

cost = S.count('E')

ans = N
for c in S:
    if c == 'E':
        cost -= 1
    else:
        if ans > cost:
            ans = cost
        cost += 1
else:
    if ans > cost:
        ans = cost
print(ans)

Submission status: Accepted

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

W_cnt_array = [0] * N
E_cnt_array = [0] * N

W_cnt = 0
E_cnt = 0
for i in range(N):
    if S[i] == 'W':
        W_cnt += 1
    elif S[i] == 'E':
        E_cnt += 1
    W_cnt_array[i] = W_cnt
    E_cnt_array[i] = E_cnt

prev_dir = S[0]
num = min(max(E_cnt_array), max(W_cnt_array))
for i in range(1, N):
    if prev_dir == 'E' and S[i] == 'W':
        num = min(num, W_cnt_array[i - 1] + E_cnt_array[-1] - E_cnt_array[i])
    prev_dir = S[i]
print(num)