Problem:


Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

2 \leq N \leq 3 \times 10^5

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Accepted

Solution code:
N=int(input())
S=input()
cnt = 0
for i in range(1,N):
  if S[i] == 'E': cnt += 1
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 = float('inf')
w_left = [0]
e_right = [0]*n

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

for i in range(n):
    ans = min(ans , w_left[i]+e_right[i])
print(ans)

Submission status: Accepted

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

ans = float('inf')
w_left = [0]*n
e_right = [0]*n

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

for i in range(n):
    if ans > w_left[i]+e_right[i]:
        ans = w_left[i]+e_right[i]
print(ans)

Submission status: Accepted

Solution code:
#098_C
n=int(input())
s=input()

sw=[0 for i in range(n)]
for i in range(n):
    if i==0:
        sw[0]+=(s[i]=='W')
    else:
        sw[i]=sw[i-1]+(s[i]=='W')
        
ans=10**6
for i in range(n):
    res=(n-i-1)-(sw[-1]-sw[i])
    if i>0:
        res+=sw[i-1]
    ans=min(ans,res)
print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

cnt = s[1:].count('E')
low = cnt
for i in range(n-1):
  if s[i] == 'W':
    cnt += 1
  if s[i+1] == 'E':
    cnt -= 1
  if cnt < low:
    low = cnt
print(low)

Submission status: Accepted

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

cntw=[0]*n
cnte=[0]*n

if s[0]=="W":
    cntw[0]=1
    cnte[0]=0
else:
    cntw[0]=0
    cnte[0]=1

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

nmin=cnte[n-1]-cnte[0]

for i in range(n):
    cntwi=cntw[i-1]    
    cntei=cnte[n-1] -cnte[i]    
    if nmin>cntwi+cntei:
        nmin=cntwi+cntei
print(nmin)

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input().rstrip())
s = input().rstrip()

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

west_count = [0 for i in range(n)]
west_count[0] = 1 if s[0] == "W" else 0

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

ans = n
for i in range(n):
    tmp = 0
    if i < n-1:
        tmp += east_count[i+1]
    if i > 0:
        tmp += west_count[i-1]

    ans = min(ans,tmp)

print(ans)

Submission status: Accepted

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

counter = [0] * n

counter[0] = s[1:].count('E')

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

print(min(counter))

Submission status: Accepted

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

# 一番西がリーダーの時
l = 0
# 東を向いている人の数
r = S[1:].count('E')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'W':  # 元リーダーの人が西向きの時
        l += 1
    if S[i] == 'E':  # 現リーダーが東向きの時
        r -= 1
    cnt = l+r
    ans = min(cnt, ans)  # 向きを変える必要のある人数
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = input()
e = [0]*n
w = [0]*n
for i in range(n):  
    if s[n-1-i] == "E":e[i] = e[i-1]+1
    else: e[i] = e[i-1]
    if s[i] == "W":w[i] = w[i-1]+1
    else: w[i] = w[i-1]
#print(e,w,s)
ans = e[n-2]
for i in range(1,n):
    #print(i,ans)
    if i == n-1:
        ans = min(ans,w[i-1])
    else: ans = min(ans,w[i-1]+e[n-2-i])
print(ans)

Submission status: Accepted

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

cnt = 0
ans = 10 ** 10

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

ans = min(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()

chg = [0]*n

cnt = 0
for i in range(1, n):  # 左側から
    if s[i-1] == 'W':
        cnt += 1
    chg[i] += cnt

cnt = 0
for i in range(1, n):  # 右側から
    if s[-i] == 'E':
        cnt += 1
    chg[-1-i] += cnt
    
print(min(chg))

Submission status: Accepted

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

# 一番左がリーダーの時
l = 0
r = S[1:].count('W')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'E':  # 元リーダーの人が東向きの時
        l += 1
    if S[i] == 'W':  # 元リーダーが西向きの時
        r -= 1
    cnt = l+r
    ans = max(cnt, ans)  # 向きを変える必要のない人数
print(N - ans -1)  # 向きを変える必要のある人数

Submission status: Accepted

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

l=0
r=s[1:].count("E")
ans=r
MIN=r

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
S = []
for i in s:
    S.append(i)

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

e = [0 for _ in range(n)]
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]

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

Submission status: Accepted

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

def E_W():
    score = []
    s = 0
    
    for i in range(N):
        if (S[i] == 'W'):
            s -= 1
        else:
            s += 1
        score.append(s)
    
    max_index = score.index(max(score))
    
    cnt = S[:max_index].count('W') + S[max_index + 1 :].count('E')
    
    
    return (cnt)

ans = E_W()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
result = [s[1:].count("E")]
for i in range(n-1):
    if s[i:i+2] == ["W","W"]:
        result.append(result[-1]+1)
    elif s[i:i+2] == ["E","E"]:
        result.append(result[-1]-1)
    else:
        continue
print(min(result))

Submission status: Accepted

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

a = [0] * n
b = [0] * n
c = [0] * n

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

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

# print(a)
# print(b)
for i in range(n):
    c[i] = a[i] + b[i]

print(min(c))

Submission status: Accepted

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

Wlist = [0]*(N+1) 
Elist = [0]*(N+1) 

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

ans = []

for i in range(1,N+1):
    ans.append((Elist[-1] - Elist[i]) + (Wlist[i-1]))
    
print(min(ans))

Submission status: Accepted

Solution code:
n=int(input())
S=input()
s=[]
s0=S.count("E",1,n)
slast=S.count("W",0,n-1)
s.append(s0)
for i in range(1,n):
    si=s[i-1]
    if S[i-1:i+1]=="EE":
        si+=-1
    if S[i-1:i+1]=="WW":
        si+=1
    s.append(si)
s.append(slast)
print(min(s))

Submission status: Accepted

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

#はじめ全員Wであると仮定
E_right,W_left=s.count('E'),0
mi=n #最小値
for i in range(n):
    if s[i]=='E': E_right-=1 #Eならデクリメント
    if W_left+E_right<mi: mi=W_left+E_right #更新
    if s[i]=='W': W_left+=1 #Wならインクリメント
print(mi)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

        ans = min(cum, ans)
    print(ans)

if __name__ == "__main__":
    main()

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(n+1):
    tmp=0
    
    tmp+=wc[i]
    tmp+=ec[n]-ec[i]
    ans=min(tmp,ans)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

def main():
    N = int(input())
    S = list(input())
    e_right = collections.Counter(S)["E"]
    w_left = 0
    vals = set([])
    for i in range(N):
        if S[i] == "E":
            e_right -= 1
        vals.add(w_left + e_right)
        if S[i] == "W":
            w_left += 1

    print(min(vals))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
cnt = S.count('E',1,N)
ans = [cnt]

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

Submission status: Accepted

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

Submission status: Accepted

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

a, b = [], []
for i in range(n):
    if s[i] == 'W':
        a.append(1)
        b.append(0)
    else:
        a.append(0)
        b.append(1)        
        
import itertools
accum_a = [0] + list(itertools.accumulate(a))
accum_b = list(itertools.accumulate(b))

ans = float('inf')
for i in range(n):
    ans = min(ans, accum_a[i] + abs((accum_b[-1] - accum_b[i])))
print(ans)

Submission status: Accepted

Solution code:
n,s=open(0);a=t=s[1:].count('E')
for i in range(int(n)-1):t+=(s[i]>'F')-(s[i+1]<'F');a=min(a,t)
print(a)

Submission status: Accepted

Solution code:
input();s=input();a=t=s[1:].count('E')
for i in range(len(s)-1):t+=(s[i]>'F')-(s[i+1]<'F');a=min(a,t)
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

tem = S[1:].count("E")
ans = min(N,tem)

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

print(ans)

Submission status: Accepted

Solution code:
from sys import maxsize

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

count_e = [0]
count_w = [0]
for i in range(1, n+1):
    if s[i-1] == 'E':
        count_e.append(count_e[i-1]+1)
        count_w.append(count_w[i-1])
    else:
        count_e.append(count_e[i-1])
        count_w.append(count_w[i-1]+1)

min = maxsize
for i in range(n):
    val = count_w[i] + count_e[n] - count_e[i+1]
    if val < min:
        min = val

print(min)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
# 一番左がリーダーの時
l = 0
r = S[1:].count('W')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'E':  # 元リーダーの人が東向きの時
        l += 1
    if S[i] == 'W':  # 元リーダーが西向きの時
        r -= 1
    cnt = l+r
    ans = max(cnt, ans)  # 向きを変える必要のない人数
print(N - ans -1)  # 向きを変える必要のある人数

Submission status: Accepted

Solution code:
N = int(input())
S = input()
for i in range(N):
    if i == 0:
        num = S[i + 1:].count("E")
        num_min = num
    else:
        if S[i - 1] == "W":
            num += 1
        if S[i] == "E":
            num -= 1
        if num < num_min:
            num_min = num
print(num_min)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda x: [ni() for _ in range(x)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda x: [na() for _ in range(x)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda x: [ns() for _ in range(x)]
nas = lambda: stdin.readline().split()

n = ni()
s = ns()

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#----------
N = int(input().strip())
A_list = list(input().strip())
#----------
W_member_sum_0_to_N = [0]*len(A_list)
E_member_sum_N_to_0 = [0]*len(A_list)

cnt=0
for i in range(len(A_list)):
    if A_list[i] == "W":
        cnt += 1
    W_member_sum_0_to_N[i] += cnt

cnt=0
for i in range(len(A_list)-1,-1,-1):
    if A_list[i] == "E":
        cnt += 1
    E_member_sum_N_to_0[i] += cnt
        

count = N
for i_leader in range(len(A_list)):
    if i_leader == 0:
        turn_member = E_member_sum_N_to_0[i_leader+1]
    elif i_leader == len(A_list)-1:
        turn_member = W_member_sum_0_to_N[i_leader-1]
    else:
        turn_member = W_member_sum_0_to_N[i_leader-1] + E_member_sum_N_to_0[i_leader+1]

    if turn_member < count:
        count = turn_member


print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
c=[0 for _ in range(n)]
c[0]=s[1:].count('W')
for i in range(1,n):
	if s[i-1] =='W' and s[i]=='W':
		c[i]=c[i-1]-1
	elif s[i-1]=='E' and s[i]=='E':
		c[i]=c[i-1]+1
	else:
		c[i]=c[i-1]
print(n-1-max(c))

Submission status: Accepted

Solution code:

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

W = [0]*(N+1)
E = [0]*(N+1)

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

INF = 1<<30
ans = INF

for i in range(N):
    res = W[i] + E[i+1]
    ans = min(ans,res)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
numE = s.count('E')
numW = 0
answer = numE
for i in range(N):
    if s[i] == 'W':
        answer = min(answer, numE + numW)
        numW += 1
    else:
        numE -= 1
        answer= min(answer, numE + numW)
 
print(answer)

Submission status: Accepted

Solution code:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce

n = int(input())
s = input()
leader = 1
ans = s[1:].count("E")
old = ans
while leader < n:
    num = old
    if s[leader - 1] == "W":
        num += 1
    if s[leader] == "E":
        num -= 1
    ans = min(ans, num)
    old = num
    leader += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
numE = s.count("E")
numW = 0
ans = 10**10
for i in range(N):
    if s[i] == 'W':
        ans = min(ans, numE + numW)
        numW += 1
    else:
        numE -= 1
        ans= min(ans, numE+numW)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

num_E=s.count('E')
num_W=0
ans=float('inf')
for i in range(n):
    if s[i]=='W':
        ans=min(ans,num_W+num_E)
        num_W+=1
    else:
        num_E+=-1
        ans=min(ans,num_W+num_E)
print(ans)

Submission status: Accepted

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

sum_e=0
for tmp in s:
    if tmp=="E":
        sum_e+=1

ans=sum_e
for tmp in s:
    if tmp=="E":
        sum_e-=1
    else:
        sum_e+=1
    if ans > sum_e:
        ans=sum_e

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
L=[0]*N
R=[0]*N
Lcnt=0
Rcnt=0
for i in range(1,N):
    if(S[i-1]=='W'):
        Lcnt+=1
    L[i]=Lcnt
for i in reversed(range(0,N-1)):
    if(S[i+1]=='E'):
        Rcnt+=1
    R[i]=Rcnt
        
ans=10e+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()
left_l = [0]
right_l = [0]
lcnt = 0
for i in range(1, n):
    if s[i - 1] == "W":
        lcnt += 1
    else:
        pass
    left_l.append(lcnt)
rcnt = 0
for j in range(1,n):
    if s[-j] == "E":
        rcnt += 1
    else:
        pass
    right_l.append(rcnt)
right_l.reverse()
minVal = n
for k in range(n):
    if left_l[k] + right_l[k] <= minVal:
        minVal = left_l[k] + right_l[k]
    else:
        continue
print(minVal)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
W = [0]
E = [0]
ans = float('inf')
for s in S:
    if s == "W":
        W.append(W[-1]+1)
        E.append(E[-1])
    else:
        W.append(W[-1])
        E.append(E[-1]+1)
for i in range(1, N+1):
    ans = min(ans, W[i-1]+E[-1]-E[i])
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
RS=S[::-1]
W=[0]
E=[0]
for i in range(N):
    if S[i]=="W":
        W.append(W[-1]+1)
    else:
        W.append(W[-1])
W.append(0)
for i in range(N):
    if RS[i]=="E":
        E.append(E[-1]+1)
    else:
        E.append(E[-1])
E.append(0)
E=E[::-1]
#print(W)
#print(E)
ans=10**9
for i in range(1,N+1):
    if W[i-1]+E[i+1]<ans:
        ans=W[i-1]+E[i+1]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
X=[1 if s=='W' else 0 for s in S]
import itertools
cumsumW = list(itertools.accumulate([0]+X))
ans=N
for i in range(1,N+1):
  _=cumsumW[i-1]+(N-i)-(cumsumW[-1]-cumsumW[i])
  if _<ans:
    ans=_
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
w = [0]
e = [0]
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])
e.append(e[-1])
# print(e)
# print(w)
ans = int(1e9)
for i in range(n):
    if w[i] + (e[-1] - e[i+1]) < ans:
        ans = w[i] + (e[-1] - e[i+1])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sumE=[0]*(N+1)

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

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

Submission status: Accepted

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

E_lst = []
cnt = 0
for i in range(N):
    if S[i] == 'E':
        cnt += 1
    E_lst.append(cnt)
min = N + 1
for i in range(N):
    nw = 0 #i番目の人の左側にいるWの人数
    if i > 0:
        nw = i - E_lst[i - 1]
    ne = E_lst[N - 1] - E_lst[i]
    if nw + ne < min:
        min = nw + ne
print(min)

Submission status: Accepted

Solution code:
def main():
    people_num = int(input())
    directions = input()
    east_direction = [0]
    for i in range(people_num):
        if directions[i] == "E":
            east_direction.append(east_direction[i] + 1)
        else:
            east_direction.append(east_direction[i])
    answer = float("inf")
    for i in range(1, people_num + 1):
        change_to_east = i - 1 - east_direction[i - 1]
        change_to_west = east_direction[-1] - east_direction[i]
        answer = min(answer, change_to_east + change_to_west)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i, j in enumerate(s):
    if j == "W":
        a[i + 1] = a[i] + 1
    else:
        a[i + 1] = a[i]

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

Submission status: Accepted

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

# W[-1], E[-1]を許容するため、1つ多く箱を用意
W = [0] * (N + 1)
E = [0] * (N + 1)

# W, Eの累積和を算出
for i in range(N):
    if S[i] == "W":
        W[i] += W[i-1] + 1
        E[i] += E[i-1]
    else:
        W[i] += W[i-1]
        E[i] += E[i-1] + 1

# リーダーを左から1つずつずらした場合の向きを変える人の数を総当たり
ans = 3 * 10 **5
for i in range(N):
    # ①i番目のリーダーよりも左に位置し、WESTを向いている人の数
    change_west_to_east = W[i-1]
    # ②i番目のリーダーよりも右に位置し、EASTを向いている人の数
    change_east_to_west = E[N-1] - E[i]
    # 向きを変える必要があるのは①と②の和
    change = change_east_to_west + change_west_to_east
    # changeが最小値の場合、答えを更新
    ans = min(ans, change)

print(ans)

Submission status: Accepted

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

ans = 3*(10**5)+1
right_E = S.count('E')
W_left = 0

for n in range(N):
  _ = S[n]
  if _ == 'E':
    right_E -= 1
    cnt = W_left+right_E
  elif _ == 'W':
    W_left += 1
    cnt = W_left+right_E-1
  ans = min(ans,cnt)

print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy

# 最大公約数
def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z

# 幅優先探索(BFS)でスタート座標(x, y)からの最大距離返す
def bfs(SS, x, y, H, W):#入力(座標系(迷路), x, y, xの端, yの端)
  x_next = [0, 0, -1, 1]
  y_next = [1, -1, 0, 0]
  tempx = x
  tempy = y
  mlength = 0
  q = deque()
  xytuple = (x, y, 0)
  q.append(xytuple)


  while q:
    xytuple = q.popleft()
    SS[xytuple[0]][xytuple[1]] = "x" #探索した場所は探索できないようにする
    for i in range(4): #このforで上下左右を調べてる
      tempx = xytuple[0] + x_next[i]
      tempy = xytuple[1] + y_next[i]
      #print(tempx, tempy, xytuple[2])
      #print(S)
      if(0 <= tempx and tempx < H and 0 <= tempy and tempy < W and SS[tempx][tempy] == "."):
        temptuple = (tempx, tempy, xytuple[2]+1)
        SS[temptuple[0]][temptuple[1]] = "x" #探索した場所は探索できないようにする
        mlength = max(mlength, xytuple[2] +1)
        q.append(temptuple)
      #print(q)
  return mlength

#素因数分解
def prime_factorize(n):
  a = []
  while n % 2 == 0:
    a.append(2)
    n //= 2
  f = 3
  while f * f <= n:
    if n % f == 0:
      a.append(f)
      n //= f
    else:
      f += 2
  if n != 1:
    a.append(n)
  return a

#重複を検出
def has_duplicates(seq):
  return len(seq) != len(set(seq))



#X = str(input()).split()
#a = [int(x) for x in input().split()]


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

c = S.count("E")

if(S[0] == "E"):
  c -= 1

ans = 0

temp = c

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




print(temp)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

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

left_W = [0]*N
right_E = [0]*N

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

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

ans = N
for i in range(N):
    ans = min(ans, left_W[i]+right_E[i])

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
buf = sys.stdin.buffer
N = int(buf.readline())
L = list(input())

tE = L.count("E")
tW = N-tE
ce,cw = 0,0
ans = 3*(10**5)
for i in range(N):
    if L[i] == "E":
        ce +=1
    elif L[i] == "W":
        cw +=1
    if L[i] == "E":
        ans = min(ans,cw+(tE-ce))
    elif L[i] == "W":
      
        ans = min(ans,cw-1+(tE-ce))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    if s[i] == 'E':
        curr_change_cnt -= 1

    min_change_cnt = min(curr_change_cnt, min_change_cnt)

print(min_change_cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

C_E = Counter(S[1:])
C_W = Counter()
ans = C_E['E']

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(s)):
    if s[i] == "E":
        e[i+1] = e[i] + 1
        w[i+1] = w[i]
    else:
        w[i+1] = w[i] + 1
        e[i+1] = e[i]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n=int(input())
s=str(input())
e_cnt=s.count('E')
num=[0]*n #Wの数
num2=[0]*(n+1) #Eno左から数えた数
count=0
count2=0
for i in range(n):
    if s[i]=='W':
        num[i]=count
        num2[i]=e_cnt-count2
        count+=1
    else:
        num[i]=count
        num2[i]=e_cnt-count2 
        count2+=1
    
   
#print(num,num2)

ans=[0]*n

for i in range(n):
    ans[i]=abs(num[i]+num2[i+1])
print(min(ans))

Submission status: Accepted

Solution code:

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

east = [0]*(N+1)
west = [0]*(N+1)


count = 0
for i, s in enumerate(S):
    if s == 'E':
        count+=1
    east[i+1] = count

count = 0
for i, s in enumerate(S):
    if s == 'W':
        count+=1
    west[i+1] = count

minimum = 10**6

for i in range(N):
    tmp = (west[i]-west[0]) + (east[N]-east[i+1])
    #print(tmp)
    if tmp < minimum:
        minimum = tmp

print(minimum)

Submission status: Accepted

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

tmp = S[1:].count('E')
  
minimum = tmp

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

print(minimum)

Submission status: Accepted

Solution code:

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

tmp = 0

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

minimum = tmp

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

print(minimum)

Submission status: Accepted

Solution code:
from itertools import accumulate

def main():
    N = int(input())
    people = list(input())

    wests = list(accumulate([0]+people, lambda total, x: total + (1 if x == "W" else 0)))[1:]
    easts = list(accumulate([0]+people, lambda total, x: total + (1 if x == "E" else 0)))[1:]

    
    def linearsearch():
        easts_total = easts[-1]
        for i in range(N):
            yield (0 if i == 0 else wests[i-1]) + easts_total - easts[i]

    print(min(linearsearch()))

main()

Submission status: Accepted

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

counter = Counter(S)
E = counter.get('E', 0)
W = counter.get('W', 0)
# print('E W', E, W)

ans = 10**6
e, w = 0, 0
for i in range(N):
    v = w + E - e
    if S[i] == 'E':
        v -= 1
        e += 1
    else:
        w += 1
    # print('v, e, w', v, e, w)
    ans = min(ans, v)
print(ans)