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
# input = sys.stdin.readline
from itertools import accumulate

def main():
    N = int(input())
    S = input()
    
    east = accumulate([0] + list(S), lambda total, c: total + (1 if c == "E" else 0))
    next(east)
    east = list(east)

    west = accumulate([0] + list(S), lambda total, c: total + (1 if c == "W" else 0))
    next(west)
    west = list(west)

    east_max = east[-1]
    for i in range(N):
        e = east_max - east[i]
        w = 0 if i == 0 else west[i-1]
        yield e + w



print(min(main()))

Submission status: Accepted

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

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

ans = left[0] + right[0]
for i in range(1, N):
    count = left[i] + right[i]
    if ans > count:
        ans = count
print(ans)

Submission status: Accepted

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

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

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

list = [0]*N
for i in range(N):
    list[i] = listw[i] + liste[-1] - liste[i+1 if i != N-1 else -1]

list.sort()

print(list[0])

Submission status: Accepted

Solution code:
n = int(input())
s = input()
lstW = [0]
c = 0
#累積和
for i in range(1,len(s)):
    if s[i-1] == "W":
        c += 1
    lstW.append(c)
    
lstE = [0]
c = 0
for i in range(len(s)-2,-1,-1):
    if s[i+1] == "E":
        c += 1 
    lstE.append(c)
lst = []
for i in range(len(lstW)):
    lst.append(lstW[i] + lstE[n-i-1])
lst.sort()
print(lst[0])

Submission status: Accepted

Solution code:
n = int(input())
s_list = list(input())
e_sum = [0]
su = 0
for s in s_list[:-1]:
    if s == "W":
        su += 1
    e_sum.append(su)
w_sum = [0]
su = 0
for s in s_list[::-1][:-1]:
    if s == "E":
        su += 1
    w_sum.append(su)
w_sum = w_sum[::-1]
ans = 10 ** 10
for i in range(n):
    ans = min(ans, e_sum[i] + w_sum[i])
print(ans)

Submission status: Accepted

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


def solve(N: int, S: str):
    nww = [0] * N
    nee = [0] * N

    acc = 0
    for i in range(N - 1):
        if S[i] == "W":
            acc += 1
        nww[i + 1] = acc

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

    print(min(nwwi + neei for nwwi, neei in zip(nww, nee)))


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
s = input()
list_e = [0]*n
list_w = [0]*n
for i in range(n):
  if s[i]=="E":
    list_e[i]=1
for i in range(n):
  if s[i]=="W":
    list_w[i]=1
    
list_e = list(accumulate([0]+list_e))
list_w = list(accumulate([0]+list_w))
m = 10**7
for i in range(n):
  a = list_w[i]
  b = list_e[-1]-list_e[i+1]
  m = min(m,a+b)
print(m)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = [0 for i in range(n)]
w = [0 for i in range(n)]
ee = 0
ww = 0
for i in range(n):
	w[i] = ww
	if s[i] == "E":
		ee += 1
	else:
		ww += 1
	e[i] = ee
ec = s.count("E")
wc = s.count("W")
ans = 10**6
c = 0
for i in range(n):
	c = ec - e[i] + w[i]
	ans = min(c,ans)
print (ans)

Submission status: Accepted

Solution code:
# solution

data=int(input())

ini=input()

array=[]

l=0

for i in range(data):
  if ini[i]=="W":
    l+=1
  array.append(l)

m=min(array[data-1],data-array[data-1])

for i in range(1,data):
  m=min(m,array[i-1]+data-1-i-array[data-1]+array[i])
print(m)

Submission status: Accepted

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

def f(S):
    cnt = S.count('E')
    for s in S:
        if s == 'E':
            cnt -= 1
        yield cnt
        if s == 'W':
            cnt += 1

print(min(list(f(S))))

Submission status: Accepted

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

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

for i in range(n-1,-1,-1):
    if s[i] == 'E':
        e += 1
    elist[i] = e
mn = 10**9
for i in range(n):
    mn = min(mn, elist[i]+wlist[i])
#print(elist,wlist)
print(mn-1)

Submission status: Accepted

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

def f(S):
  count = S.count('E')
  for s in S:
    if s == 'E':
      count += -1
    yield count
    if s == 'W':
      count += 1

print(min(list(f(S))))

Submission status: Accepted

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

def f(S):
  count = {'W': 0, 'E': 0}
  yield count['W'], count['E']
  for c in S:
    count[c] += 1
    yield count['W'], count['E']

count = list(f(S))
print(min([count[i-1][0] + count[-1][1] - count[i][1] for i in range(1, N+1)]))

Submission status: Accepted

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

w_cnt = [0] * n
e_cnt = [0] * n
for i, ss in enumerate(s):
    if i != 0:
        w_cnt[i] = w_cnt[i-1]
        e_cnt[i] = e_cnt[i-1]
    if ss == 'W':
        w_cnt[i] += 1
    else:
        e_cnt[i] += 1

ans = n
for i in range(n):
    left = i - 1
    right = i + 1
    left_w = 0
    right_e = 0
    if left >= 0:
        left_w = w_cnt[i-1]
    if right_e <= n - 1:
        right_e = e_cnt[-1] - e_cnt[i]
    ans = min(ans, left_w + right_e)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
int_s = [0] * N
cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)
answers = [0] * N
for i in range(N):
    if (S[i] == 'W'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_w[i + 1] = cum_sum_w[i] + int_s[i]
for i in range(N):
    if (S[i] == 'E'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_e[i + 1] = cum_sum_e[i] + int_s[i]
for i in range(N):
    answers[i] = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
print(min(answers))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
c = [0 for i in range(n)]
for i in range(0+1, n):
    if s[i] == 'E':
        c[0] += 1

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

Submission status: Accepted

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

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

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()
 
ans=s[1:].count("E")
c=ans

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()
east = s.count('E')
ans = east
for i in range(n):
  if s[i]=='E': east-=1
  if i>=1:
    if s[i-1]=='W': east+=1
  ans = min(ans,east)
print(ans)

Submission status: Accepted

Solution code:
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
s = ns()

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

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

print(min(ans))

Submission status: Accepted

Solution code:
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

def main():
    n = ni()
    s = ns()

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

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

    print(min(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
wdp = [0]*n
edp = [0]*n
if s[0] == "E":
    edp[0] = s.count("E")-1
else:
    edp[0] = s.count("E")

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

Submission status: Accepted

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

a = S[1:].count("E")
ans = [a]
for i in range(1,N):
  if S[i]=="E" and S[i-1]=="E":
    a -= 1
  elif S[i]=="E" and S[i-1]=="W":
    a += 0
  elif S[i]=="W" and S[i-1]=="E":
    a += 0
  elif S[i]=="W" and S[i-1]=="W":
    a += 1
  ans.append(a)

print(min(ans))

Submission status: Accepted

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

ans = 10 ** 8

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

Submission status: Accepted

Solution code:
import sys

n, s = sys.stdin.read().split()

def main():
    cnt = s.count('E')
    cur = 'E'
    res = [cnt]
    for c in s:
        if cur == 'W': cnt += 1
        if c == 'E': cnt -= 1
        res.append(cnt)
        cur = c
    print(min(res))
    
if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
# 累積和の手法をとる
# 簡単にいうと要素が ある区間の和 であるようなリストを先に作成しておき
# 必要な時に取り出すという手法である
# 同じ計算過程を何回も繰り返す（上の解法）ことなく結果がでる
N = int(input())
S = input()
w = [0 for _ in range(N)]       # Wを向いている人の番号に１、逆の方角の人に０を割り当てる
sw = [0 for _ in range(N+1)]    # リストwの累積和のリスト、つまり sw[i] は 0~i-1 でWを向いている人の累積和
e = [0 for _ in range(N)]       # Eを向いている人の番号に１、逆の方角の人に０を割り当てる
se = [0 for _ in range(N+1)]    # リストeの累積和のリスト
l = []

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

# 累積和を格納したリストを求める
for i in range(N):
    sw[i+1] = sw[i] + w[i]
    se[i+1] = se[i] + e[i]

# 本計算
for i in range(N):      # i 番目がリーダーだとする
    counter = sw[i] + (se[N] - se[i+1])
    l.append(counter)

print(min(l))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
result = []
w_list = []
e_list = []
w_1 = 0
e_1 = 0
for i in range(n):
  if s[i] == "W":
    w_1 += 1
    w_list.append(w_1)
    e_list.append(e_1)
  if s[i] == "E":
    e_1 += 1
    w_list.append(w_1)
    e_list.append(e_1)
  
for i in range(n):
  if i == 0:
    ans = e_list[-1] - e_list[0]
    result.append(ans)
  if i == n-1:
    ans = w_list[n-2]
    result.append(ans)
  else:
    ans_e = w_list[i-1]
    ans_w = e_list[-1] - e_list[i]
    result.append(ans_e + ans_w)
    
print(min(result))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
countr = 0
countl = 0
r = [0 for i in range(n-1)]
l = [0 for i in range(n-1)]
for i in range(n-1):
    if s[i] == 'W':
        countr += 1
    if s[n-i-1] == 'E':
        countl += 1
    r[n-i-2] = countr
    l[i] = countl
mi = min(countl,countr)
for i in range(n-1):
    mi = min(mi,r[i]+l[i])
print(mi)

Submission status: Accepted

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

mn = 10**6
for i in range(n):
    if i==0:
        left_W = 0
        right_E = (n-1)-(ruiseki[n-1]-ruiseki[0])
    elif i==n-1:
        left_W = ruiseki[n-2]
        right_E = 0
    else:
        left_W = ruiseki[i-1]
        right_E = (n-(i+1))-(ruiseki[n-1]-ruiseki[i])
    
    ans = left_W + right_E
    mn = min(mn,ans)
print(mn)

Submission status: Accepted

Solution code:
import sys

n, s = sys.stdin.read().split()

def main():
    cnt = s.count('E')
    cur = 'E'
    res = cnt
    for c in s:
        if cur == 'W': cnt += 1
        if c == 'E': cnt -= 1
        res = min(res, cnt)
        cur = c
    print(res)
    
if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

n, s = sys.stdin.read().split()

def main():
    cnt = s.count('E')
    cur = 'E'
    res = [cnt]
    for c in s:
        if cur == 'W': cnt += 1
        if c == 'E': cnt -= 1
        res.append(cnt)
        cur = c
    print(min(res))
    
if __name__ ==  '__main__':
    main()

Submission status: Accepted

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

t=[0]*n
if s[0]=="W":
  t[0]=-1
else:
  t[0]=1
for i in range(1,n):
  if s[i]=="W":
    t[i]=-1+t[i-1]
  else:
    t[i]=1+t[i-1]
    
max1=max(t)
a=t.index(max1)

ans=0
ans+=s[:a].count("W")

ans+=s[a+1:].count("E")
print(ans)

Submission status: Accepted

Solution code:
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    s = input()
    min_ = float('inf')
    lw = 0
    le = 0
    rw = s.count('W')
    re = s.count('E')

    # 1番目 [0]番目はあらかじめ計算する
    if s[0] == 'W':
        rw -=1
    else:
        re -=1
    min_ = min(min_,lw+re)
    for i in range(1,n):
        if s[i] == 'W':
            rw -=1
        else:
            re -=1
        if s[i-1] == 'W':
            lw += 1
        else:
            le += 1
        min_ = min(min_,lw + re)
    print(min_)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
rignt = 0
left = 0
#rignt = 1

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

if s[0] == "E":
  rignt -= 1

ans = rignt

for i in range(1,n):
  if s[i-1] == "E":
    left += 1
    
  if s[i] == "E":
    rignt -= 1
    
  temp = rignt + (i-left) 
  
  if temp < ans :
    ans = temp
    
print(ans)

Submission status: Accepted

Solution code:
def cal(n,s):
	cum = np.cumsum(s == 'W')
	val = [-1]*n
	for i in range(n):
		W = cum[i-1]  if i-1 >= 0 else 0 #i <- W
		H = (n-i-1) - (cum[-1] - cum[i])	 #i -> H 
		val[i] = W+H
	return min(val)

import numpy as np

n = int(input())
s = np.array(list(input()))
print(cal(n,s))

Submission status: Accepted

Solution code:
def main():
  n=int(input())
  s=input()
  E=s.count('E')
  e=[0]*n
  e[0]=1 if s[0]=='E' else 0
  ans=E-e[0]
  for i in range(1,n):
    e[i]=e[i-1] + (1 if s[i]=='E' else 0)
    x=E-e[i]+i-e[i-1]
    ans=ans if ans<=x else x
  print(ans)
if __name__=='__main__':
  main()

Submission status: Accepted

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

wcount = [0 for i in range(N)]
ecount = [0 for i in range(N)]
sumcount = [0 for i in range(N)]

for i in range(1,N):
    if i == 0:
        continue
    elif i > 0 and S[i-1] == "W":
        wcount[i] = wcount[i-1] + 1
    elif i > 0:
        wcount[i] = wcount[i-1]
 
for i in reversed(range(N)):
    if i == N:
        continue
    elif i < N-1 and S[i+1] == "E":
        ecount[i] = ecount[i+1] + 1
    elif i < N-1:
        ecount[i] = ecount[i+1]
    

for i in range(N):
    sumcount[i] = wcount[i] + ecount[i]

print(min(sumcount))

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
cum = [0]*(n+1)
for i in range(1,n+1):
    cum[i] = cum[i-1]
    if s[i-1] == 'E':
        cum[i] += 1
cnt = n
for i in range(1,n+1):
    cnt = min(cnt, (i-1)-cum[i-1]+cum[n]-cum[i])
print(cnt)

Submission status: Accepted

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

l = [0]
r = [0]
l_cnt = 0
r_cnt = 0
for i in range(n-1):
    if s[i] == 'W':
        l_cnt += 1
    if s[n-i-1] == 'E':
        r_cnt += 1
    l.append(l_cnt)
    r.append(r_cnt)
print(min(l[i]+r[n-i-1] for i in range(n)))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
INF = int(1e6)

nE = S.count("E")
nW = N - nE

lnW = 0
ans = INF

for i in S:
    if i == "W":
        nW -= 1
        ans = min(ans, nE + lnW)
        lnW += 1
    else:
        nE -= 1
        ans = min(ans, nE + lnW)

print(ans)

Submission status: Accepted

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

res = N
num = [0] * (N+1)
for i in range(0, N):
    num[i + 1] = num[i]
    if A[i] == 'W':
        num[i + 1] += 1
for i in range(1, N + 1):
    res = min(res, num[i - 1] + (N - i) - (num[N] - num[i]))

print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cum = [0]*(n+1)
for i in range(1,n+1):
    cum[i] = cum[i-1]
    if s[i-1] == 'E':
        cum[i] += 1
cnt = n
for i in range(1,n+1):
    cnt = min(cnt, (i-1)-cum[i-1]+cum[n]-cum[i])
print(cnt)

Submission status: Accepted

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

ans = float('inf')
for i in range(1,n+1):
    count = 0
    count += W_csum[i-1]
    count += E_csum[-1] - E_csum[i]
    if count < ans:
        ans = count
print(ans)

Submission status: Accepted

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

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

W_csum_r = [0] * n
E_csum_r = [0] * n
if s[-1] == 'W':
    W_csum_r[0] = 1
else:
    E_csum_r[0] = 1
for i,j in enumerate(range(n-2,-1,-1),1):
    if s[j] == 'W':
        W_csum_r[i] = W_csum_r[i-1] + 1
        E_csum_r[i] = E_csum_r[i-1]
    else:
        W_csum_r[i] = W_csum_r[i-1]
        E_csum_r[i] = E_csum_r[i-1] + 1

W_csum_r.reverse()
E_csum_r.reverse()

ans = float('inf')
for i in range(1,n-1):
    num = 0
    if s[i] == 'W':
        num += E_csum_r[i+1]
        num += W_csum[i-1]
    else:
        num += E_csum_r[i+1]
        num += W_csum[i-1]
    if num < ans:
        ans = num

if ans > E_csum_r[1]:
    ans = E_csum_r[1]
if ans > W_csum[-2]:
    ans = W_csum[-2]

print(ans)

Submission status: Accepted

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

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

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

#print(turn_west_list)
#print(turn_east_list)

answer=10**9
for i in range(N):
  answer=min(answer,turn_west_list[i]+turn_east_list[i])
  
print(answer)

Submission status: Accepted

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

l = [0]
r = [0]
l_cnt = 0
r_cnt = 0
for i in range(n-1):
    if s[i] == 'W':
        l_cnt += 1
    if s[n-i-1] == 'E':
        r_cnt += 1
    l.append(l_cnt)
    r.append(r_cnt)
print(min(l[i]+r[n-i-1] for i in range(n)))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000)
def resolve():
    N = int(input())
    S = input()
    mem = [None for i in range(N)]
    mem[0] = S[1:].count("E")
    for i in range(1, N):
        if S[i-1] == "W" and S[i] == "W":
            mem[i] = mem[i-1] + 1
        elif S[i-1] == "E" and S[i] == "E":
            mem[i] = mem[i-1] - 1
        else:
            mem[i] = mem[i-1]
    #print(mem)
    print(min(mem))

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
s = input()
sl = [0] * (N + 1)
sr = [0] * (N + 1)
for i in range(N):
    if s[i] == 'W':
        sl[i + 1] = sl[i] + 1
    else:
        sl[i + 1] = sl[i]
for i in range(N , 0, -1):
    if s[i - 1] == 'E':
        sr[i - 1] = sr[i] + 1
    else:
        sr[i - 1] = sr[i]
min_ = N
for i in range(N + 1):
    min_ = min(min_, sl[i] + sr[i])
print(min_)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(str,input()))
count = 0
for i in range(1,n):
  if s[i] == "E":
    count += 1
box = [0]*n
box[0] = count
for j in range(1,n):
  if s[j] == "E":
    count -= 1
  if s[j - 1] == "W":
    count += 1
  box[j] = count
print(min(box))

Submission status: Accepted

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


# 初期スコア
count = 0
for i in range(1, n):
    if s[i] == 'E':
        count += 1


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

print(min(arr))

Submission status: Accepted

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

# T[i]: i番目より左の'E'の個数
T = [0] * N
for i in range(1,N):
  T[i] = T[i-1]
  if S[i-1] == 'E':
    T[i] += 1

# U[i]: i番目より右の'W'の個数
U = [0] * N
for i in range(N-1,0,-1):
  U[i-1] = U[i]
  if S[i] == 'W':
    U[i-1] += 1

ans = N
for i in range(N):
  tmp = N- 1 - T[i] - U[i] 
  if tmp < ans:
    ans = tmp
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
work = s[1:].count("E")
temp = [work]
leader = s[0]
for c in s[1:]:
    if leader == "W":
        work += 1
    if c == "E":
        work -= 1
    temp.append(work)
    leader = c
print(min(temp))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc098/tasks/arc098_a

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
rr = range


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


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


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


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret

# 要は各地点で向きが揃っている人数をカウントすれば良い
# 0番目がリーダーのとき、向きが揃っている人はWのカウントとなる(あとはリーダーの文字次第)
# i番目を++するとき、[0,i)は向きが揃っているひとをカウントできる、[i+1,N)で向きが揃っているのはWが登場するたび引けば良い


N = read_a_int()
S = read()[:-1]
W = 'W'
E = 'E'

l = 0  # iの左側でEを向いている人数
r = S.count(W)  # iの右側でWを向いている人数

ans = 10**9
for s in S:
    if s == W:  # 右を更新
        r -= 1

    # 答えを更新
    ans = min(ans, N - 1 - (r + l))  # -1は自分のコストを考えない分

    if s == E:  # 左を更新
        l += 1

print(ans)

Submission status: Accepted

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

w_lst = [0] * n
e_lst = [0] * n

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

ans = 10**10
for i in range(n):
    ans = min(ans, w_lst[i] + e_lst[i])
print(ans)

Submission status: Accepted

Solution code:
# 自分より左のWと右のEを数える
# 累積和だけど自分自身は見ない

n = int(input())
s = input()
lw, re = [0] * n, [0] * n
for i in range(1, n):
    if s[i - 1] == "W":
        lw[i] = lw[i - 1] + 1
    else:
        lw[i] = lw[i - 1]
for i in range(n - 2, -1, -1):
    if s[i + 1] == "E":
        re[i] = re[i + 1] + 1
    else:
        re[i] = re[i + 1]
ans = 10**10
for i in range(n):
    ans = min(ans, lw[i] + re[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
cum_s = []
wum = 0
eum = 0
for i in range(n):
    if s[i] == "W":
        wum += 1
    else:
        eum += 1
    cum_s.append([wum,eum])
r = cum_s[-1]
ans = int(1e10)
for i in range(n):
    tar = cum_s[i]
    cnt = 0
    if s[i] == "W":
        cnt += tar[0]-1
        cnt += r[1] - tar[1]
    else:
        cnt += tar[0]
        cnt += r[1] - tar[1]
    ans = min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    w_ruisekiwa = [0] * (n+1)
    e_ruisekiwa = [0] * (n+1)

    for i in range(n):
        w_ruisekiwa[i+1] = w_ruisekiwa[i] + (1 if s[i] == "W" else 0)
        e_ruisekiwa[i+1] = e_ruisekiwa[i] + (1 if s[i] == "E" else 0)

    mn = 10 ** 10

    for i in range(n):
        l_total = w_ruisekiwa[i] - w_ruisekiwa[0]
        r_total = e_ruisekiwa[n] - e_ruisekiwa[i+1]
        mn = min(l_total + r_total,mn)

    print(mn)






if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#C - Attention
N = int(input())
S = input()
DP_w= []
DP_e = []
tmp = 0
tmpw = 0
tmpe = 0
ans = 10**9
for i in range(N):
    if S[i] == "W":
        tmpw += 1
        DP_w.append(tmpw)
    else:
        DP_w.append(tmpw)

for j in range(N-1,-1,-1):
    if S[j] == "E":
        tmpe += 1
        DP_e.append(tmpe)
    else:
        DP_e.append(tmpe)

DP_e.reverse()
for k in range(N):
    ans = min(ans,DP_w[k]+DP_e[k]-1)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
list_S = list(input())
left_W = 0
left_E = 0
right_W = list_S.count("W")
right_E = list_S.count("E")
ans = n

for i, s in enumerate(list_S):
    if s == "W":
        left_W += 1
        right_W -= 1
    elif s == "E":
        left_E += 1
        right_E -= 1

    if i == 0 and s == "W":
        ans = min(ans, right_E)
    elif i == n-1 and s == "E":
        ans = min(ans, left_W)
    else:
        ans = min(ans, left_W+right_E)

print(ans)

Submission status: Accepted

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

acc_e=[0]
acc_w=[0]
for i in S:
  acc_e.append(acc_e[-1])
  acc_w.append(acc_w[-1])
  if i=='E':
    acc_e[-1]+=1
  else:
    acc_w[-1]+=1

ans=N
for i in range(N):
  turn=acc_w[i]+acc_e[-1]-acc_e[i+1]
  ans=min(ans,turn)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
acc_e = [0]
acc_w = [0]
for c in s:
  acc_e.append(acc_e[-1])
  acc_w.append(acc_w[-1])
  if c == 'E': acc_e[-1] += 1
  else: acc_w[-1] += 1
ans = n
for i in range(n):
  turn = acc_w[i] + acc_e[-1] - acc_e[i+1]
  ans = min(ans, turn)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = []
e = []
cnt_w, cnt_e = 0, 0
for i in range(n):
    if s[i] == "W":
        cnt_w += 1
    else:
        cnt_e += 1
    w.append(cnt_w)
    e.append(cnt_e)

f_inf = float("inf")
res = f_inf
for i in range(n):
    if s[i] == "W":
        t = w[i] - w[0] + e[len(e) - 1] - e[i]
    else:
        t = w[i] + e[len(e) - 1] - e[i]
    res = min(res, t)
print(res)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

ans = [0] * N
#リーダーより東の東向きの人
ans[0] = S[1:].count("E")

#リーダーを東にずらしていく
for i in range(1, N):
    ans[i] = ans[i-1]
    if S[i-1] == "W":
        ans[i] += 1
    if S[i] == "E":
        ans[i] -= 1

#print("ans : {}".format(ans))
print(min(ans))

Submission status: Accepted

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

wc = [0]
ec = [0]

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

#print("wc : {}, ec : {}".format(wc, ec))

ans = ec[N]
for i in range(1, N):
    #W -> cnt, E -> sum - cnt
    tmp = wc[i] + ec[N] - ec[i+1]
    ans = min(tmp, ans)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
wc=[0]
ec=[0]
for i in range(n):
    if s[i]=="W":
        wc.append(wc[i]+1)
        ec.append(ec[i])
    else:
        wc.append(wc[i])
        ec.append(ec[i]+1)
ans=10**6
for i in range(1,n+1):
    tmp=0
    
    tmp+=wc[i-1]
    tmp+=ec[n]-ec[i]
    ans=min(tmp,ans)
print(ans)

Submission status: Accepted

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

#E(東)を向いてる人数
ruiseki = [0]
for i in range(n):
    if s[i] == "E":
        ruiseki.append(ruiseki[i] + 1)
    else:
        ruiseki.append(ruiseki[i])

INF = float("inf")
ans = INF
for i in range(n):
    if s[i] == "E":
        sub = -1
    else:
        sub = 0
    change = i - (ruiseki[i] - ruiseki[0] - sub)
    change += ruiseki[-1] - ruiseki[i]
    ans = min(ans, change)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = list(input())

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

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

print(min(ans))