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

Submission status: Accepted

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

cost = S.count("E")

ans = 1000000
for si in S:
    if si == "E":
        cost -= 1
    else:
        if cost < ans:
            ans = cost
        cost += 1
else:
    if cost < ans:
        ans = cost
print(ans)

Submission status: Accepted

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

cost = list(S).count("E")

ans = 1000000
for si in S:
    if si == "E":
        cost -= 1
    else:
        if cost < ans:
            ans = cost
        cost += 1
else:
    if cost < ans:
        ans = cost
print(ans)

Submission status: Accepted

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

cost = list(S).count("E")

ans = 1000000
for si in S:
    if si == "E":
        cost -= 1
    elif si == "W":
        if cost < ans:
            ans = cost
        cost += 1
else:
    if cost < ans:
        ans = cost
print(ans)

Submission status: Accepted

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

cost = list(S).count("E")

ans = 10 ** 6
for si in S:
    if si == "E":
        cost -= 1
    
    if cost < ans:
        ans = cost

    if si == "W":
        cost += 1

print(ans)

Submission status: Accepted

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

cost = list(S).count("E")

ans = 10 ** 6
for si in S:
    if si == "E":
        cost -= 1
    
    ans = min(ans, cost)

    if si == "W":
        cost += 1

print(ans)

Submission status: Accepted

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

leftWest = 0
rightEast = list(S).count("E")

ans = 10 ** 6
for si in S:
    if si == "E":
        rightEast -= 1
    
    ans = min(ans, leftWest + rightEast)

    if si == "W":
        leftWest += 1

print(ans)

Submission status: Accepted

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

leftWest = 0
rightEast = S.count("E")

ans = 10 ** 6
for i in range(len(S)):
    if S[i] == "E":
        rightEast -= 1
    
    ans = min(ans, leftWest + rightEast)

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

print(ans)

Submission status: Accepted

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

leftWest = 0
rightEast = list(S).count("E")

ans = 10 ** 6
for i in range(len(S)):
    if S[i] == "E":
        rightEast -= 1
    
    ans = min(ans, leftWest + rightEast)

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

print(ans)

Submission status: Accepted

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

n_e = 0
for ss in s:
    if ss == 'E':
        n_e += 1
n_w = n - n_e

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

Submission status: Accepted

Solution code:
#ABC098C
N=int(input())
S=list(str(input()))
W=[0]*N
Turn=[0]*N
for i in range(N):
    if S[i]=="W":
        if i==0:
            W[i]=1
        else:
            W[i] = W[i - 1] + 1
    else:
        if i==0:
            W[i]=0
        else:
            W[i]=W[i-1]
for i in range(N):
    if i==0:
        Turn[i]=0+((N-1-i)-(W[N-1]-W[i]))
    else:
        Turn[i]=W[i-1]+((N-1-i)-(W[N-1]-W[i]))
print(min(Turn))

Submission status: Accepted

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

count = 0
for i in range(1, len(s)):
    if s[i] == 'E':
        count += 1
minc = count
for i in range(1, len(s)):
    if s[i - 1] == 'W':
        count += 1
    if s[i] == 'E':
        count -= 1
    minc = min(minc, count)
print(minc)

Submission status: Accepted

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

ans = S.count('E')
w = 0
e = S.count('E')
for i in range(N):
    if S[i-1] == 'W' and i != 0:
        w += 1
    if S[i] == 'E':
        e -= 1
    if w + e < ans:
        ans = w + e
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
ls = []
from numpy import cumsum
for i in input():
    if i == 'W':
        ls.append(1)
    else:
        ls.append(0)
a = cumsum(ls)
ans = (len(a)-1)-(a[-1]-a[0])
for i in range(1,len(a)):
    ans = min(ans,(len(a)-(i+1))-(a[-1]-a[i])+a[i-1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = N
x = 0
y = S.count('E')
for i in range(N):
    if S[i-1] == 'W' and i > 0:
        x += 1
    if S[i] == 'E':
        y -= 1
    if x + y < ans:
        ans = x + y
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e_l = [0] * (n+1)
w_l = [0] * (n+1)
for i in range(n):
    if s[i] == 'E':
        e_l[i+1] = e_l[i] + 1
    else:
        e_l[i+1] = e_l[i]
for j in range(n, 0, -1):
    if s[j-1] == 'W':
        w_l[j-1] = w_l[j] + 1
    else:
        w_l[j-1] = w_l[j]
ans = 0
for k in range(n):
    res = e_l[k+1] + w_l[k]
    ans = max(ans, res)
print(n-ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def run(N, S):
    num_l = [0]
    num_r = [0]
    for i in range(1, len(S)):
        cnt = num_l[i-1]
        if S[i-1] == 'W':
            num_l.append(cnt+1)
        else:
            num_l.append(cnt)
        cnt = num_r[i-1]
        if S[N-i] == 'E':
            num_r.append(cnt+1)
        else:
            num_r.append(cnt)
    ret = max(num_l) + max(num_r)
    for i in range(len(num_l)):
        ret = min(ret, num_l[i]+num_r[N-1-i])
    return ret


def main():
    N = int(input())
    S = input()
    print(run(N, S))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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


numlist = []
for i in range(n):
    if i > 0:
        if s[i-1] == 'W':
            LW_cnt += 1
        if s[i] == 'E':
            RE_cnt -= 1

    numlist.append(LW_cnt+RE_cnt)

# 出力
print("{}".format(min(numlist)))

Submission status: Accepted

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

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

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

del w[0]
del e[0]



l = [0]*n
r = [0]*n

for i in range(n):
    if i != 0:
        l[i] = w[i - 1]
        r[i] = e[n - 1] - e[i]
    else:
        r[0] = e[n-1]


ans = [(l[i] + r[i]) for i in range(n)]

print(min(ans))

Submission status: Accepted

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

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

print(min(count))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
east = S.count("E")
west = 0
min = east

for i in S:
    if i == "E":
        east -= 1
    else:
        west += 1
    if min > east + west:
        min = east + west

print(min)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
c=s[1:n].count('E')
l=[]
l.append(c)
for i in range(1,n):
  if s[i-1:i+1]=='WW':c+=1
  elif s[i-1:i+1]=='EE':c-=1
  l.append(c)
print(min(l))

Submission status: Accepted

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

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

print(min(nums))

Submission status: Accepted

Solution code:
N=int(input())
arr=[0 for i in range(N+1)]
for i,x in enumerate(input()):
    if x == 'W':
        arr[i+1]+=arr[i]+1
    else:
        arr[i+1]=arr[i]

min_=float('inf')
for l in range(N):
    tmp = arr[l]+N-arr[N]-l-1+arr[l+1]
    if tmp<min_:
        min_=tmp
print(min_)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
s = input()
reverse_list_1 = [0]
reverse_list_2 = [0]
ans = n

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

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

reverse_list_2 = list(reversed(reverse_list_2))

for i in range(n):
    if reverse_list_1[i]+reverse_list_2[i] < ans:
        ans = reverse_list_1[i]+reverse_list_2[i]
print(ans)

Submission status: Accepted

Solution code:
#encoding utf-8

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

reverse = 3*10**5+1
west = []
east = []
west_num = 0
east_num = 0
for i in range(len(s)):
    if s[i] == "W":
        west_num += 1
    elif s[i] == "E":
        east_num += 1
    west.append(west_num)
    east.append(east_num)
# print(west,east)
for i in range(n):
    if i == 0:
        point = east[-1]+east[0]
    elif i == n-1:
        point = west[i-1]
    else:
        point = east[-1]-east[i]+west[i-1]
    # print(point)
    reverse = min(point, reverse)

print(reverse)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
cnt = []
count = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
for i in range(n):
    if i:
        s[i] = s[i - 1] + int(s[i] == "E")
    else:
        s[i] = int(s[i] == "E")
s.append(s[n - 1])
ans = float("inf")
for i in range(n):
    rightE = s[n] - s[i]
    if i == 0:
        ans = min(ans, rightE)
    else:
        leftW = i - s[i - 1]
        ans = min(ans, leftW + rightE)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
w = 0
e = s[1:n].count("E")
m = w + e
for i in range(n-1):
  if s[i] == "W":
    w = w + 1
  if s[i+1] == "E":
    e = e - 1
  if w + e < m:
    m = w + e
print(m)

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
    ans = min(ans, r+l)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = 3 * 10 ** 5
w = S.count('W')
w_r = 0

for i, a in enumerate(S):
	if a == 'W':
		w_r += 1
		ans = min(ans, 2*w_r+N-w-i-2)
	else:
		ans = min(ans, 2*w_r+N-w-i-1)
	

print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

INF = float('inf')


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

num_E = S[:].count('E')
num_W = S[:].count('W')


e = num_E
w = num_W
ans = INF
for s in S:
    cost = 0
    if s == 'E':
        e -= 1
        cost = e + (num_W - w)
    else:
        w -= 1
        cost = e + (num_W - w - 1)
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

INF = float('inf')


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

num_E = 0
num_W = 0
for s in S:
    if s == 'E':
        num_E += 1
    else:
        num_W += 1

e = num_E
w = num_W
ans = INF
for s in S:
    cost = 0
    if s == 'E':
        e -= 1
        cost = e + (num_W - w)
    else:
        w -= 1
        cost = e + (num_W - w - 1)
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

def main():
    min_index = 0
    min_num = 0
    curr = 0
    for i in range(N):
        if S[i] == "E":
            curr -= 1
        else:
            curr += 1
        if min_num > curr:
            min_index = i
        min_num = min(min_num, curr)
    
    left = S[:min_index]
    right = S[min_index + 1:] # excludes the leader.
    ans = left.count("W") + right.count("E")
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
e_count=s.count("E")
w_count=n-e_count
w_left=0
e_right=e_count
c=[]
for i in range(n):
    if s[i]=='W':
        c+=[w_left+e_right]
        w_left+=1
    else:
        e_right-=1
        c+=[w_left+e_right]
print(min(c))

Submission status: Accepted

Solution code:
N=int(input())
S=str(input())
#W=左
#E=右
ans=[]
count=0
#一番左
for i in range(1,N):
    
    if S[i]=="E":
        count+=1    
        
        
ans.append(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=input()

le=0
ri=s[1:n].count("E")

minmuki=min(s[0:n-1].count("W"),s[1:n].count("E"))

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

print(minmuki)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
p_sum=[0]*N
max_sum=0
leader=0
count = 0

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

	if max_sum < p_sum[i]:
		max_sum=p_sum[i]
		leader=i

for i in range(N):
	if i<leader:
		if S[i]=="W":
			count += 1
	elif i>leader:
		if S[i]=="E":
			count += 1

print(count)

Submission status: Accepted

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

SUM=[0]*(N+1)#iより西のWの数
SUM2=[0]*(N+1)#iより西のEの数
for i in range(N):
    if A[i]=="W":
        SUM[i+1] = SUM[i]+1
        SUM2[i+1] = SUM2[i]
    else:
        SUM2[i+1] = SUM2[i]+1
        SUM[i+1] = SUM[i]
    
SUM3 = [SUM[i]+(SUM2[-1]-SUM2[i+1]) for i in range(N)]
print(min(SUM3))

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input())
S = input()

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

#左からi番目の人をリーダーに任命した場合に向く方向を変える人数を
#c[i]に格納する。
C = []

for i in range(n):
    cl = Wcnt[i]-Wcnt[0]
    cr = n-i-1-(Wcnt[n]-Wcnt[i+1])
    C.append(cl + cr)
    
print(min(C))

Submission status: Accepted

Solution code:
N = int(raw_input())
S = raw_input()
slist = []
for i in range(0, N):
	slist.append(S[i])
min = slist.count('E')
if slist[0] == 'E':
	min = min - 1
cnt = min
for i in range(1, N):
	if slist[i] == 'E':
		cnt = cnt - 1
	if slist[i-1] == 'W':
		cnt = cnt + 1
	if cnt < min:
		min = cnt

print min

Submission status: Accepted

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

ans = [0]*n
ans[0] = s[1:].count("E")

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

print(min(ans))

Submission status: Accepted

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

c = list(s)

lw = 0
le = 0
rw = s[1:].count("W")
re = s[1:].count("E")

ans = lw + re

for i in range(1,n):
    lw = lw + bool(c[i-1] == "W")
    le = le + bool(c[i-1] == "E")
    rw = rw - bool(c[i] == "W")
    re = re - bool(c[i] == "E")
    
    ans = min(ans, lw+re)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a=0
b=0
e=[]
w=[0]
ans=[]

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

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

A = []

for n in range(N):
    if n == 0:
        L = 0
        R = sum([s == 'E' for s in S[n+1:]])
    else:
        L += 0 if S[n-1] == 'E' else 1
        R -= 0 if S[n]   != 'E' else 1
    A.append(L+R)

print(min(A))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
s = []
cnt = 0
for i in range(N):
    if S[i] == "W":
        s.append(-1)
    elif S[i] == "E":
        s.append(1)
        if i != 0:
            cnt += 1
        
DP = [cnt]

for i in range(1, N):
    cnt = 0
    if s[i - 1] == -1:
        cnt += 1
    if s[i] == 1:
        cnt -= 1
    DP.append(DP[i - 1] + cnt)

print(min(DP))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = list(input())

Wleft = np.zeros(N, dtype = int)
Eright = np.zeros(N, dtype = int)
Eright[0] = (S.count("E") if S[0] == "W" else S.count("E") - 1)
for i in range(N-1):
    if S[i] == "W":
        Wleft[i+1] = Wleft[i]+1
    else:
        Wleft[i+1] = Wleft[i]
    if S[i+1] == "E":
        Eright[i+1] = Eright[i] - 1
    else:
        Eright[i+1] = Eright[i]
total = Wleft + Eright
print(np.min(total))

Submission status: Accepted

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

answer = prev = S[1:].count('E')
for i in range(1, N):
    _answer = prev if S[i-1] == 'E' else prev+1
    if S[i] == 'E':
        _answer -= 1
    answer = min(answer, _answer)
    prev = _answer

print(answer)

Submission status: Accepted

Solution code:
n = int(input())
line = list(input())
cum_sum_w = [0]
cum_sum_e = [0]
count_w = 0
count_e = 0
for i in range(n):
    if line[i] == 'W':
        count_w += 1
    else:
        count_e += 1
    cum_sum_w.append(count_w)
    cum_sum_e.append(count_e)
count_list = []
for i in range(1, n + 1):
    count = 0
    count += cum_sum_w[i - 1]
    count += cum_sum_e[n] - cum_sum_e[i]
    count_list.append(count)
print(min(count_list))

Submission status: Accepted

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

"""
ある点から西を全てEに、東を全てWにする際のコストを最小化したい

入力例1)
5
WEEWW

EAST: 21000             東から西にかけてEが何個あるか
WEST: 01112             西から東にかけてWが何個あるか
 SUM: 22112             この配列の最小値が答え
"""

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

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

sum = []
for i in range(n):
    sum.append(east[i]+west[i])

print(min(sum))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = input()
S = '*' + S + '*'
W = [x=='W' for x in S]
E = [x=='E' for x in S]
W = np.cumsum(W)
E = np.cumsum(E[::-1])[::-1]

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

Submission status: Accepted

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

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

m = float("inf")
for i in range(N):
    cnt = w[i] + e[i] - 1
    if cnt < m:
        m = cnt
print(m)

Submission status: Accepted

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

cost = S[1:].count('E')
min_cost = cost

for i in range(1, len(S)):
  cost = cost + (S[i-1] == 'W') - (S[i] == 'E')
  if cost < min_cost:
    min_cost = cost
print(min_cost)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  6 17:34:40 2018

@author: Yuki
"""

N = int(input())
S = input()
res = []

res.append(S[1:].count('E'))
for i in range(1, len(S)):
    if S[i] == 'E':
        tmp = res[-1]-1
        if S[i-1] == 'W':
            tmp += 1
        
    elif S[i] == 'W':
        tmp = res[-1]
        if S[i-1] == 'W':
            tmp += 1
    res.append(tmp)
    

print(min(res))

Submission status: Accepted

Solution code:
#!/usr/bin/python3

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

def main():
    N = int(input())
    S = input()
    cs = [0] * (N + 1)
    for i in range(N):
        cs[i + 1] = cs[i] + (S[i] == "W")

    ans = INF
    for i in range(N):
        l_w = cs[i]
        l_e = i - l_w
        r_w = cs[N] - cs[i + 1]
        r_e = (N - i - 1) - r_w

        ans = min(ans, l_w + r_e)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = input()

F = np.zeros(N, dtype=int)

F[0] = S[1:].count('E')

for i in range(1, len(S)):
    F[i] = F[i-1]
    if S[i-1] == 'W':
        F[i] += 1

    if S[i] == 'E':
        F[i] -= 1

minv = np.amin(F)
print(minv)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = str(input())

E = np.cumsum([1 if s == "E" else 0 for s in S])
W = np.cumsum([1 if s == "W" else 0 for s in S])

ans = float("inf")
for i, v in enumerate(S):
    if v == "E":
        ans = min(ans, (E[-1] - E[i]) + W[i])
    else:
        ans = min(ans, (E[-1] - E[i]) + W[i] - 1)
    

print(ans)

Submission status: Accepted

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

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

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

ans = 3 * 10 **5

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
E = s.count("E")
W = 0
min_num = n
for s_i in s:
    if s_i == "E":
        E -= 1
    else :
        W += 1

    change_man = E + W - bool(s_i == "W")
    min_num = min(min_num, change_man)
print(min_num)

Submission status: Accepted

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

left = 0
left_E = 0
right = N-1
right_W = S[1:].count("W")
ans = ((left-left_E) + (right-right_W))

for i in range(1,N):
    left += 1
    if S[i-1] == "E":
        left_E += 1
        
    right -= 1
    if S[i] == "W":
        right_W -= 1
        
    ans = min(ans, (left-left_E) + (right-right_W))
    
print(ans)

Submission status: Accepted

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

cumw = [0]
for c in S:
    cumw.append(cumw[-1] + int(c=='W'))

ans = N
for i in range(N):
    tmp = cumw[i]
    tmp += (N-i-1) - (cumw[-1]-cumw[i+1])
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

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

min_num = 300000
w_west = 0
e_east = s.count("E")
for s_i in s:
    if s_i == "E":
        e_east -= 1
    else:
        w_west += 1
    tmp = w_west + e_east - bool(s_i == "W")
    if tmp < min_num:
        min_num = tmp
print(min_num)

Submission status: Accepted

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

lc = 0
rc = (N - 1) - (sum_s[N - 1] - sum_s[0])
ans = lc + rc

for i in range(1, N):
    lc = sum_s[i - 1]
    rc = (N - (i + 1)) - (sum_s[N - 1] - sum_s[i])

    ans = min(ans, lc + rc)

print(ans)

Submission status: Accepted

Solution code:
import sys

#a, b = map(int, input().split(" "))
N = int(input())
S = input()

w = [0 for i in range(N)]
e = [0 for i in range(N)]

if S[0] =='W':
	w[0] = 1
else:
	e[0] = 1

for i in range(1, 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	

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

Submission status: Accepted

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

cum_wlist, cum_elist = [], []
cum_w, cum_e = 0, 0

for i in S :
    if i == "W" :
        cum_w += 1
    else :
        cum_e += 1
    cum_wlist.append(cum_w)
    cum_elist.append(cum_e)
    
min_change = N + 1

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

print(min_change)

Submission status: Accepted

Solution code:
# 左向きがw、 右がE
n = int(input())
sarr = input()
L = 'W'
R = 'E'

RtoL = 0
lead = sarr[0]
for s in sarr[1:]:
	if s == R:
		RtoL += 1

LtoR = 0

lowest = RtoL
for s in sarr[1:]:
	if lead == L:
		LtoR += 1
	if s == R:
		RtoL -= 1
	
	tmpres = LtoR + RtoL
	
	if tmpres < lowest:
		lowest = tmpres
	
	lead = s

print(lowest)

Submission status: Accepted

Solution code:
import sys

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

West = S.count("W")
ans = sys.maxsize
tmp_W = 0

for i in range(0, N):
    tmp_ans = tmp_W

    if(S[i] == "W"):
        tmp_W += 1

    tmp_ans += N - i - 1 - West + tmp_W

    if(tmp_ans < ans):
        ans = tmp_ans

print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8

n = int(input())
string = input()
ecnt = 0
for i in range(n):
    if string[i] == "E":
        ecnt += 1
wcnt = 0
minv = 10000000000
for i in range(n):
    if string[i] == "E":
        ecnt -= 1
    if i >= 1 and string[i-1] == "W":
        wcnt += 1
    minv = min(minv,wcnt+ecnt)
    
print(minv)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
w_cnt=[0]*(n+1)
e_cnt=[0]*(n+1)
s.insert(0,"G")
for i in range(1,n+1):
    if s[i]=="W":
        w_cnt[i]=w_cnt[i-1]+1
    else:
        w_cnt[i]=w_cnt[i-1]
s.reverse()
s.pop(n)
s.insert(0,"G")
for i in range(1,n+1):
    if s[i]=="E":
        e_cnt[i]=e_cnt[i-1]+1
    else:
        e_cnt[i]=e_cnt[i-1]
ans=float("inf")
for i in range(n):
    temp=w_cnt[i]+e_cnt[n-1-i]
    if temp<=ans:
        ans=temp
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(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_E + num_W)
        num_W += 1
    else:
        num_E -= 1
        ans=min(ans , num_E + num_W)
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
direction = list(input())
change = [0]*(N+1)
# print(direction)
change[0] = direction.count("E")
# print(change)
for i in range(N):
    f_n = 0
    f_p = 0
    if direction[i] == "E":
        f_n = 1
    else:
        f_p = 1
    change[i+1] = change[i] - f_n + f_p
# print(change)
print(min(change))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Ecount = S.count('E')
Wcount = 0
min = Ecount
for i in S:
    if i == 'E':
        Ecount -= 1
    else:
        Wcount += 1
    if min > Ecount + Wcount:
        min = Ecount + Wcount
print(min)

Submission status: Accepted

Solution code:
import numpy
n = int(input())
s = input()
Ws = [0] + [1 if i == "W" else 0 for i in s]
Ws_sum = list(numpy.cumsum(Ws))
cnt_l = []
for i in range(n):
    cnt_l.append(Ws_sum[i]+n-i-1-Ws_sum[-1]+Ws_sum[i+1])
print(min(cnt_l))