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:
class EList(list):
    def accumulation_sum(self):
        if self.__len__() > 1:
            for i in range(self.__len__() - 1):
                self[i + 1] += self[i]
        return self

    def back_accumulation_sum(self):
        if self.__len__() > 1:
            for i in range(self.__len__() - 1, 0, -1):
                self[i - 1] += self[i]
        return self


if __name__ == '__main__':
    N = int(input())
    S = [i for i in input()]
    left = EList(1 if i == 'W' else 0 for i in S)
    right = EList(1 if i == 'E' else 0 for i in S)
    left.accumulation_sum()
    right.back_accumulation_sum()

    ans = min(map(lambda l, r:
                  l + r - 1
              , *(left, right)))
    print(ans)

Submission status: Accepted

Solution code:
N, S = int(input()), input()
countw, counte = 0, S.count("E")

ans = 3 * 10 ** 5
for s in S:
    if s == "W":
        ans = min(ans, counte + countw)
        countw += 1
    elif s == "E":
        counte -= 1
        ans = min(ans, counte + countw)
print(ans)

Submission status: Accepted

Solution code:

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


en = s.count('E')

ans = en if s[0]=='W' else en -1


wc = 0
for i in range(1,len(s)):
    if s[i-1] == 'W':
        wc += 1
    else:
        en -= 1

#    print('wc = {}, en = {}'.format(wc,en))
    ans = min(ans, en + wc - (1 if s[i] == 'E' else 0 ) )

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [1 if l == 'E' else 0 for l in list(input())]

for i in range(1,N):
    S[i] += S[i-1]
S.insert(0,0)

ans = N
for i in range(N):
    diff = i - S[i] + S[-1] - S[i+1]
    if ans > diff:
        ans = diff

print(ans)

Submission status: Accepted

Solution code:
import numpy

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

li = numpy.asarray([1 if i == 'W' else 0 for i in s])
w = numpy.cumsum(li)
e = numpy.cumsum(1 - li[::-1])[::-1]

su = w + e
ans = min(su) - 1

print(ans)

Submission status: Accepted

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

def integral(li, query):
    res = [0]
    for i in li:
        cnt = res[-1] + (1 if i == query else 0)
        res.append(cnt)
    return res[1:]

w = integral(s, 'W')
e = integral(s[::-1], 'E')[::-1]

su = [x + y for x, y in zip(w, e)]
ans = min(su) - 1


print(ans)

Submission status: Accepted

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

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

ans = 10**9
for i in range(N):
    ans = min(ans, left[i]+right[i])

print(ans)

Submission status: Accepted

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

e = [0]
w = [0]

te, tw = 0, 0
for i in range(N):
    if s[i] == 'E':
        te += 1
    else:
        tw += 1
    e.append(te)
    w.append(tw)

ans = N
for leader_idx in range(N):
    t = 0
    t += (e[N] - e[leader_idx + 1])
    t += (w[leader_idx] - w[0])
    ans = min(ans, t)
print(ans)

Submission status: Accepted

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

L = [1 if s == 'W' else 0 for s in S]
R = [1 if s == 'E' else 0 for s in S][::-1]

for i in range(1, N):
    L[i] += L[i-1]
    R[i] += R[i-1]

L = [0] + L[:-1]
R = R[:-1][::-1] + [0]

print(min([L[i] + R[i] for i in range(N)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
eastcount = [0 for i in range(N+1)]

for i in range(N):
    if S[i] == "E":
        eastcount[i+1] = eastcount[i] + 1
    else:
        eastcount[i+1] = eastcount[i]
ans = 1000000
ans = min(ans, eastcount[N] - eastcount[1])
ans = min(ans, N - 1 - eastcount[N-1])
for i in range(1, N):
    ans = min(ans, (i-1 - eastcount[i-1]) + (eastcount[N] - eastcount[i]))
print(ans)

Submission status: Accepted

Solution code:
N = input()
S = input()
L = [0]+[]
for x in S:
    if x == 'W':
        L.append(0)
    else:
        L.append(1)
for i in range(len(L)-1):
    L[i+1] += L[i]
ans =[]
for i in range(len(L)-1):
    cnt = i - L[i]
    try :cnt += L[len(L)-1] - L[i+1]
    except : break
    ans.append(cnt)
print(min(ans))

Submission status: Accepted

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

ruiseki_count_w = [0]*n
ruiseki_count_w[0] = 1 if s[0]=="W" else 0

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

#print(ruiseki_count_w)
ans=300000

for i in range(n):
  
  if i ==0:
    ans = min(n-1-(ruiseki_count_w[n-1]-ruiseki_count_w[0]),ans)
  elif i ==n:
    ans = min(ruiseki_count_w[n-1],ans)
  else:
    left_side_w = ruiseki_count_w[i-1]
    rihgt_side_e = (n-1-i)-(ruiseki_count_w[n-1]-ruiseki_count_w[i])
    ans = min(left_side_w+rihgt_side_e,ans)

print(ans)


#計算量オーバーしそう
#for i in range(n):
#  ans = 0
#  for j in range(n):
#    if i < j and s[j]=="E":
#      ans += 1
#    if j < i and s[j]=="W":
#      ans += 1
#  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
numS = [0 for i in range(N)]
sumS = [0 for i in range(N)]
for i in range(N):
    if S[i] == 'E':
        numS[i] = 0
    else:
        numS[i] = 1

for i in range(N):
    sumS[i] = sumS[i-1] + numS[i]
# print(sumS)
minS = (N-1) - (sumS[N-1]-sumS[0])
for i in range(1,N):
    minS = min(minS,sumS[i-1]+(N-i-1)-(sumS[N-1]-sumS[i]))
print(minS)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    S = ii(str)
    S = [1 if s == "W" else -1 for s in S]
    pivot = np.argmin(np.cumsum(S))
    E = S[:pivot].count(1)
    W = S[pivot+1:].count(-1)
    return E + W


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

#振り向く人数の最小値
min = N

#まずは0番目をリーダーとした場合の振り向く人数を調べる。
left = 0    #リーダーの左側にいる人の振り向く人数     
right = 0   #リーダーの右側にいる人の振り向く人数
for i in S:
    if i =='E':
        right+=1

if S[0]=='E':
    right -=1

min = right

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

print(min)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ww=0
ee=0
for i in S:
    if i=='E':
        ee+=1
res=[]
for i in S:
    if i=='E':
        ee-=1
    count=ww+ee
    res.append(count)
    if i=='W':
        ww+=1
print(min(res))

Submission status: Accepted

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

def main():
    N = int(input())
    S = input()

    m = r = S[1:].count('E')
    for i in range(1, N):
        s = S[i - 1: i + 1]
        if s == 'EE':
            r -= 1
        elif s == 'WW':
            r += 1
        m = min(m, r)

    print(m)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
S = input()
w_num = S.count('W')
e_num = S.count('E')
a = 0
b = e_num
amin = n+1
if(len(set(S)) == 1):
    print(0)
else:
    for i in S:
        if(i == 'E'):
            b -= 1
        else:
            a+=1
        if(a+b < amin):
            amin = a+b
    print(min(amin, w_num, e_num))

Submission status: Accepted

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

Submission status: Accepted

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

ans = S[1:].count('E')
left = 0
right = ans

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

Submission status: Accepted

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

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

for i in range(1,n):
    l[i]+=l[i-1]
    r[n-i-1]+=r[n-i]

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
s = list(input())
sum = [0]*(N+1)

for i in range(1,N+1):
    if s[i-1] == 'W':
        sum[i] = sum[i-1]+1
    else:
        sum[i] = sum[i-1]
    
for i in range(1,N+1):
    temp = sum[i-1] + (N-i - (sum[N]-sum[i]))
    if i == 1:
        ans = temp
    elif temp < ans:
        ans = temp
    

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

count = deque()

left = {'W':0, 'E':0}
w = S.count('W')
right = {'W':w, 'E':N-w}

for i in range(N):
    base = S[i]
    right[base] -= 1
    count.append(left['W']+right['E'])
    left[base] += 1
print(min(count))

Submission status: Accepted

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

min_count = N

left = {'W':0, 'E':0}
w = S.count('W')
right = {'W':w, 'E':N-w}

for i in range(N):
    base = S[i]
    right[base] -= 1
    min_count = min(left['W']+right['E'], min_count)
    left[base] += 1
print(min_count)

Submission status: Accepted

Solution code:
def we():
    input()
    s=input()
    cnt=s.count('E')
    ans=s.count('E')
    for i in s:
        if i =='W':
            cnt+=1
        else:
            cnt-=1
            ans=min(ans,cnt)
    return (min(cnt,ans))
print(we())

Submission status: Accepted

Solution code:
def we(s):
    cnt=s.count('E')
    ans=s.count('E')
    for i in s:
        if i =='W':
            cnt+=1
        else:
            cnt-=1
            ans=min(ans,cnt)
    return (min(cnt,ans))
input()
print(we(input()))

Submission status: Accepted

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

cnt = [[0, 0] for _ in range(N)]

if S[0] == 'W':
    cnt[0][0] = 1

if S[-1] == 'E':
    cnt[-1][1] = 1

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

count = float('inf')
for i in range(N):
    count = min(count, cnt[i][0] + cnt[i][1])

print(count - 1)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = [i for i in input()]
ans = S.count("E")
cnt = S.count("E")

for i in S:
  if i == "W":
    cnt += 1
  elif i == "E":
    cnt -= 1
    ans = min(ans,cnt)
  
print(ans)

Submission status: Accepted

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

C = 0
for i in range(1, N):
  if S[i] == "E":
    C += 1
    
A = C
for i in range(1, N):
  if S[i-1] == "W":
    C += 1
  if S[i] == "E":
    C -= 1
  if A > C:
    A = C
    
print(A)

Submission status: Accepted

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

#O(N)で累積和が求められる

ruiseki=[[0 for i in range(2)] for j in range(N)]

for i in range(0,N):
    if i!=0:
        if S[i]=='W':
            ruiseki[i][0]=1
        else:
            ruiseki[i][1]=1
    else:
        if S[i]=='W':
            ruiseki[i][0]=1
        else:
            ruiseki[i][1]=1

    ruiseki[i][0]+=ruiseki[i-1][0]
    ruiseki[i][1]+=ruiseki[i-1][1]







mn = 10**6

for i in range(0,N):
    if i>=1 and i<=N-2:
        m = ruiseki[i-1][0]+ruiseki[N-1][1]-ruiseki[i][1]
    elif i==0:
        m = ruiseki[N-1][1]-ruiseki[0][1]
    else:
        m = ruiseki[N-2][0]

    if mn>m:
        mn = m


print(mn)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
ans = n
temp = 0

for i in range(1, n):
    if S[i] =='E':
        temp += 1
ans = min(ans, temp)

for i in range(1, n):
    if S[i - 1] == 'W':
        temp += 1
    if S[i] == 'E':
        temp -= 1
    ans = min(ans, temp)        

print(ans)

Submission status: Accepted

Solution code:
input()
S = input()
res = cnt = S.count('E')
for c in S:
    if c == 'E':
        cnt -= 1
        res = min(res, cnt)
    else:
        cnt += 1
print(res)

Submission status: Accepted

Solution code:
input()
S = input()
res = cnt = S.count('E')
for c in S:
    cnt -= c == 'E'
    res = min(res, cnt)
    cnt += c == 'W'
print(res)

Submission status: Accepted

Solution code:
import sys
# N,M = [int(n) for n in input().split()]
# S = str(input())
# T = str(input())
# a,b,c = [int(n) for n in input().split()]
# ←W, E→
# EEELWWWWにしたい
N = int(input())
S = str(input())
cusum = [0]*N
cusum[0] = 1 if S[0] == 'E' else 0

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

Submission status: Accepted

Solution code:

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

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

print(min)

Submission status: Accepted

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

output=S[1:].count("E")
a=output

for i in range(N-1):
    if(S[i]=="W"):
        a+=1
    if(S[i+1]=="E"):
        a-=1
    if(a<output):
        output=a
print(output)

Submission status: Accepted

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

e = []
w = []
ek = 0
wk = 0
for i in range(n):
    if s[i] == "E":
        ek += 1
        e.append(ek)
        w.append(wk)
    if s[i] == "W":
        wk += 1
        e.append(ek)
        w.append(wk)

ma = e[n-1]-e[0]
for i in range(n):
    if i != n-1:
        cos = e[n-1]-e[i] + w[i]
    else:
        cos = w[n-2]
    ma = min(cos,ma)

print(ma)

Submission status: Accepted

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

num = [0 for _ in range(n)]

num[0] = s[1:].count("E")
for i in range(1,n):
    num[i] = num[i-1] + int(s[i-1]=="W") - int(s[i]=="E")

print(min(num))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ss = [c == 'E' for c in input()]
    c_l = 0
    c_r = len([t for t in ss[1:] if t])
    res = c_r
    for i in range(1, n):
        c_l += int(not ss[i - 1])
        c_r -= int(ss[i])
        res = min(res, c_l + c_r)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
e=s.count("E")
w=n-e
wc=0
ec=0
min1=300001
for i in range(len(s)):
    if s[i]=="W":
        wc+=1
        el=e-ec
        wl=wc-1
        ans=el+wl
        min1=min(min1,el+wl)    
    else:
        ec+=1
        el=e-ec
        wl=wc
        ans=el+wl
        min1=min(min1,el+wl)
print(min1)

Submission status: Accepted

Solution code:
n = int(input())
ls = [(0, 0) for _ in range(n)]
s = list(input())
sum_ls = {}

for i in range(n):
    if i == 0:
        w = 0
        e = s[i+1:].count('E')
        sum_ls[i] = (w, e)
    else:
        _w, _e = sum_ls[i - 1]
        if s[i] == 'W':
            if s[i-1] == 'W':
                sum_ls[i] = (_w+1, _e)
            else:
                sum_ls[i] = (_w+1, _e-1)
        else:
            if s[i-1] == 'W':
                sum_ls[i] = (_w, _e)
            else:
                sum_ls[i] = (_w, _e-1)

print(min([sum(v) for v in sum_ls.values()]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def sum_list(l1, l2):
  for i in range(len(l1)):
    l1[i] += l2[i]

  return l1


n = (int)(input())
s = input()
l_e = [0 for _ in range(n+1)]
l_w = [0 for _ in range(n+1)]

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

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

l_e = l_e[:-1]
l_w = l_w[:n]


#print("E", l_e)
#print("W", l_w)

print(min(sum_list(l_e, l_w))-1)

Submission status: Accepted

Solution code:
a = int(input())
b = list(input())
e = b.count("E")
w = 0
z = e
for c in b:
    if c == "E":
        e -= 1
    else:
        w += 1
    if e + w < z:
        z = e + w
print(z)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count

for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    if e_count + w_count < ans:
        ans = e_count+w_count

print(ans)

Submission status: Accepted

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

w = 0
e = S[1:].count('E')
score = w + e

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

print(score)

Submission status: Accepted

Solution code:
# encoding: utf-8
N = int(input())
S = input()

tmp = 0
acm = [0] * (N + 1)

for i, Si in enumerate(S):
    if Si == "W": tmp += 1
    else: tmp -= 1
    acm[i + 1] = tmp

minscore = N
for i in range(N):
    L = acm[i]
    R = acm[N] - acm[i + 1]
    if (L - R) < minscore:
        minscore = (L - R)

print((minscore + N - 1) // 2)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count
for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    if e_count + w_count < ans:
        ans = e_count + w_count
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
#左からスタートするとして，全てWならば0になるため
E_count = S.count('E')
W_count = 0
mini_count = E_count

for i in S:
  if i == 'E':
    #リーダーはどちらを向いていても良いので1を引く
    E_count -= 1
  else:
    #Wの場合は，今後向きを変えてもらう必要があるため
    W_count += 1
  if mini_count > E_count + W_count:
    mini_count = E_count + W_count
print(mini_count)

Submission status: Accepted

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

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

ans = 1e100
for i in range(1, N + 1):
    lt = lefts[i]
    rt = lefts[-1]

    lw = lt[0]
    le = lt[1]
    rw = rt[0] - lw
    re = rt[1] - le
    if S[i - 1] == "W":
        lw -= 1
    else:
        le -= 1
    ans = min(ans, lw + re)
print(ans)

Submission status: Accepted

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

dp = [0] * n

WsW = 0
WsE = 0
EsW = 0
EsE = 0
count = float('inf')

for i in range(n):
    if s[i] =="W":
        WsW += 1
    else:
        WsE += 1

for j in range(n):
    if s[j] == "W" :
        WsW -= 1
    else:
        WsE -= 1
    count = min(count, WsE + EsW)
    if s[j] == "W" :
        EsW += 1
    else:
        EsE += 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
nin = s[1:n].count("E")
ans = nin
for i in range(n - 1):
    if s[i] == "E" and s[i + 1] == "E":
        nin -= 1
    elif s[i] == "W" and s[i + 1] == "W":
        nin += 1
    if nin < ans:
        ans = nin
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = s[1:].count('E')
inf = float('inf')
ans = [inf]*n
ans[0] = a
for i in range(1,n):

    if s[i] == 'E':
        ans[i] = ans[i-1] - 1
    else:
        ans[i] = ans[i-1]

    if s[i-1] == 'W':
        ans[i] += 1
    else:
        continue

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
min = S[1:N].count('E')
now = S[1:N].count('E')

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

print(min)

Submission status: Accepted

Solution code:
n = int(input())
s = list(str(input()))
p = s.count("E")
ans = p
for i in s:
    if i == "W":
        p += 1
    else:
        p -= 1
    ans = min(p, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s = list(s)
allli = {"w":s.count("W"),"e":s.count("E")}
leftli = {"w":0,"e":0}
rightli = allli
coli = []
for i in range(n):
    if i == 0:
        if s[i] == "W":
            rightli["w"] -= 1
        else:
            rightli["e"] -= 1
        coli.append(rightli["e"])
    else:
        if s[i - 1] == "W":
            leftli["w"] += 1
        else:
            leftli["e"] += 1
        if s[i] == "W":
            rightli["w"] -= 1
        else:
            rightli["e"] -= 1
        coli.append(rightli["e"] + leftli["w"])

print(min(coli))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = [0] * N
ans[0] = S[1:].count('E')
num = ans[0]

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

Submission status: Accepted

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

c = 0
for i in range(1, N):
    if S[i] == 'E':
        c += 1
dp = [c]

for i in range(1, N):
    c = dp[i-1]
    if S[i-1] == 'W':
        c += 1
    if S[i] == 'E':
        c -= 1
    dp.append(c)
dp.sort()
print(dp[0])

Submission status: Accepted

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

c = 0
for i in range(1, N):
    if S[i] == 'E':
        c += 1
dp = [{'i': 0, 'c': c}]

for i in range(1, N):
    c = dp[i-1]['c']
    if S[i-1] == 'W':
        c += 1
    if S[i] == 'E':
        c -= 1
    dp.append({'i': i, 'c': c})
dp.sort(key=lambda x:x['c'])
print(dp[0]['c'])

Submission status: Accepted

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

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

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

Sum = []
for i in range(0,N):
    Sum.append(Wsum[i] + Esum[i]-1)

print(min(Sum))

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
ls=[0 if i=='W' else 1 for i in s]
a=sum(ls)
#print(a)
l=[0]*n
for i in range(n):
    if ls[i]==1:
        a-=1
        l[i]=a
    elif ls[i]==0:
        a+=1
        l[i]=a-1
print(min(l))

Submission status: Accepted

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

es,ws = [],[]
if s[0] == 'E':
    es.append(1)
    ws.append(0)
else:
    es.append(0)
    ws.append(1)

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

ans = 9999999
for i in range(n):
    if i == 0:
        ans = min(ans,es[n-1] - es[0])
    else:
        ans = min(ans,ws[i-1] + es[n-1] - es[i])
print(ans)

Submission status: Accepted

Solution code:
def 解():
    iN = int(input())
    sS = input().rstrip()

    aE= [0]*iN #東向かされる人
    for i in range(1,iN):
        if sS[i-1] == "W":
            aE[i] = aE[i-1]+1
        else:
            aE[i] = aE[i-1]
    iMinCost = aE[iN-1]
    iW = 0 #西向かされる人
    for i in range(iN-2,-1,-1):
        if sS[i+1]=="E":
            iW += 1
        iMinCost = min(iMinCost,iW+aE[i])
    print(iMinCost)

解()

Submission status: Accepted

Solution code:
def 解():
    iN = int(input())
    sS = input().rstrip()

    aE= [0]*iN #東向かされる人
    aW= [0]*iN #西向かされる人
    for i in range(1,iN):
        if sS[i-1] == "W":
            aE[i] = aE[i-1]+1
        else:
            aE[i] = aE[i-1]
    iMinCost = aE[iN-1]
    for i in range(iN-2,-1,-1):
        if sS[i+1]=="E":
            aW[i] = aW[i+1]+1
        else:
            aW[i] = aW[i+1]
        iMinCost = min(iMinCost,aW[i]+aE[i])
    print(iMinCost)

解()

Submission status: Accepted

Solution code:
def 解():
    iN = int(input())
    sS = input().rstrip()

    aE= [0]*iN #東向かされる人
    aW= [0]*iN #西向かされる人
    for i in range(1,iN):
        if sS[i-1] == "W":
            aE[i] = aE[i-1]+1
        else:
            aE[i] = aE[i-1]
    for i in range(iN-2,-1,-1):
        if sS[i+1]=="E":
            aW[i] = aW[i+1]+1
        else:
            aW[i] = aW[i+1]

    print(min(map(lambda x,y:x+y,aW,aE)))
解()

Submission status: Accepted

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

west = []
east = []
w = 0
e = 0
for i in range(n):
    if s[i] == "W":
        w += 1
    else:
        e += 1
    west.append(w)
    east.append(e)


ans = float('inf')
for i in range(n):
    count = 0
    if i == 0:
        count = east[n-1] - east[0]
    elif i == n-1:
        count = west[n-2]
    else:
        count = west[i-1] + (east[n-1] - east[i])
    ans = min(ans, count)

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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):
    E[i]=E[i-1]+E[i]
for i in range(N-2,-1,-1):
    W[i]=W[i+1]+W[i]

SUM=[E[i]+W[i] for i in range(N)]
print(N-max(SUM))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
s = lc()

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

ans = lw + re

for i in range(1,n):
    if s[i-1] == "E":
        le += 1
    else:
        lw += 1
        
    if s[i] == "E":
        re -= 1
    else:
        rw -= 1
        
    ans = min(ans, lw+re)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

nisi=[0]*n
for i in range(n):
    if(s[i]=="W"):
        nisi[i]=1

ruiseki=[0]
for i in range(n):
    ruiseki.append(ruiseki[i]+nisi[i])



ans=[]

for i in range(n):
    ans.append(ruiseki[i] + (n-i-1) - (ruiseki[-1] - ruiseki[i+1]) )

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
num_E = []
num_W = []
num_All = []
count_W = 0
count_E = 0

for i in range(N):
  if S[i] == 'W':
    count_W += 1
  if S[N-i-1] =='E':
    count_E += 1
  num_W.append(count_W)
  num_E.append(count_E)

num_E.reverse()
num_All = [x + y for (x, y) in zip(num_W, num_E)]

print(min(num_All)-1)