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()
 
num_E, num_W = 0, 0
# num_E_list = [0] * (N + 1)
# num_W_list = [0] * (N + 1)
min_sum_operation = N
argmin_i = 0
for i, c in enumerate(S):
    if i == 0:
        # This operation is safe because N is larger than 2
        right = S[i + 1:]
        num_E = right.count('E')
        num_W += 1 if c == 'W' else 0
        # num_E_list[i] = num_E
        # num_W_list[i] = num_W
    elif c == 'W':
        num_W += 1
        # num_W_list[i + 1] = num_W
    elif c == 'E':
        num_E -= 1
        # num_E_list[i] = num_E
    if min_sum_operation > num_E + num_W:
        min_sum_operation = min(min_sum_operation, num_E + num_W)
        argmin_i = i
 
    # print(min_sum_operation, num_E, num_W)
 
if argmin_i == 0:
    print(min_sum_operation - 1)
else:
    print(min_sum_operation)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E = [0] * N
t = 0
for i, s in enumerate(S[len(S)-1:0:-1]):
  if s == 'E':
    t += 1
  E[-i-2] = t

t = 0
for i, s in enumerate(S[:len(S) - 1]):
  if s == 'W':
    t += 1
  E[i+1] += t

print(min(E))

Submission status: Accepted

Solution code:
n = int(input())
members = input()
m_e = members.count("E")
cost = m_e
costs = []
for i in range(n):
    if members[i]=="E":
        cost-=1
    if (i!=0 and members[i-1]=="W"):
        cost+=1
    costs.append(cost)
print(min(costs))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    from collections import Counter, defaultdict
    ri = defaultdict(int, dict(Counter(S)))
    le = defaultdict(int, {'W': 0, 'E': 0})
    pre = ''
    ans = 1 << 60
    for s in S:
        le[pre] += 1
        ri[s] -= 1
        ans = min(ans, le['W'] + ri['E'])
        pre = s
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#                         author:  kagemeka 
#                         created: 2019-11-07 23:33:56(JST)
## internal modules
import sys
import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator
## external modules
# import scipy.special   # if use comb function on AtCoder, 
# import scipy.misc      # select scipy.misc.comb (old version) 

def main():
    n = int(sys.stdin.readline().rstrip())
    s = sys.stdin.readline().rstrip()

    opposite = collections.Counter(s)
    opposite['W'] = 0
    minimum = n
    for i in range(n):
        if s[i] == 'E':
            opposite['E'] -= 1
        minimum = min(minimum, sum(opposite.values()))
        if s[i] == 'W':
            opposite['W'] += 1
    print(minimum)




if __name__ == "__main__":
    # execute only if run as a script
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
cumsum_east = [0 for i in range(N+1)]
cumsum_west = [0 for i in range(N+1)]
for i in range(1,N+1):
    if S[i-1] == 'E':
        cumsum_east[i] = cumsum_east[i-1] + 1
    else:
        cumsum_east[i] = cumsum_east[i-1]
    if S[i-1] == 'W':
        cumsum_west[i] = cumsum_west[i-1] + 1
    else:
        cumsum_west[i] = cumsum_west[i-1]
cumsum_east = cumsum_east[1:]
cumsum_west = cumsum_west[1:]
ans = 1000000
for i in range(N):
    if i == 0:
        flip_num = cumsum_east[-1]-cumsum_east[0]
    elif i == N-1:
        flip_num = cumsum_west[-2]
    else:
        flip_num = cumsum_west[i-1] + cumsum_east[-1] - cumsum_east[i]
    if ans > flip_num:
        ans = flip_num
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
a=S[1:].count('E')
ans=a
t=1
while t<=N-1:
    if S[t-1]=='W':
        a+=1
    if S[t]=='E':
        a-=1
    ans=min(ans,a)
    t+=1
print(ans)

Submission status: Accepted

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

ans = n-1-sufw[1]

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

ans=min(ans,prew[n])

print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = list(input())
rightE = S.count('E')
rightW = S.count('W')
leftE = 0
leftW = 0
ans = N
for n in range(N):
    if S[n] == 'E':
        rightE -= 1
        ans = min(ans,leftW+rightE)
        leftE += 1
    else:
        rightW -= 1
        ans = min(ans,leftW+rightE)
        leftW += 1
print(ans)

Submission status: Accepted

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

turn_east = [0 for x in range(n)]
turn_west = [0 for x in range(n)]
total = [0 for x in range(n)]

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

    if s[n-i] == 'E':
      turn_west[n-1-i] = turn_west[n-i]+1
    else:
      turn_west[n-1-i] = turn_west[n-i]

for j in range(n):
  total[j] = turn_east[j] + turn_west[j]

print(min(total))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
left=[0]*(n+1)
right=[0]*(n+1)
for i in range(n):
    if s[i]=="W":
        left[i+1]=left[i]+1
    else:
        left[i+1]=left[i]

for i in range(n-1,0,-1):
    if s[i]=="E":
        right[i-1]=right[i]+1
    else:
        right[i-1]=right[i]
print(min([left[i]+right[i] for i in range(n)]))

Submission status: Accepted

Solution code:
n=int(input())
l=list(input())
lw=[0]
countw=0
counte=0
le=[0 for i in range(n)]

for i in range(1,n):
  if l[i-1]=="W":
    countw+=1
  lw.append(countw)
  if l[-i]=="E":
    counte+=1
  le[-i-1]=counte
m=n
for i in range(n):
  if m>lw[i]+le[i]:
    m=lw[i]+le[i]
print(m)

Submission status: Accepted

Solution code:
from itertools import accumulate

N=int(input())
s=str(input())


a = accumulate(c=='W' for c in 'X'+s[:-1])
b = reversed(list(accumulate(c=='E' for c in 'X'+s[-1:0:-1])))
print(min(x + y for x, y in zip(a, b)))

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
s = [str(i) for i in s]
d = int(n/2)
w, e = [], []
w.append(1) if s[0] == "W" else w.append(0)
e.append(1) if s[-1] == "E" else e.append(0)
for i in range(1, n):
    w.append(w[i-1] + 1) if s[i] == "W" else w.append(w[i-1])
    e.append(e[i-1] + 1) if s[n-1-i] == "E" else e.append(e[i-1])
e.reverse()
ans = [w[i-1] + e[i+1] for i in range(1, n-1)]
ans.insert(0, e[0])
ans.insert(-1, w[-1])
print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
string = input().rstrip("\n")

facing_west_in_west = 0
facing_east_in_east = string.count("E") - (string[0] == "E")
list_ = [facing_east_in_east]
for i in range(1, n):
    if string[i - 1] == "W":
        facing_west_in_west += 1
    if string[i] == "E":
        facing_east_in_east -= 1
    list_.append(facing_east_in_east + facing_west_in_west)
print(min(list_))

Submission status: Accepted

Solution code:
import sys

#+++++
		
def main():
	a = int(input())
	s = input()
	
	sst=len([c for c in s[1:] if c == 'E'])
	ret=sst
	cc=sst
	for i, c in enumerate(s[:-1]):
		leader = s[i+1]
		if leader == 'E':
			cc -= 1
		
		if c == 'W':
			cc+=1
		#pa(cc)
		ret = min(ret, cc)
	
	
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
S = input()
NE = S.count('E')
ans = cur = NE
prev = S[0]
n = 0
for s in S + 'A':
    if s == prev:
        n += 1
    else:
        if prev == 'E':
            cur -= n
            if cur < ans:
                ans = cur
        else:
            cur += n
        n = 1
        prev = s
print(ans)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
S = input()
NE = S.count('E')
ans = cur = NE
for s in S:
    if s == 'E':
        cur -= 1
        if cur < ans:
            ans = cur
    else:
        cur += 1
print(ans)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

N = int(input())
S = input()
ele, num = get_sequence(S)
Estart = 1-int(ele[0]=='E')
NE = sum(num[Estart::2])
ans = cur = NE
for i in range(len(num)):
    if (i - Estart) % 2:
        cur += num[i]
    else:
        cur -= num[i]
        if cur < ans:
            ans = cur
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
mini=10**9
prew=[0]*n 
if s[0]=='W':
    prew[0]+=1 
pre=[0]*n 
if s[n-1]=='E':
    pre[n-1]+=1 
for i in range(1,n):
    #pre[i]=pre[i-1]+(s[i]=='E')
    prew[i]=prew[i-1]+(s[i]=='W')
for i in range(n-2,-1,-1):
    pre[i]=pre[i+1]+(s[i]=='E')
for i in range(1,n-1): 
    cost=prew[i-1]+pre[i+1]
    mini=min(mini,cost)
mini=min(mini,pre[0])
mini=min(mini,prew[n-1])
print(mini)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 変える必要がある人を数える

def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

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

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

        ans = min(ans, w_count + e_count)
    print(ans)
    


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

C = S.count("E")
M = C

for i in S:
    if i == "E":
        C -= 1
    else:
        C += 1
    M = min(C,M)
print(M)

Submission status: Accepted

Solution code:
import numpy as np

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

W=[0 for i in range(N)] # W[i]=(S[i]より西で西を向いている人の数)
E=[0 for i in range(N)] # E[i]=(S[i]より東で東を向いている人の数)

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

print(min([x for x in np.array(W)+np.array(E)]))

Submission status: Accepted

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

C = S.count("E")
M = C

for i in S:
    if i == "E":
        C -= 1
    else:
        C += 1
    M = min(C,M)
print(M)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 10**10
rightW = s.count('W')
rightE = len(s) - rightW
leftW = 0
leftE = 0

for i in range(len(s)):
    if s[i] == 'W':
        rightW -= 1
    else:
        rightE -= 1

    if i > 0:
        if s[i-1] == 'W':
            leftW += 1
        else:
            leftE += 1
    ans = min(ans, rightE + leftW)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 10**10
# wCnt = s.count('W')
# eCnt = len(s) - wCnt
rightW = s.count('W')
rightE = len(s) - rightW
leftW = 0
leftE = 0


for i in range(len(s)):
    # right = s[i+1:]
    # left = s[:i]

    if s[i] == 'W':
        rightW -= 1
    else:
        rightE -= 1

    if i > 0:
        if s[i-1] == 'W':
            leftW += 1
        else:
            leftE += 1
    temp = rightE + leftW
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

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

W = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    W.append(w)

e = 0
for i, si in enumerate(S[::-1]):
    if si == 'E':
        e += 1
    ans = min(ans, W[-1 * i - 1] + e - 1)

print(ans)

Submission status: Accepted

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

A = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    A.append(w)

e = 0
for i, si in enumerate(S[::-1]):
    if si == 'E':
        e += 1
    ans = min(ans, A[-1 * i - 1] + e - 1)

print(ans)

Submission status: Accepted

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

A = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    A.append([w])

# E = []
e = 0
for i, si in enumerate(S[::-1]):
    if si == 'E':
        e += 1
    # E.append(e)
    A[-1 * i - 1].append(e)

# E = E[::-1]
# for i in range(N):
#     A[i][1] = E[i]

ans = N
for ai in A:
    ans = min(ans, ai[0] + ai[1] - 1)
print(ans)

Submission status: Accepted

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

A = []
w = 0
for si in S:
    if si == 'W':
        w += 1
    A.append([w, 100])

E = []
e = 0
for si in S[::-1]:
    if si == 'E':
        e += 1
    E.append(e)

E = E[::-1]
for i in range(N):
    A[i][1] = E[i]

ans = N
for ai in A:
    ans = min(ans, ai[0] + ai[1] - 1)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = S.count('E')
if S[0] == 'E':
    cnt -= 1
ans = cnt
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())

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

R_count = {}
R_count[N-1] = 0
for i in range(N-2,-1,-1):
    if S[i+1] == "E":
        R_count[i] = R_count[i+1] + 1
    else:
        R_count[i] = R_count[i+1]

leader_i = 0
turn_num = N+1

for i in range(N):
    #print(L_count[i] + R_count[i],"i is",i,"leader_i is",leader_i)
    if L_count[i] + R_count[i] < turn_num:
        leader_i = i
        turn_num = L_count[i] + R_count[i]


R_sort = sorted(R_count.items(), key=lambda x: x[0])
#print(L_count)
#print(R_sort)
#print(leader_i)
print(L_count[leader_i] + R_count[leader_i])

Submission status: Accepted

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

diff = []
e = 0
w = 0
for si in S:
    if si == 'E':
        e += 1
    else:
        w += 1
    diff.append(e - w)

i = diff.index(max(diff))
# print(i)

ans = S[:i].count('W') + S[i+1:].count('E')
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(str, input()))
ret = float("inf")
west = [0 for _ in range(N + 2)]
east = [0 for _ in range(N + 2)]

for i in range(1, N + 1):
	if S[i - 1] == "W":
		west[i] = west[i - 1] + 1
	else:
		west[i] = west[i - 1]
for i in range(N, 0, -1):
	if S[i - 1] == "E":
		east[i] = east[i + 1] + 1
	else:
		east[i] = east[i + 1]
	ret = min(east[i] + west[i] - 1, ret)

print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

e = [0] * (N+1)
w = [0] * (N+1)

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

ans = N+1
for i in range(N):
   if e[i+1] + w[i] < ans:
       ans = e[i+1] + w[i]

print(ans)

Submission status: Accepted

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

numlist=[1 for i in range(N)]

if S=="E"*N or S=="W"*N:
    numlist[0]=0
else:
    num_0=0
    for i in range(1,N):
        if S[i]=="E":
            num_0+=1
    numlist[0]=num_0

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

print(max(min(numlist),0))

Submission status: Accepted

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

Submission status: Accepted

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

N=int(input())
S=input()
S=list(S)
East_number=0
East=[]
for i in S:
  if i=='E':
    East_number+=1
  East.append(East_number)
number=0
ans=[]
for i in range(N):
  if i >0:
    number+=i-East[i-1]
  number+=East[N-1]-East[i]
  ans.append(number)
  number=0
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
w_list = [0] * (N + 2) 
e_list = [0] * (N + 2) 
cnt = 0
for i in range(N):
  if S[i] == "W":
    cnt += 1
  w_list[i + 1] = cnt
cnt = 0
for i in range(N - 1, -1, -1):
  if S[i] == "E":
    cnt += 1
  e_list[i + 1] = cnt
ans = 10 ** 9
for i in range(1, N + 1):
  ans = min(ans, w_list[i - 1] + e_list[i + 1])
print(ans)

Submission status: Accepted

Solution code:
INF = 1e9
n = int(input())
s = list(input())
sum_1 = [0]*(n+1)
for i in range(n):
    if s[i]=="E":
        sum_1[i+1] = sum_1[i]+1
    else:
        sum_1[i+1] = sum_1[i]

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
L = [0 for j in range(n)] # L[j] => W
R = [0 for j in range(n)]
if s[0] == "W":
    L[0] = 1
if s[-1] == "W":
    R[-1] = 1
for i in range(1,n):
    L[i] = L[i - 1]
    R[-(i + 1)] = R[-i]
    if s[i] == "W":
        L[i] += 1
    if s[-(i + 1)] == "W":
        R[-(i + 1)] += + 1
m = min(n - 1 - R[1], L[-2])
for i in range(1,n - 1):
    k = L[i- 1] + (n - i - 1) - R[i + 1]
    if m > k:
        m = k
print(m)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cnt=s.count('E') #西から0番目の人から見たときの向きを変える人数
m=cnt

#西からi番目の人がリーダーの時に向きを変える人の増減について
for i in range(n):
  if s[i]=='E':
    cnt-=1 
  else:
    cnt+=1
  m=min(m,cnt)
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

n = int(input())
d = input()

pre = d[1:].count('E')
minimum = pre
for i in range(1,n):
    if d[i-1]=='W':
        pre = pre+1
    if d[i]=='E':
        pre = pre-1
    minimum = min(pre, minimum)

print(minimum)

Submission status: Accepted

Solution code:


def resolve():
    n=int(input())
    s=[i for i in input()]
    count=0
    l1=[0 for i in range(n)]
    for i in range(n):
        l1[i]=count
        if s[i]=='W':
            count+=1
    count=0
    l2=[0 for i in range(n)]
    for i in range(n-1,-1,-1):
        l2[i]=count
        if s[i]=='E':
            count+=1
    print(min([l1[i]+l2[i] for i in range(n)]))



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    s = input()

    lw = [0]*n
    le = [0]*n
    rw = [0]*n
    re = [0]*n

    for i in range(n-1):
        if s[i] == 'W':
            lw[i+1] = lw[i] + 1
            le[i+1] = le[i]
        else:
            lw[i+1] = lw[i]
            le[i+1] = le[i] + 1

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

    ans = INF

    for i in range(n): 
        #print([lw[i], re[i]] , [le[i], rw[i]])

        ans = min(ans, lw[i]+re[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
S=list(input())

all=len(S)
all_E=S.count("E")
all_W=S.count("W")
E=0
W=0

eva=10**12
ans=0
for index,item in enumerate(S):
    temp=W+all_E-E
    eva=min(eva,temp)
    if eva==temp:
        ans=index
    if item=="E":
        E+=1
    else:
        W+=1

temp=W+all_E-E
eva=min(eva,temp)

#print(ans)
print(eva)

Submission status: Accepted

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

Submission status: Accepted

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

e=[0]*n
w=[0]*n

for i in range(n-1):
    if l[i]=='E':
        e[i+1]=e[i]+1
    else:
        e[i+1]=e[i]
    if l[n-1-i]=='W':
        w[n-2-i]=w[n-1-i]+1
    else:
        w[n-2-i]=w[n-1-i]
ew=[0]*n
for i in range(n):
    ew[i]=e[i]+w[i]
print(n-1-max(ew))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
memoW = [0 for i in range(n)]
memoE = [0 for i in range(n)]
countW = 0
countE = 0
for i in range(1,n):
    if s[i-1]=='W':
        countW +=1
    memoW[i] = countW
for i in range(n-2,-1,-1):
    if s[i+1]=='E':
        countE +=1
    memoE[i] =countE
ans = n
for i in range(n):
   ans = min(memoW[i]+memoE[i],ans) 
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
way = [0]*(n+1)
for i in range(n):
    if s[i] == "E":
        way[i+1] = way[i]+1
    else:
        way[i+1] = way[i]
key = way[-1]
ans = 10000000
for j in range(n):
    sub = j - way[j]-way[j+1] + key
    if sub < ans:
        ans = sub
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w_num=[0]*n
e_num=[0]*n
for i,c in enumerate(s[:-1]):
    w_num[i+1] = w_num[i] + (c=='W')
for i,c in enumerate(s[:0:-1]):
    e_num[n-2-i] = e_num[n-1-i] + (c=='E')
print(min(w + e for w,e in zip(w_num,e_num)))

Submission status: Accepted

Solution code:
import numpy as np

def test():
    n = int(input())
    s = input()
    w = [0]*n
    e = [0]*n
    ans = float("inf")
    for i in range(n):
        if s[i] == "W":
            w[i] = 1
        else:
            e[i] = 1
    w = np.cumsum(w)
    e = np.cumsum(e)

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


if __name__ == "__main__":
    test()

Submission status: Accepted

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

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

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

print(min(cws[i] + ces[i] for i in range(N)))

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
    リーダーに向いてない人の最小
    (W in 0~i) + (E in i+2~n-1)
"""
from collections import Counter as count
n = int(input())
s = input()
turn = count(s[1:])['E']
least = turn

for i in range(n-1):
    if s[i] == 'W':
        turn += 1
    if s[i+1] == 'E':
        turn -= 1
    if turn < least:
        least = turn

print(least)

Submission status: Accepted

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

W = [0] *N
E = [0] *N
L = [0] *N
if S[0] == "W": W[0] = 1
if S[-1] == "E": E[-1] = 1

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

Submission status: Accepted

Solution code:
n = int(input())
S = input()
E = [0]*n
W = [0]*n
ans = n
for i in range(n):
  if S[i]=='E':
    E[i] = 1
  else:
    W[i] = 1

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

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

Submission status: Accepted

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

E_cnt = S.count('E')
S_cnt = 0

min_cnt = 10 ** 6
for i in range(N):
    cnt = 0
    if S[i] == 'E':
        E_cnt -= 1
        cnt = S_cnt + E_cnt
    else:
        cnt = S_cnt + E_cnt
        S_cnt += 1
    min_cnt = min(min_cnt, cnt)

print(min_cnt)

Submission status: Accepted

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

E_cnt = S.count('E')
S_cnt = 0
l_change_cnt = 0

min_cnt = 10 ** 6
for i in range(N):
    cnt = 0
    if S[i] == 'E':
        E_cnt -= 1
        cnt = S_cnt + E_cnt
    else:
        cnt = S_cnt + E_cnt
        S_cnt += 1
    min_cnt = min(min_cnt, cnt)

print(min_cnt)

Submission status: Accepted

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

pos = 0
minpos = 0
mini = 0

for i in range(N):
  if S[i] == "W":
    pos += 1
  else:
    pos -= 1
  if pos < minpos:
    minpos = pos
    mini = i

s1 = S[0:mini].count("W")
s2 = S[mini+1:N].count("E")
print(s1+s2)

Submission status: Accepted

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

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

# 反対方向の人を数え上げ
for i in range(N):
  if S[i] == "E":
    E[i] = 1
  else:
    W[i] = 1
    
# リーダーより前を累積和していく
for i in range(1, N):
  W[i] += W[i-1]
  E[i] += E[i-1]

ans = float("inf")  
for i in range(N):
  sm = 0
  if 0 < i:
    sm += W[i-1]
  sm += E[N-1] - E[i]
  ans = min(ans, sm)
    
print(ans)

Submission status: Accepted

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

west = [0]*n
east = [0]*n

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

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

change = [0]*n
for i in range(n):
  if i==0:
    change[i] = east[1]
  elif i== n-1:
    change[i] = west[-2]
  else:
    change[i]= (west[i-1] + east[i+1])

print(min(change))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input().strip()
A = [[0,0] for _ in range(N+1)]
for i in range(N):
    if S[i]=="E":
        A[i+1][0] = A[i][0]+1
        A[i+1][1] = A[i][1]
    else:
        A[i+1][0] = A[i][0]
        A[i+1][1] = A[i][1]+1
cmin = N
for i in range(N):
    a,b = A[i][0],A[i][1]
    c,d = A[N][0]-A[i+1][0],A[N][1]-A[i+1][1]
    cmin = min(cmin,b+c)
print(cmin)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
cnt_W=0
cnt_E=0
arr_W=[]
arr_E=[]
for s in S:
    if s=='W':
        cnt_W+=1
    arr_W.append(cnt_W)

for s in S[::-1]:
    if s=='E':
        cnt_E+=1
    arr_E.append(cnt_E)

import numpy as np

print((np.array(arr_W)+np.array(arr_E[::-1])).min()-1)

Submission status: Accepted

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

le, lw, re, rw = 0, 0, S.count('E'), S.count('W')
ret = float('inf')
for s in S :
    if s == 'E' :
        re -= 1
        ret = min(ret, lw + re)
        le += 1
    else :
        rw -= 1
        ret = min(ret, lw + re)
        lw += 1
print(ret)