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:
def main():
    N = int(input())
    S = list(input())

    CW = [0 for i in range(N + 1)]
    CE = [0 for i in range(N + 1)]

    count_w = count_e = 0

    for i in range(N):
        if S[i] == "W":
            count_w += 1
        CW[i + 1] = count_w

    S.reverse()
    
    for i in range(N):
        if S[i] == "E":
            count_e += 1
        CE[i + 1] = count_e

    ans = 10**6
    for i in range(N):
        change = CW[i] - CW[0] + CE[N - (i + 1)] - CE[0]

        if ans > change:
            ans = change

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

    CW = [0 for i in range(N + 1)]
    CE = [0 for i in range(N + 1)]

    count_w = count_e = 0

    for i in range(N):
        if S[i] == "W":
            count_w += 1
        CW[i + 1] = count_w

    S.reverse()
    
    for i in range(N):
        if S[i] == "E":
            count_e += 1
        CE[i + 1] = count_e

    S.reverse()

    ans = 10**6
    for i in range(N):
        w = CW[i] - CW[0]
        e = CE[N - (i + 1)] - CE[0]
        change = w + e

        if ans > change:
            ans = change

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ec = 0
wc = 0
el = []
wl = []
for f,r in zip(s,reversed(s)):
    el.append(ec)
    wl.append(wc)
    if f == 'W':
        wc += 1
    if r == 'E':
        ec += 1

ans = n
for e,w in zip(wl, reversed(el)):
    ans = min(ans, e+w)

print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    S = in_s()

    wsum = [0] * N

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

    for i in range(N - 1):
        wsum[i + 1] += wsum[i]

    ans = 10**9 + 7
    for i in range(N):
        t = N - (i + 1) - (wsum[N - 1] - wsum[i])
        if i != 0:
            t += wsum[i - 1]
        ans = min(ans, t)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

countE = [0]*(N+1)
countW = [0]*(N+1)

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

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

minx = N+1
for i in range(N):
    newv = countW[i] + countE[i]
    if minx > newv:
        minx = newv

print(minx)

Submission status: Accepted

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

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

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

min = N

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

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
e=s.count("E")
w=n-e
ans=e
nume=0
numw=0
for i in range(n):
    if s[i]=="E":
        nume+=1
    else:
        numw+=1
    ans=min(ans,numw+e-nume)
print(ans)

Submission status: Accepted

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

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

if S[0]=='E':
    E[0]=1
else:
    W[0]=1

i=1
while i<N:
    if S[i]=='E':
        E[i]=E[i-1]+1
        W[i]=W[i-1]
    else:
        W[i]=W[i-1]+1
        E[i]=E[i-1]
    i+=1

E=[0]+E
W=[0]+W

ans=N
j=1
while j<=N:
    count=W[j-1]+(E[N]-E[j])
    ans=min(ans,count)
    j+=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = [0]
ans = 10**6
for i,si in enumerate(s):
    l.append(l[i]+(si=="W"))
for i in range(n):
    ans = min(ans,l[i]+(n-1-i)-(l[-1]-l[i+1]))
    #print(i,f"W{l[i]} E{(n-1-i)-(l[-1]-l[i+1])}",s[:i],s[i+1:])
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ss = list(input())

    ans = float("inf")
    ans = [0] * n
    cum_sum_w = [0] * (n+1)
    cum_sum_e = [0] * (n+1)
    for i in range(n):
        if ss[i] == 'W':
            add = 1
        else:
            add = 0
        cum_sum_w[i+1] = cum_sum_w[i] + add
    for i in range(n):
        if ss[i] == 'E':
            add = 1
        else:
            add = 0
        cum_sum_e[i+1] = cum_sum_e[i] + add
    for i in range(n):
        ans[i] = cum_sum_w[i] + (cum_sum_e[n] - cum_sum_e[i+1])
    print(min(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

import sys

DEBUG = False

def read(t):
    return t(sys.stdin.readline().rstrip())


def read_list(t, sep = " "):
    return [t(s) for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


def main():
    n = read(int)
    s = read(str)

    sum_east = [0] * (n + 1)
    sum_west = [0] * (n + 1)
    for i in range(1, n + 1):
        sum_east[i] = (1 if s[i - 1] == "E" else 0) + sum_east[i - 1]
        sum_west[i] = i - sum_east[i]
    dprint(repr(sum_east))
    dprint(repr(sum_west))
    
    ans = 1_000_000
    for i in range(n):
        turn_from_west = sum_west[i]
        turn_from_east = sum_east[n] - sum_east[i + 1]
        if ans > turn_from_east + turn_from_west:
            ans = turn_from_east + turn_from_west
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    S = input()
    east = [0] * (N + 1)
    west = [0] * (N + 1)
    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 - 1, -1, -1):
        if S[i] == "E":
            east[i] = east[i + 1] + 1
        else:
            east[i] = east[i + 1]

    ans = 10**18
    for n in range(N):
        ans = min(ans, west[n] + east[n + 1])
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


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


def z():
    return int(input())


def S():
    return input()


def C(line):
    return [input() for _ in range(line)]


N = z()
S = S()
wester = [0] * N
west_sum = 0
easter = [0] * N
sum_ = [0]*N
east_sum = S.count('E')
for i, s in enumerate(S):
    wester[i] = west_sum
    if (s == 'W'):
        west_sum += 1
    else:
        east_sum -= 1
    easter[i] = east_sum
    sum_[i] = wester[i] + easter[i]
# print(wester)
# print(easter)
print(min(sum_))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
W=[0]*(n+1) #Wについての累積和
E=[0]*(n+1) #Eについての累積和
e=0
w=0
for i in range(n):
    if s[i]=="W":
        w += 1
    else:
        e += 1
    
    W[i+1] = w
    E[i+1] = e
ans = 10**10
for i in range(n):
    tmp = W[i] + (E[n]-E[i+1])
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N = i()
S = list(s())
w = [0]*N
e = [0]*N
for i in range(N):
    if S[i] == 'W':
        w[i] = 1
    if S[i] == 'E':
        e[i] = 1
w = [0]+list(itertools.accumulate(w))
e = [0]+list(itertools.accumulate(e[::-1]))
e.reverse()
ans = 10**9
for i in range(N+1):
    ans = min(ans,w[i]+e[i])
print(ans)

Submission status: Accepted

Solution code:
def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N = ii()
    S = input()
    dp = [0]*N
    dp[0] = S[1:].count('W')
    for i in range(1, N):
        dp[i] = dp[i-1]
        if S[i-1] == 'E':
            dp[i] += 1
        if S[i] == 'W':
            dp[i] -= 1
    print(N-1-max(dp)) 
    


if __name__ == '__main__':
    main()

Submission status: Accepted

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

l = [0]*(n+1)
r = [0]*(n+1)

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ap=[0]*(N+1)
bp=[0]*(N+1)
a=0
b=0
for i in range(N):
  if S[i]=='W':
    a+=1
  else:
    b+=1
  ap[i+1]=a
  bp[i+1]=b
ans=10**9
for j in range(1,N+1):
  ans=min(ap[j-1]+bp[N]-bp[j],ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = readline().decode().rstrip()

cost = [0]*n
cost[0] = s[1::].count('E')
if s[0] == 'W':
    cost[1] += 1

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

print(min(cost))

Submission status: Accepted

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

ecount=s.count("E")
wcount=0
mn=n
for i in range(n):
    if s[i]=="E":
        ecount-=1
        count=ecount+wcount
        if mn>count:
            mn=count
    else:
        count=ecount+wcount
        if mn>count:
            mn=count
        wcount+=1

print(mn)

Submission status: Accepted

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

# 西端の人がリーダーの場合
temp = 0
for j in range(1, N):
    temp += (S[j] == 'E')
    ans = temp

for i in range(1, N): # 西からi番目の人がリーダーの場合
    temp -= (S[i] == 'E')
    temp += (S[i - 1] == 'W')
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, S: str):
    E = [0] * (N+1)
    W = [0] * (N+1)
    for i, s in enumerate(S):
        E[i+1] = E[i] + (1 if s == 'W' else 0)
    for i, s in enumerate(reversed(S)):
        W[N-i-1] = W[N-i] + (1 if s == 'E' else 0)
    return min(e+w for e, w in zip(E, W))


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    print(solve(N, S))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

Solution code:
N = int(input())

ppl = list(input())

buff = []
w = 0
e = ppl[1:].count('E')
for i in range(N):
    if i > 0:
        w += (ppl[i-1] == 'W' and 1 or 0)
    if i > 0:
        e -= (ppl[i] == 'E' and 1 or 0)
    n = w+e
    buff.append(n)

print(min(buff))

Submission status: Accepted

Solution code:
n = int(input())
S = list(input())
left = []
right = []
l = 0
r = 0
wa = []
for i in range(n):
  if i == 0:
    l += 0
    r += 0
    left.append(l)
    right.append(r)
  else:
    if S[i-1] == "W":
      l += 1
    left.append(l)
    if S[n-i] == "E":
      r += 1
    right.append(r)
for j in range(n):
  wa.append(left[j] + right[n-1-j])
print(min(wa))

Submission status: Accepted

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

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

ans = 10 ** 10
for i in range(N):
  if S[i] == "E":
    count -= 1
  if i != 0:
    if S[i - 1] == "W":
      count += 1
  ans = min(ans, count)
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

S = input()

min_num = N+1

W = []
E = []

w_cnt = 0
e_cnt = 0

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

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

print(min(min_num,e_cnt))

Submission status: Accepted

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

#index=0,N+1は初期化に必要
leftwest = np.array([0]*(N+2))
righteast = np.array([0]*(N+2))

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

#print("left",leftwest)
#print('right',righteast)


print(min(leftwest[1:N+1]+righteast[1:N+1]))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a=[]
now=0
for i in s:
    a.append(now)
    if i=="W":
        now+=1
    
now=0
for i in range(n):
    a[n-i-1]+=now
    if s[n-i-1]=="E":
        now+=1
    
a=sorted(a)
print(a[0])

Submission status: Accepted

Solution code:
N=int(input())
S=input()
nagasa=len(S)
D=[]
goukei=0
for i in range(nagasa-1):
    if S[i+1]=="E":
        goukei+=1
    else:
        goukei+=0
#print(goukei)
     
D.append(goukei)
for i in range(nagasa-1):
    if S[i]=="W":
        goukei+=1
    else:
        goukei+=0
    if S[i+1]== "W":
        goukei+=0
    else:
        goukei-=1
    
    D.append(goukei)
#print(D)
print(min(D))

Submission status: Accepted

Solution code:

N = int(input())
S = list(input()) #１文字ずつ
#print('S',S)

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

#print('count:',hidari,migi)
out=hidari+migi
#print('out0:', out)
ld=1  #leader

while ld<N:
    if S[ld-1]=='W' and S[ld]=='E':
        hidari+=1
        migi-=1
    elif S[ld-1]=='W' and S[ld]=='W':
        hidari+=1
    elif S[ld-1]=='E' and S[ld]=='E':
        #print('kokoda!')
        migi-=1
    #print('ld,hidari,migi,out:',ld,hidari,migi,migi+hidari)
    out = min(out,hidari+migi)
    ld+=1

print(out)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
E = [0]*N #i番目より左側に、右向いてる人が何人
W = [0]*N #i番目より右側に、左向いてる人が何人

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

out = [0]*N
for i in range(N):
    out[i] = (N-1) - (W[i]+E[i])
print(min(out))

Submission status: Accepted

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

# check

# 参考：https://qiita.com/IGMML/items/5aa36f7fa03746ed6e6c
# 累積和で求める
int_s = [0] * N
cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)
answers = [0] * N

for i in range(N):
    if S[i] == 'W':
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_w[i + 1] = cum_sum_w[i] + int_s[i]

for i in range(N):
    if S[i] == 'E':
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_e[i + 1] = cum_sum_e[i] + int_s[i]

for i in range(N):
    answers[i] = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
    
print(min(answers))

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
left = [0] * n
right = [0] * n
for i in range(n):
    if s[i] == 'W':
        if i == 0:
            left[0] = 1
        else:
            left[i] = left[i-1] + 1
    elif i != 0:
        left[i] = left[i-1]
for i in range(n-1, -1, -1):
    if s[i] == 'E':
        if i == n - 1:
            right[n-1] = 1
        else:
            right[i] = right[i+1] + 1
    elif i != n - 1:
        right[i] = right[i+1]
ans = right[1]
for i in range(1, n-1):
    ans = min(ans, left[i-1] + right[i+1])
ans = min(ans, left[n-2])
print(ans)

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]
    if S[i] == 'E':
        e[i+1] = e[i] + 1
        w[i+1] = w[i]
ans = 10 ** 6
for i in range(N):
    ans = min(ans, w[i] + e[N] - e[i + 1])
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s = input()
    ans = 0
    temp = 0

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

    temp = ans
    #print(temp)

    for j in range(1,N):
        if s[j-1] == 'W':
            temp += 1

        if s[j] == 'E':
            temp -=1


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

    return ans

print(main())

Submission status: Accepted

Solution code:
def solve(S,N):
    E,W = [0 for _ in range(N)],[0 for _ in range(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]
        W[i] += W[i-1]

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

    return ans

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

    print(solve(S,N))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    NUM = [0] * (N+1)

    for n in range(N):
        NUM[n+1] += NUM[n]
        if S[n] == 'E':
            NUM[n+1] += 1

    ret = MAX
    for n in range(N+1):
        # n人目までのうち左(W)を向いている人数
        #   n - NUM[n]
        # n人目以降に右(E)を向いている人数
        #   NUM[-1] - NUM[n]
        ret = min(ret, n - NUM[n] + (NUM[-1] - NUM[n]))
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    NUM = [0] * (N+1)

    for n in range(N):
        NUM[n+1] += NUM[n]
        if S[n] == 'W':
            NUM[n+1] += 1

    ret = MAX
    for n in range(N+1):
        ret = min(ret, NUM[n] + N - n - (NUM[-1] - NUM[n]))
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

L = [0] * n
R = [0] * n

# 左にある W
for i in range(1, n):
    if s[i - 1] == 'W':
        L[i] = L[i - 1] + 1
    else:
        L[i] = L[i - 1]

# 右にある W
for i in range(n - 2, -1, -1):
    if s[i + 1] == 'E':
        R[i] = R[i + 1] + 1
    else:
        R[i] = R[i + 1]

res = 1 << 29
for i in range(n):
    res = min(res, L[i] + R[i])

print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e_l, e_sum = [], 0
w_l, w_sum = [], 0
for i in range(n):
    if s[i] == 'E':
        e_sum += 1
    else:
        w_sum += 1
    e_l.append(e_sum)
    w_l.append(w_sum)
for i in range(n):
    if i == 0:
        ans = e_l[n-1] - e_l[0]
    elif i == n:
        ans = max(ans, w_l[n-2])
    else:
        ans = min(ans, e_l[n-1] - e_l[i] + w_l[i-1])
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = input()
l = s.count("W")
r = s.count("E")
ans = 0
keep = r
minn = keep
if s[0] == "E":
    ans = 1
for i in range(1, n):
    if s[i - 1:i + 1] == "WW":
        keep += 1
    elif s[i - 1:i + 1] == "WE":
        keep += 0
    elif s[i - 1:i + 1] == "EW":
        keep += 0
    elif s[i - 1:i + 1] == "EE":
        keep -= 1
    if minn > keep:
        minn = keep
print([minn, minn - 1][ans])

Submission status: Accepted

Solution code:
#! env python
# -*- coding: utf-8 -*-

import os
import sys
from itertools import accumulate


# ac_py.main
# Date: 2020/06/14
# Filename: main 
# Author: acto_mini

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

    W = list()
    E = list()
    for i in range(N):
        flag = S[i] == "W"
        W.append(int(flag))
        E.append(int(not flag))

    #
    W_l = accumulate([0] + W[:-1])
    E_r = accumulate([0] + list(reversed(E[1:N])))

    for w, e in zip(W_l, reversed(list(E_r))):
        ans = min(ans, w + e)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

res = 0
for i in range(n):
    res = max(res, cnt_e[i] + cnt_w[i+1])

print(n - res - 1)

Submission status: Accepted

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

ans = s.count("E")

tmp = ans

for i in s:
    tmp += (-1 if i == "E" else 1)
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
n,s=open(0);a=t=s.count('E')
for i in s:t+=(-1 if i=='E' else 1);a=min(a,t)
print(a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 21:52:40 2020

@author: naoki
"""

N = int(input()) # 並んでいる人数N

S = input() # 向いている方向
    
change = [0 for i in range(N)] # 向きを変えた回数を格納
change[0] = S.count("E",1,N)
for i in range(1,N):
        if S[i-1] == "W" and S[i] == "E":
            change[i] = change[i-1]
        elif S[i-1] == "W" and S[i] != "E": 
            change[i] = change[i-1] + 1
        elif S[i-1] != "W" and S[i] == "E":
            change[i] = change[i-1] - 1
        else:
            change[i] = change[i-1]
change.sort()
print(change[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = [0]
w = [0]
for i in range(len(s)):
    if s[i] == 'E':
        e.append(e[i]+1)
        w.append(w[i]+0)
    if s[i] == 'W':
        e.append(e[i]+0)
        w.append(w[i]+1)

ans = float('INF')
for i in range(n):
    iya = (w[i])+(e[n] - e[i+1])
    ans = min(ans,iya)
print(ans)

Submission status: Accepted

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

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

for i in range(1, n + 1):
    ref = e[i + 1]
    ref += w[i - 1]
    ans = min(ans, ref)

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    
    a = [0] * (n)
    b = [0] * (n)
    for i in range(1, n):
        if s[i - 1] == 'W':
            a[i] = a[i - 1] + 1
        else:
            a[i] = a[i - 1]
        if s[n- i] == 'E':
            b[n - i - 1] = b[n - i ] + 1
        else:
            b[n- i - 1] = b[n - i ]
    ans = 10**9
    for aa, bb in zip(a, b):
        ans = min(ans, aa++bb)
    print(ans) 
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    #csw[i]:='W'のi番目までの登場回数
    #cse[i]:='E'のi番目までの登場回数
    csw = [0]
    cse = [0]
    for i in range(N):
        if S[i] == 'W':
            csw.append(csw[i]+1)
            cse.append(cse[i])
        else:
            csw.append(csw[i])
            cse.append(cse[i]+1)
    ans = int(1e6)
    for i in range(N):
        #Sのi番目の文字を見たときSの区間[0,i）にある'W'の数と区間(i, N)にある'E'の数の合計が「向く方向を変える人数」である
        #i番目はリーダーなので数えないことに注意
        ans = min(cse[N]-cse[i+1] + csw[i] - csw[0], ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
s = [_ for _ in input()]

e = [0]
w = [0]

for si in s:
  if si == 'E':
    e.append(e[-1]+1)
    w.append(w[-1])
  if si == 'W':
    w.append(w[-1]+1)
    e.append(e[-1])

# print(e)
# print(w)

mn = 10**7

for i in range(n):
  cost = w[i] + e[n]-e[i+1]
  mn = min(cost,mn)

print(mn)

Submission status: Accepted

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


W = [[] for _ in range(N)]
E = [[] for _ in range(N)]

prev_w = 0
prev_e = 0
for i, s in enumerate(S):
    if s == 'W':
        W[i] = prev_w + 1
    else:
        W[i] = prev_w
    prev_w = W[i]

for i, s in enumerate(S[::-1]):
    if s == 'E':
        E[-i] = prev_e + 1
    else:
        E[-i] = prev_e
    prev_e = E[-i]
        
ans = len(S)
for i in range(N):
    if i == 0:
        cost = E[i+1]
    elif i == N-1:
        cost = W[i-1]
    else:
        cost = W[i-1] + E[i+1]
    ans = min([cost, ans])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
e = S.count('E')
left = 0
right = e - (1 if S[0] == 'E' else 0)
ans = left + right

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

Submission status: Accepted

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

Submission status: Accepted

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

sum_W = [0] * (N+1)
sum_E = [0] * (N+1)

for i in range(N):
    if S[i] == "W":
        sum_W[i+1] = sum_W[i] + 1
        sum_E[i+1] = sum_E[i]
    else:
        sum_W[i+1] = sum_W[i]
        sum_E[i+1] = sum_E[i] + 1

answers = []
for i in range(N):
    answer = sum_W[i] + (sum_E[N] - sum_E[i+1])
    answers.append(answer)

print(min(answers))

Submission status: Accepted

Solution code:
n=int(input())
#a,b=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

s=list(input())

wtotal=s.count("W")
etotal=s.count("E")
wl=[];el=[]
wcount=0
ecount=0
for i in range(n):
    if s[i]=="E":
        ecount+=1
    el.append(ecount)
for i in range(n-1,-1,-1):
    if s[i]=="W":
        wcount+=1
    wl.append(wcount)

wl.reverse()
mn=etotal-el[0]
for i in range(n):
    mn=min(mn,wtotal-wl[i]+etotal-el[i])

print(mn)

Submission status: Accepted

Solution code:

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

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

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

total_list = []
for i in range(N):
    to_E = count_W[i]
    to_W = count_E[i]
    total_list.append(to_E + to_W)

print(min(total_list))