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 numpy as np
n = int(input())
s = list(input())
west = np.array(list(map(lambda x: 1 if(x == "W") else 0, s)))
east = np.array(list(map(lambda x: 1 if(x == "E") else 0, s))[::-1])
west_amount = np.cumsum(west)
east_amount = np.cumsum(east)[::-1]
res = []
for i in range(n):
  res.append(west_amount[i] + east_amount[i] - 1)
print(min(res))

Submission status: Accepted

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

west = [0] * (n + 2)
east = [0] * (n + 2)

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

for i,v in enumerate(reversed(s)):
    east[n - i] = east[n + 1 - i]
    if v == 'E':
        east[n - i] += 1

min_count = None
for i in range(n):
    if min_count is None:
        min_count = west[i] + east[i + 2]
    else:
        min_count = min(min_count, west[i] + east[i + 2])

print(min_count)

Submission status: Accepted

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

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

for i in range(1, n):
  if s[i] != s[i-1]:
    turns.append(turns[i-1])
  elif s[i] == 'E':
    turns.append(turns[i-1] - 1)
  else:
    turns.append(turns[i-1] + 1)
  
print(min(turns))

# E E で1減少
# W W で1増加
# E W, W E は変化なし

Submission status: Accepted

Solution code:
n = int(input())
s = input()
sum_w = s.count("W")
ans = n
lw = [0]*n
count = 0
for i in range(n):
    lw[i] = count
    if s[i] == "W":
        count += 1
for i in range(n):
    left_w = lw[i]
    right_w = sum_w-left_w
    if s[i] == "W":
        right_w -= 1
    right_e = (n-i-1)-right_w
    sum = left_w+right_e
    if sum < ans:
        ans = sum
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    max_count = 0
    totE, totW = 0, 0
    for c in S:
        if c == "E": totE += 1
        else: totW += 1

    countE, countW = 0, 0
    min_flip = float('inf')
    for i in range(N):
        f = countW  + totE-(S[i]=="E")- countE
        min_flip = min(min_flip, f)
        if S[i] == "E": countE += 1
        else: countW += 1

    return min_flip

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

Submission status: Accepted

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

Submission status: Accepted

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

t_w = [0]*N
t_e = [0]*N

ans = 3*10**6
west = 0
east = 0

for i in range(N):
    if i == 0:
        if S[i] == "W":
            t_w[0] = 1 
            t_e[0] = 0
        elif S[i] == "E":
            t_w[0] = 0 
            t_e[0] = 1 
    else:
        if S[i] == "W":
            t_w[i] = t_w[i-1] + 1
            t_e[i] = t_e[i-1]
        elif S[i] == "E":
            t_w[i] = t_w[i-1]
            t_e[i] = t_e[i-1] + 1

for i in range(N):
    west = 0 if i == 0 else t_w[i-1]
    east = t_e[N-1]-t_e[i]
    t = west + east
    ans = min(ans,t)
print(ans)

Submission status: Accepted

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

ans = N

d = [0] * N

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

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

print(min(d))

Submission status: Accepted

Solution code:

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n=getint()
s=input()

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

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

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

print(res)

Submission status: Accepted

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import sys
import math

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

wl = [0] * (N + 2)
er = [0] * (N + 2)

for i in range(N):
    wl[i + 1] = int(S[i] == 'W') + wl[i]
    er[N - i] = int(S[N - 1 - i] == 'E') + er[N - i + 1]

ans = N

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

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
E = [0]*N
W = [0]*N
min = 10**9
for i in range(N):
    if S[i]=="E":
        E[i] += 1+E[i-1]
        W[i] = W[i-1]
    elif S[i]=="W":
        W[i] += 1+W[i-1]
        E[i] = E[i-1]
if S[0]=="W":
    W.insert(0,0)
    W.pop(-1)
for j in range(N):
    tmp = E[-1]-E[j]+W[j]
    if min>tmp:
        min = tmp
print(min)

Submission status: Accepted

Solution code:

N = int(input())
A = list(input())

L = [0] * N
R = [0] * N
L[0] = 0
R[N-1] = 0
for i in range(1, N):
    count = 1 if A[i-1] == 'W' else 0
    L[i] = L[i-1] + count

    count = 1 if A[N-i] == 'E' else 0
    R[N-i-1] = R[N-i] + count

print(min(list(map(lambda a, b: (a+b), L, R))))

Submission status: Accepted

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

Submission status: Accepted

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

c_e=0

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

cnt=[]
c_w=0
cnt.append(c_e)

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

print(min(cnt))

Submission status: Accepted

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

Submission status: Accepted

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

score = [0] * N
# i番目の人がリーダーの時の、W->Eの変化を数える
counter = 0
for i in range(N):
    score[i] += counter
    if S[i] == 'W':
        counter += 1

# i番目の人がリーダーの時の、E->Wの変化を数える
counter = 0
for i in range(N):
    i = N - i - 1
    score[i] += counter
    if S[i] == 'E':
        counter += 1

print(min(score))

Submission status: Accepted

Solution code:
N = int(input())
s = list(input())
e_count =s.count("E")
w_count =s.count("W")
temp = s[1:].count("E")
ans = 10**6
if temp <= ans:
    ans = temp
for i in range(N-1):
    if s[i] == "E" and s[i+1] == "E":
        temp -= 1
    elif s[i] == "W" and s[i+1] == "E":
        pass
    elif s[i] == "E" and s[i+1] == "W":
        pass
    else:
        temp += 1

    if temp <= ans:
        ans = temp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ws=sum([1 for i in s if i=="E"])
s="E"+s
count=[0]*n

for i in range(n):
    now=s[i+1]
    before=s[i]
    
    if now=="E":
        ws-=1
    if before=="W":
        ws+=1
    count[i]=ws
print(min(count))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
en = s.count("E")
es = n - en
cnt = ans = en - (s[0] == "E") * 1
for i in range(1, n):
    if s[i] == "E": cnt -= 1
    if s[i - 1] == "W": cnt += 1
    if cnt < ans:
        ans = cnt
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
count_w = 0
count_e = 0
count_list = [[0] * 2 for i in range(n)]
min_num = 100000000

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

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

for i in range(n):
    num = count_list[i][0] + count_list[i][1]
    if num <= min_num:
        min_num = num

print(min_num)

Submission status: Accepted

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

s2 = list(reversed(s1))

s1s = [0]*(n+1)
s2s = [0]*(n+1)

for i in range(n):
  if s1[i] == 'W':
    s1s[i+1] = s1s[i]+1
  else:
    s1s[i+1] = s1s[i]
  
  if s2[i] == 'E':
    s2s[i+1] = s2s[i]+1
  else:
    s2s[i+1] = s2s[i]

min_s = 3*10**5+1
for j in range(n):
  min_s = min(min_s, s1s[j]+s2s[n-1-j])
print(min_s)

Submission status: Accepted

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

L = [0] * (N + 1)  # 西向いてる人の累積(0から)
R = [0] * (N + 1)  # 東向いてる人の累積(Nから)

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

cnt = 0
for i in reversed(range(N)):
    if S[i] == 'E':
        cnt += 1
    R[i] = cnt

ans = 300000
for i in range(N):
    if i == 0:
        ans = R[i + 1]
    else:
        ans = min(ans, L[i-1] + R[i+1])

print(ans)

Submission status: Accepted

Solution code:
def main():
    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:
            W[i + 1] = W[i]
            E[i + 1] = E[i] + 1

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

main()

Submission status: Accepted

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

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


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


ans=n
for i in range(n):
    ans = min(ans, left_attention[i]+right_attention[i])


print(ans)

Submission status: Accepted

Solution code:
import copy

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

lst = []
lst.append({"W": 0, "E": 0})
for i in range(1,len(S)+1):
  res = {"W": 0, "E": 0}
  if S[i-1] == "W":
    res["W"] += lst[i-1]["W"] + 1
    res["E"] += lst[i-1]["E"]
  else:
    res["W"] += lst[i-1]["W"]
    res["E"] += lst[i-1]["E"] + 1
  lst.append(res)
ans = N
for i in range(1,len(lst)):
  n = 0
  if (i != len(lst)-1):
    n = lst[i-1]["W"] + (lst[-1]["E"] - lst[i]["E"])
  else:
    n = lst[i-1]["W"]
  if (n < ans):
    ans = n
print(ans)

Submission status: Accepted

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

cntE = [0] * N
cntW = [0] * N

if S[0] == 'E' :
    cntE[0] = 1
if S[N-1] == 'W' :
    cntW[N-1] = 1

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

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

#print(cntE)
#print(cntW)
cntSum = list(map(sum, zip(cntE, cntW)))
#print(cntSum)
print(N - max(cntSum))

Submission status: Accepted

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

def counter(ss,direction):
    l = []
    cnt = 0
    l.append(cnt)
    for s in ss[:-1]:
        if s == direction:
            cnt += 1
        l.append(cnt)
        
    return l

wtoe_cnt = counter(ss,"W")
etow_cnt = counter(ss[::-1],"E")[::-1]

cnt = 10**9
for wtoe,etow in zip(wtoe_cnt,etow_cnt):
    tmp_cnt = wtoe+etow
    if tmp_cnt < cnt:
        cnt = tmp_cnt
        
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

S = input()

re = [0] * N # 右からの累積和
le = [0] * N # からの累積和
INF = float("inf")
for i in range(1,N):
    
    if S[i-1] == "E":
        le[i] = le[i-1] + 1
    else:
        le[i] = le[i-1]

    if S[N-i] == "E":
        re[N-1-i] = re[N-i] + 1
    else:
        re[N-1-i] = re[N-i]

ans = [INF] * N

for i in range(N):
    ans[i] = (i - le[i]) + (re[i])

print(min(ans))

Submission status: Accepted

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

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

tmp = []
for i in range(n):
  tmp.append(w[i]+e[i])
print(min(tmp))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
A=[0]*N
B=[0]*N
count=0
for i in range(N):
    if S[i]=='W':
        count+=1
    A[i]=count
cnt=0

for i in range(N)[::-1]:
    if S[i]=='E':
        cnt+=1
    B[i]=cnt
mini=10000000
if mini>B[1]:
    mini=B[1]
if mini>A[N-2]:
    mini=A[N-2]

for i in range(1,N-1):
    if mini>(A[i-1]+B[i+1]):
        mini=A[i-1]+B[i+1]

print(mini)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left_attention = [0 for i in range(N)]
right_attention = [0 for i in range(N)]

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
ans = n
num_list = []
W_count = 0

for i in range(n):
    if S[i] == 'W':
        W_count += 1
    num_list.append(W_count)

for i in range(1, n-1):
    left_w = num_list[i-1]
    right_e = n - num_list[n-1] - (1+i - num_list[i])
    pre_ans = left_w + right_e
    if ans > pre_ans:
        ans = pre_ans

ans_0 = n-num_list[n-1] - (1-num_list[0])
ans_n = num_list[n-2]
print(min(ans, ans_0, ans_n))

Submission status: Accepted

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

sumW = [0 for _ in range(N + 1)]
sumE = [0 for _ in range(N + 1)]

for i, s in enumerate(S) :
    sumW[i + 1] = sumW[i]
    sumE[i + 1] = sumE[i]
    if s == 'W' :
        sumW[i + 1] += 1
    else :
        sumE[i + 1] += 1

ans = float('inf')
for i, s in enumerate(S) :
    count = sumW[i] + (sumE[N] - sumE[i + 1])
    ans = min(ans, count)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
S = (stdin.readline().rstrip())
w = [0] * N
e = [0] * N

W_c = 0
E_c = 0
for ind,i in enumerate(S):
    if i == "E":
        E_c += 1
    
    e[ind] = E_c

for ind,i in enumerate(reversed(S)):
    if i == "W":
        W_c += 1
   
    w[ind] = W_c

w = list(reversed(w))
ans = [0] * N
w_end = w[0]
e_end = e[-1]

for j in range(N):
    ans[j] = (e_end - e[j]) + (w_end - w[j])
print(min(ans))

Submission status: Accepted

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

W = [0 for k in range(N)]
E = [0 for k in range(N)]

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

for k in range(1,N):
    if S[N-k] == "E":
        E[N-k-1] = E[N-k] + 1
    else:
        E[N-k-1] = E[N-k]

ans = 10**9
for k in range(N):
    ans = min(W[k]+E[k],ans)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [x for x in input()]
start = len([1 for i in s[1:] if i=='E'])
dp = [0]*n
ans =start
for i,j in enumerate(s):
	if i ==0:
		now=start
		continue
	if j =='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 = list(input())
mi =[0]*N

LE = 0
LW = 0
RE = S[1:N].count("E")
RW = N-1-RE
mi[0] = RE

if S[0] == "E":
    LE +=1
else:
    LW +=1

for i in range(1,N-1):
    if S[i]=="E":
        RE -=1
        mi[i]= LW + RE 
        LE +=1
    elif S[i]=="W":
        RW -=1
        mi[i]=LW +RE
        LW +=1

mi[N-1] = S[0:N-1].count("W")
print(min(mi))

Submission status: Accepted

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

LE = 0
LW = 0
RE = S[1:N].count("E")
RW = N-1-RE
ans = RE

if S[0] == "E":
    LE +=1
else:
    LW +=1

for i in range(1,N-1):
    if S[i]=="E":
        RE -=1
        if LW + RE < ans:
            ans = LW +RE
        LE +=1
    elif S[i]=="W":
        RW -=1
        if LW +RE < ans:
            ans = LW+RE
        LW +=1

if S[0:N-1].count("W")<ans:
    ans = S[0:N-1].count("W")
print(ans)

Submission status: Accepted

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

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

for i in range(n,0,-1):
  e[i-1]=e[i]
  if (s[i-1]=='E'):
    e[i-1]+=1
    
a=2*n
for i in range(n+1):
  a=min(a,w[i]+e[i])
print(a)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
can = []
for lead in range(n):
  now = 0
  try:
    if s[lead-1] == 'W' and s[lead] == 'W':
      now = can[-1] + 1
    elif s[lead-1] == 'W' and s[lead] == 'E':
      now = can[-1]
    elif s[lead-1] == 'E' and s[lead] == 'W':
      now = can[-1]
    else:
      now = can[-1] - 1
  except:
    #lead==0
    for i in range(1,n):
      if s[i] == 'E':
        now += 1
  can.append(now)
print(min(can))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
e=s.count("E")
w=0
ans=e+w
for si in s:
    if si=="E":
        e-=1
    else:
        w+=1
    ans=min(ans,w+e)
print(ans)

Submission status: Accepted

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

cnt = [0] * N
if S[0] == "W":
    cnt[0] = 1

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

ans = 3*10**5
for i in range(N):
    temp = 0
    # 基準より左側の人
    if i != 0:
        temp += cnt[i-1]
    # 基準より右側の人
    if i != N-1:
        temp = temp + ((N-1) - i) - (cnt[N-1] - cnt[i])
    if ans > temp:
        ans = temp
print(ans)

Submission status: Accepted

Solution code:
"""
WEEWW
<>><<
01100
10100
01000
10000
10010

下記の和
1. リーダーより左にいるWの数
2. リーダーより右にいるEの数
"""

N = int(input())
S = input()
# 1.
cntW = 0
W_num = [0]*N
for i,s in enumerate(S):
    if s == "W":
        cntW += 1
    W_num[i] = cntW
        
# 2. 
cntE = 0
E_num = [0]*N
for i,s in enumerate(S[::-1]):
    if s == "E":
        cntE +=1 
    E_num[i] = cntE
E_num = E_num[::-1]

ans = 10**10
for e,w in zip(E_num, W_num):
    cnt = e + w - 1
    ans = min(cnt, ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = N+1
W_cnt = S.count('W')
left_W_cnt = 0
right_W_cnt = W_cnt
E_cnt = S.count('E')
left_E_cnt = 0
right_E_cnt = E_cnt

for i in range(0, N):
    if S[i] == 'W':
        left_W_cnt += 1
        ans = min(ans,(left_W_cnt-1 + right_E_cnt))
    else:
        right_E_cnt -= 1
        ans = min(ans,(left_W_cnt + right_E_cnt))
    #print(left_W_cnt,right_E_cnt)
print(ans)

Submission status: Accepted

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

# west to east
num = 0
w2e = [0] * len(s)
prev = s[0]
for ix in range(1, len(s)):
    if prev == 'W':
        num += 1
        w2e[ix] = num
    else:
        w2e[ix] = num
    
    prev = s[ix]

# print("w2e", w2e)

# east to west
num = 0
e2w = [0] * len(s)
prev = s[len(s)-1]

for ix in range(len(s)-2, -1, -1):
    if prev == 'E':
        num += 1
        e2w[ix] = num
    else:
        e2w[ix] = num

    prev = s[ix]

# print("e2w", e2w)

# solve

minv = 2**32
for ix in range(0, n):
    cand = w2e[ix] + e2w[ix]
    if minv > cand:
        minv = cand

print(minv)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools


def solve(N: int, S: str):
    E = []
    W = []
    e, w = 0, 0
    for i in range(N):
        if S[i] == 'W':
            w += 1
        W.append(w)
        if S[N-1-i] == 'E':
            e += 1
        E.append(e)
    mn = 1e6
    for i in range(N):
        mn = min(mn, E[i] + W[N-1-i] - 1)
    print(mn)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
n = int(input())
s = input()

w_num = [0] * (n+1)
e_num = [0] * (n+1)
e = s.count('E')

for i in range(n):
    if s[i] == 'W':
        w_num[i+1] = w_num[i] + 1
    else:
        w_num[i+1] = w_num[i]
        
    if s[i] == 'E':
        e_num[i+1] = e_num[i] + 1
    else:
        e_num[i+1] = e_num[i]


ans = n

for i in range(n):
    cnt = w_num[i] + (e - e_num[i+1])
    if cnt < ans:
        ans = cnt

print(ans)

Submission status: Accepted

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

sumsW = [0]
sumsE = [0]

cnt = 0
for i in range(N):
    if S[i] == 'W':
        cnt += 1
    sumsW.append(cnt)

cnt = 0
for j in range(N-1, -1, -1):
    if S[j] == 'E':
        cnt += 1
    sumsE.append(cnt)
sumsE.reverse()

ans = 3 * 10**5
for i in range(N+1):
    ans = min(sumsW[i] + sumsE[i], ans)
    
print(ans)

Submission status: Accepted

Solution code:
_=input()
q=input()
ans=min(q[1:].count("E"),q[:-1].count("W"))
left=0
right = q.count("E")
for i in q:
	if i=="E":
		right-=1
	else:
		left+=1
	ans=min(ans,right+(left!=0)*left)
print(ans)

Submission status: Accepted

Solution code:
import sys

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

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

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

count = min(ans, count)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# print(N, S)
ans = N

f = S[0]
s = S[1:]

nw = 0
ne = 0

for w in s:
    if w != 'W':
        ne += 1

ans = nw+ne
for i in range(1, N):
    # skipflg = False
    # f = S[0: i]
    # l = S[i]
    # s = S[i+1:]
    # # print(i, f, l, s)
    # nw = f.count('W')
    # ne = s.count('E')
    # print(nw, ne)

    if S[i-1] != 'E':
        nw += 1

    if S[i] != 'W':
        ne -= 1
    # print(nw, ne)
    ans = min(ans, nw+ne)

print(ans)

Submission status: Accepted

Solution code:
def solve():
    s = read()
    result = think(s)
    write(result)


def read():
    n = read_int(1)[0]
    return read_line(n=n)


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(s):
    accumulated_count_facing_east, accumulated_count_facing_west = preprocess(s)
    member_count_needed_to_change_direction = len(s)  # as initial value

    n = len(s)

    for i in range(len(s)):
        if i == 0:
            change_to_west = accumulated_count_facing_east[n - 1] - accumulated_count_facing_east[0]
            member_count_needed_to_change_direction = min(change_to_west, member_count_needed_to_change_direction)
        elif i == len(s) - 1:
            change_to_east = accumulated_count_facing_west[n - 2]
            member_count_needed_to_change_direction = min(change_to_east, member_count_needed_to_change_direction)
        else:
            change_to_east = accumulated_count_facing_west[i - 1]
            change_to_west = accumulated_count_facing_east[n - 1] - accumulated_count_facing_east[i]
            member_count_needed_to_change_direction = min(change_to_east + change_to_west, member_count_needed_to_change_direction)
    return member_count_needed_to_change_direction


def preprocess(s):
    east_symbol = 'E'
    west_symbol = 'W'
    accumulated_count_facing_east = [0 for x in range(len(s))]
    accumulated_count_facing_west = [0 for x in range(len(s))]
    for i, elem in enumerate(s):
        if i == 0:
            if elem == east_symbol:
                accumulated_count_facing_east[i] = 1
            else:
                accumulated_count_facing_west[i] = 1
        else:
            if elem == east_symbol:
                accumulated_count_facing_east[i] = accumulated_count_facing_east[i - 1] + 1
                accumulated_count_facing_west[i] = accumulated_count_facing_west[i - 1]
            else:
                accumulated_count_facing_east[i] = accumulated_count_facing_east[i - 1]
                accumulated_count_facing_west[i] = accumulated_count_facing_west[i - 1] + 1
    return accumulated_count_facing_east, accumulated_count_facing_west

def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

l = [0]
r = [0]

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

for i in s[::-1]:
    if i == "E":
        r.append(r[-1] + 1)
    else:
        r.append(r[-1])

l = l[1:]
r = r[1:][::-1]

ans = 10 ** 9

for i in zip(l, r):
    ans = min(sum(i), ans)

print(ans-1)

Submission status: Accepted

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

Submission status: Accepted

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

tmp = s[1:].count("E")
ans = min(10**6,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:
#!/usr/bin/env python3
import sys


def solve(N: int, S: str):
    lw = [0] * N
    re = [0] * N

    if(S[0] == 'W'):
        lw[0] = 1
    if(S[N-1] == 'E'):
        re[N-1] = 1

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

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

    # print(lw)
    # print(re)

    ans = 1000000000
    for i in range(N):
        if(i == 0):
            ans = min(ans, re[i+1])
        elif(i == N-1):
            ans = min(ans, lw[i-1])
        else:
            ans = min(ans,lw[i-1]+re[i+1])

    print(ans)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
n = int(input())
S = input()

west = [0]*(n+2)
east = [0]*(n+2)
ans = 10**6
for i in range(1,n+1):
	if S[i-1] == "W":
		west[i] = west[i-1]+1
	else:
		west[i] = west[i-1]
	if S[-i] == "E":
		east[-i-1] = east[-i]+1
	else:
		east[-i-1] = east[-i]

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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