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:
# coding: utf-8
# hello worldと表示する
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
s=SI()
east=[]
west=[]
state=s[0]
count=1
if s[0]=="W":
    east.append(0)
for i in range(1,n):
    if s[i]==state:
        count+=1
    else:
        if state=="W":
            west.append(count)
            count=1
            state="E"
        else:
            east.append(count)
            count=1
            state="W"
if state=="W":
    west.append(count)
else:
    east.append(count)
if s[-1]=="E":
    west.append(0)
#print(east)
#print(west)
x=len(east)
E=sum(east)
W=sum(west)
anse=[]
answ=[]
vale=E-east[0]
for i in range(1,x):
    anse.append(vale)
    vale+=(west[i-1]-east[i])
anse.append(vale)
'''if east[0]==0:
    anse[0]=inf'''
print(min(anse))

Submission status: Accepted

Solution code:
"""
3*10**5
単純に両側の差を全探索だと間に合わないO(N*2)

情報を先に保存しておきそれを使う
"""

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

E_list = [0] * N 
W_list = [0] * N

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

n = 0
for i in reversed(range(N-1)):
    if S[i+1] == "E":
        n += 1
        E_list[i] = n
    else:
        E_list[i] = n

print(min([e+w for e,w in zip(E_list, W_list)]))

Submission status: Accepted

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

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

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

print(min([e + w for e, w in zip(e_list, w_list)]))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = list(input())
c = Counter(S)
left_facing_west = 0
right_facing_east = c['E']
ans = float('inf')
for s in S:
    if s == 'W':
        ans = min(ans,left_facing_west+right_facing_east)
        left_facing_west += 1
    else:
        right_facing_east -= 1
        ans = min(ans,left_facing_west+right_facing_east)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
S_rep = S.replace('W', '1').replace('E', '0') 

lst1 = [int(S_rep[0])]
for s in S_rep[1:]:
    lst1.append(lst1[-1] + int(s))
ans = [N - lst1[-1] + int(S_rep[0]) - 1]
for i in range(1, N):
    ans.append(lst1[i-1] + N - i - 1 - lst1[-1] + lst1[i])

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input().strip())
s = input().strip()
arr = [0] * n
east = 0
west = 0

for i in range(n):
    arr[i] = east
    if s[i] == 'E':
        east += 1
for i in range(n - 1, -1, -1):
    arr[i] += west
    if s[i] == 'W':
        west += 1
ma = max(arr)
idx = i
for i in range(n):
    if arr[i] == ma:
        idx = i
if idx == (n - 1):
    print(s[:idx].count('W'))
else:
    print(s[:idx].count('W') + s[(idx + 1):].count('E'))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
arr = input()
west_side = Counter()
east_side = Counter(arr)
costs = []

for i in range(N):
    leader = arr[i]
    east_side[leader] -= 1

    costs.append(west_side['W'] + east_side['E'])
    west_side[leader] += 1

print(min(costs))

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc098/tasks/arc098_a
n=int(input())
s=input()

"""ans=100000000000
for i in range(n):
    count=0
    for j in range(n):
        if i==j:
            continue

        if (i<j)&(s[j]=="E"):
            count+=1
        elif (j<i)&(s[j]=="W"):
            count+=1

    ans=min(ans,count)

print(ans)"""#O(N^2)

count_W=[]
for i in range(n):
    count_W.append(0)

if s[0]=="W":
    count_W[0]=1
else:
    count_W[0]=0

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

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

print(ans)

Submission status: Accepted

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

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

ans = 10 ** 9
for i in range(n):
    if s[i] == 'E':
        e = count_e[-1] - count_e[i]
        w = count_w[i]
    else:
        e = count_e[-1] - count_e[i]
        w = count_w[i] - 1
    ans = min(ans,e+w)
print(ans)

Submission status: Accepted

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

e = 1
w = 1
count_e = [0] * n
count_w = [0] * n
for i in range(n):
    if i == 0:
        if s[i] == 'E':
            count_e[i] = e
            e += 1
        else:
            count_w[i] = w
            w += 1
        continue
    if s[i] == 'E':
        count_e[i] = e
        count_w[i] = count_w[i-1]
        e += 1
    else:
        count_w[i] = w
        count_e[i] = count_e[i-1]
        w += 1

ans = 10 ** 9
for i in range(n):
    if s[i] == 'E':
        e = count_e[-1] - count_e[i]
        w = count_w[i]
    else:
        e = count_e[-1] - count_e[i]
        w = count_w[i] - 1
    ans = min(ans,e+w)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
W=[0] * N
E=[0] * N

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

tmp=W[0]+E[0]
for i in range(1, N):
    if tmp > W[i]+E[i]:
        tmp = W[i]+E[i]

print(tmp)

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
ans=0

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

cnt=ans

for i in range(n):
    if s[i:i+2]=='WW':
        cnt=cnt+1
    elif s[i:i+2]=='EE':
        cnt=cnt-1
    else:
        0
        
    #print('i,s[i:i+2],ans,cnt',i,s[i:i+2],ans,cnt)
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Wl = []
El = []
c = 0
for i in range(N):
    if S[i] == 'W':
        c += 1
    Wl.append(c)
c = 0
for i in range(N):
    if S[-(1 + i)] == 'E':
        c += 1
    El.append(c)
El.reverse()
Amin = min(El[1], Wl[N - 2])
for i in range(1, N - 2):
    A = Wl[i - 1] + El[i + 1]
    Amin = min(Amin, A)
print(Amin)

Submission status: Accepted

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

w_e = [0] * n
e_w = [0] * n
ans = []

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

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

for i in range(n):
    ans.append(w_e[i] + e_w[i])

print(min(ans))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
ln = list(map(int, input().replace('W', '0 ').replace('E', '1 ').split()))

# 先頭だけ先に計算（do-while相当）
w_sum = 0                           # 西側で振り返る人
sum_min = e_sum = sum(ln) - ln[0]   # 東側で振り返る人

for i in range(1, n-1):
    #sum_min = min(sum_min, sum(ln[i+1:n]) + (i - sum(ln[0:i])))
    w_sum += 1 if ln[i-1] == 0 else 0
    e_sum -= ln[i]
    sum_min = min(sum_min, w_sum + e_sum)

# 末尾も個別に計算
else:
    w_sum += 1 if ln[n-1] == 0 else 0
    sum_min = min(sum_min, w_sum)


print(sum_min)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
S = input()
left_W = 0
right_E = S.count("E")
ans = N

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

def main():
    N = int(readline())
    S = readline().rstrip()
    e_right = S.count('E')
    w_left = 0
    res = N
    
    for i in range(N):
        if S[i] == 'E':
            e_right -= 1
            res = min(res, e_right + w_left)
        else:
            res = min(res, e_right + w_left)
            w_left += 1
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

def main():
    N = int(readline())
    S = readline().rstrip()
    e_right = S.count('E')
    w_right = S.count('W')
    e_left, w_left = 0, 0
    res = N
    for i in range(N):
        if S[i] == 'E':
            e_right -= 1
            res = min(res, e_right + w_left)
            e_left += 1
        else:
            w_right -= 1
            res = min(res, e_right + w_left)
            w_left += 1
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
lCurrent=rCurrent=0
l=[0]*n
r=[0]*n
ans=[0]*n

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

for i in reversed(range(1,n)):
    if s[i]=="E":
        rCurrent+=1
    r[i-1]=rCurrent

ans=[x+y for x,y in zip(l,r)]
print(min(ans))

Submission status: Accepted

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

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

# e,wの累積和を記録
for i, d in enumerate(s):
    if d == "W":
        w[i+1] = w[i] + 1
        e[i+1] = e[i]
    else:
        e[i+1] = e[i] + 1
        w[i+1] = w[i]

mnum = n
for i, d in enumerate(s):
    mnum = min(mnum, w[i] + e[n] - e[i+1])

print(mnum)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
west = []
east = []
count_w = 0
count_e = 0

for i in s:
    if i == "W":
        count_w+=1
    west.append(count_w)
for j in reversed(s):
    if j == "E":
        count_e +=1
    east.append(count_e)
r_east = [k for k in reversed(east)]
   
attention = [west[i] + r_east[i] for i in range(n)]
print(min(attention)-1)

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    S = list(_in[1])   # type:str
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    dp = [0]*N
    for i in range(N):
        if i==0:
            dp[i] = len([s for s in S[1:] if s=='E'])
        else:
            dp[i] = dp[i-1] + (1 if S[i-1]=='W' else 0) - (1 if S[i]=='E' else 0)
    cnt = min(dp)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

e = 0
ans = w = S.count('E')
for i in S:
    if i == 'E':
        w -= 1
    else:
        e += 1
    ans = (min(ans, e+w))

print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

left = [0] * (N + 2)
right = [0] * (N + 2)

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

ans = N
for i in range(1, N + 1):
    ans = min(ans, N - left[i] - right[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
change = s.count("E")
ans = change
i_o = "dum"
for i in s:
    #print(change)
    if i == "E":
        change -= 1
    if i_o == "W":
        change += 1
    ans = min(change,ans)
    i_o = i
print(ans)

Submission status: Accepted

Solution code:
def main():
    from itertools import accumulate
    n = int(input())
    s = input()
    s = s.replace("W", "1")
    s = s.replace("E", "0")
    s = [int(x) for x in s]

    t = list(accumulate(s))
    u = t[-1]
    ans = n

    for i, (j, k) in enumerate(zip(s, t)):
        m = (k - j) + (n - 1 - i) - (u - k)
        ans = min(ans, m)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from itertools import accumulate
    n = int(input())
    f = lambda x: 1 if x == "W" else 0
    *s, = map(f, list(input()))
    t = list(accumulate(s))
    u = t[-1]
    ans = n

    for i, (j, k) in enumerate(zip(s, t)):
        m = (k - j) + (n - 1 - i) - (u - k)
        ans = min(ans, m)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

to_east = 0
to_west = s.count('E', 1)
ans = to_east+to_west

for i in range(1, n):
  if s[i-1]=='W':
    to_east += 1
  if s[i]=='E':
    to_west -= 1
  ans = to_east+to_west if ans>to_east+to_west else ans
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [p for p in input()]
w = [0]
e = [0]
mn = float("inf")
for i in range(n):
  if s[i] == 'W':
  	w.append(w[i]+1)
  else:
    w.append(w[i])
  e.append(i+1-w[i+1])
for i in range(n):
  if w[i]+e[n]-e[i+1] < mn:
    mn = w[i]+e[n]-e[i+1]
print(mn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    S=readline().strip()

    t_left=0
    t_right=N-1
    left=0
    right=0
    for i in range(1,N):
        if S[i]=="W":
            right+=1

    res=0
    min_res=N
    for i in range(N):
        res=abs(t_left-left)+abs(t_right-right)
        min_res=min(min_res,res)
        if i!=N-1:
            t_left+=1
            t_right-=1
            if S[i]=="E":
                left+=1
            if S[i+1]=="W":
                right-=1

    print(min_res)
if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
制約
2 ≤ N ≤ 3×10^5(300,000)

入力例 3
8
WWWWWEEE

出力例 3
3

考え方：
Eを0, Wを1と置くと、
0000000X11111111 と並ぶようにすればよい。
そのために、値を変える数が最小になるリーダーの位置を調べればよい

-> 左端のW(West), 右端のE(East)はほぼ必ず向きを変える必要がある。


A. 全パターン試す
ある地点より左のWの数 + 右のEの数　の合計が最小になるパターンを求める（文字一致でカウント）
-> TLE

B. 全パターン
str.count('W')でやる
-> TLE

C. 「累積和」(cumulative sum)というアルゴリズムを使わないと解けない問題。学習して、使う！


'''

# from collections import deque # キュー、スタックに利用

# sys.stdin.readline() # 標準入力の高速化に利用
import sys
input = sys.stdin.readline

is_debug = False


def main():
    '''メイン処理'''
    n = int(input())
    sss = input()

    s_list = list(sss)

    W_sums = [0] * n # 累積和を入れるリスト
    E_sums = [0] * n # 累積和を入れるリスト
    W_sum = 0 # 左端から数えて、向きを変える必要のあるWの数
    E_sum = 0 # 右端から数えて、向きを変える必要のあるWの数
    
    if is_debug:
        print(W_sums)
    
    for i in range(n):
        if s_list[i] == 'W':
            W_sum += 1
        W_sums[i] = W_sum
        if s_list[n-i] == 'E':
            E_sum += 1
        E_sums[(n-1)-i] = E_sum

    if is_debug:
        print(W_sums)
        print(E_sums)
    
    # W_sums, E_sumsを用いて、向きを変える人数が最小となる値を見つける
    min_cost = float('inf')
    for i in range(n):
        # Wを振り向かせる人数
        if i == 0:
            W_cost = 0
        else:
            W_cost = W_sums[i-1]
        # Eを振り向かせる人数
        if i == n-1:
            E_cost = 0
        else:
            E_cost = E_sums[(i-1)+1]

        if is_debug:
            print(i, W_cost, E_cost)

        cost = W_cost+E_cost
        if cost < min_cost:
            min_cost = cost
    
    print(min_cost)

if __name__ == "__main__":
    # ファイル実行時に、main()関数を実行
    main()

Submission status: Accepted

Solution code:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

wl = [0]*N
el = [0]*N
wr = [0]*N
er = [0]*N

if S[0] == 'W':
    wl[0] = 1
    el[0] = 0
else:
    wl[0] = 0
    el[0] = 1

if S[-1] == 'W':
    wr[0] = 1
    er[0] = 0
else:
    wr[0] = 0
    er[0] = 1

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

    if S[-i-1] == 'W':
        wr[i] = wr[i-1] + 1
        er[i] = er[i-1]
    else:
        wr[i] = wr[i-1]
        er[i] = er[i-1] + 1

# print(wl)
# print(el)
# print(wr)
# print(er)

res = N
for i in range(N):
    if i == 0:
        val = er[N-i-1]
    elif i == N-1:
        val = wl[i-1] 
    else:
        val = wl[i-1] + er[N-i-1]
    # print(i, val)
    if res > val:
        res = val

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
S = input()

ans = ans_cur = S.count("E")
for c in S:
    if c == "E":
        ans_cur -= 1
    else:
        ans_cur += 1
    ans = ans if ans <= ans_cur else ans_cur
print(ans)

Submission status: Accepted

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

l = 0
r = S.count("E", 1, len(S))
ans = l + r
for i in range(1, len(S)):
    if S[i] == "E":
        r -= 1
    if S[i - 1] == "W":
        l += 1

    ans = ans if ans <= l + r else l + r

print(ans)

Submission status: Accepted

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

print(min(dp))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
s=input()
d=[0]
for i in s:
  if i=="W":
    d.append(1)
  else:
    d.append(0)

accum=list(itertools.accumulate(d))
ans=10**9
count=0
for i in range(1,n+1):
  count+=accum[i-1]-accum[0]
  #左のWの数
  count+=(n-i)-(accum[-1]-accum[i])
  #右のEの数
  if ans>count:
    ans=count
  count=0
print(ans)

Submission status: Accepted

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

    all_east = 0
    all_west = 0

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

    count = 10 ** 6

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

    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = input()
pre_s = S[0]
sum1 = sum(s == 'E' for s in S)
res = sum1
if S[0] =='E':
    sum1-=1
    
for i in range(1,len(S)):
    if pre_s =='W':
        sum1 +=1
    if S[i]=='E':
        sum1 -=1
    if sum1 < res:
        res =sum1
    pre_s = S[i]
print(res)

Submission status: Accepted

Solution code:
n=int(input())
A=[0]*n
S=input()

for i in range(n):
    if S[i]=="E":
        A[i]=1
E=sum(A)
e=E

ans=n+10
for i in range(n):
    if A[i]==1:
        e-=1
        ans=min(e+i-(E-e-1),ans)
    else:
        ans=min(e+i-(E-e),ans)
print(ans)

Submission status: Accepted

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

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

for i in range(n):
    if i == 0:
        if s[i] == 'W':
            w[i] = 1
            e[i] = 0
        else:
            w[i] = 0
            e[i] = 1
    else:
        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 = 10 ** 10
for i in range(n):
    if i == 0:
        ans = min(ans, e[-1] - e[0])
    elif i == n:
        ans = min(ans, w[-1])
    else:
        right = e[-1] - e[i]
        left = w[i - 1]
        ans = min(ans, left + right)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
W = 0
E = S[1:].count('E')
Turn = []
Turn.append(E)

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

print(min(Turn))

Submission status: Accepted

Solution code:
# coding: utf-8

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


def main():
    N = int(input())
    S = input()
    
    ans = len([1 for s in S[1:] if s == "E"])
    now = ans
    for i in range(1, N):
        now += 1 if S[i-1] == "W" else 0
        now -= 1 if S[i] == "E" else 0

        if now < ans:
            ans = now
    
    return ans


print(main())

Submission status: Accepted

Solution code:
n=int(input())
s=input()
rw=s.count("W")
re=s.count("E")
lw=0
le=0
now=""
m=10000000000000000000
for i in range(n):
    if now=="W":lw+=1
    if now=="E":le+=1
    if s[i]=="W":rw-=1
    if s[i]=="E":re-=1
    m=min(m,lw+re)
    now=s[i]
print(m)

Submission status: Accepted

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

E = 0
W = S.count("W")
a, ans = 0, len(S)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
#h, w = list(map(int, input().split()))
# b = list(map(int, input().split()))
s = input()
countE = 0
countW = s.count("W")
ans = len(s)
min_idx = 0

for i in range(len(s)):
    if s[i] == "W":
        countW -= 1
    total = int(len(s))-1-i-countW + i-countE
    if total < ans:
        ans = total
    if s[i] == "E":
        countE += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cum_R = [0]*(n+1)
for i in range(n):
    cum_R[i+1] = cum_R[i]+(s[i] == 'E')

INF = 10**18
ans = INF
for i in range(n):
    cnt_L = i-cum_R[i]
    cnt_R = cum_R[n]-cum_R[i+1]
    ans = min(ans, cnt_L+cnt_R)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt_R = s.count('E')
cnt_L = 0
INF = 10**18
ans = INF
for x in s:
    if x == 'W':
        ans = min(ans, cnt_L+cnt_R)
        cnt_L += 1
    else:
        cnt_R -= 1
        ans = min(ans, cnt_L+cnt_R)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
dp_L = [0]*(n+1)
dp_R = [0]*(n+1)

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

for i in reversed(range(n)):
    dp_R[i] = dp_R[i+1]+[0, 1][s[i] == 'E']
    
print(min(L+R for L, R in zip(dp_L, dp_R)))

Submission status: Accepted

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

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

cnt = 0
for i in reversed(range(n)):
    if s[i] == 'E':
        cnt += 1
    dp[i] += cnt
print(min(dp))

Submission status: Accepted

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

for i in range(n):
    if s[i] == "E":
        cum_sum[i+1] += 1
    cum_sum[i+1] += cum_sum[i]

INF = 10**18
ans = INF
for i in range(1, n+1):
    ans = min(ans, (((i-1)-cum_sum[i-1])+(cum_sum[n]-cum_sum[i])))

print(ans)

Submission status: Accepted

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

S_W = [0]*(N+1)
S_E = [0]*(N+1)

A_W = []
A_E = []

for i in range(N):
    if S[i]=="W":
        A_W.append(1)
        A_E.append(0)
    else:
        A_W.append(0)
        A_E.append(1)
for i in range(len(S)):
    S_W[i+1] = S_W[i] + A_W[i]
    S_E[i+1] = S_E[i] + A_E[i]

ans = float('inf')
for i in range(len(S)):
    e_count = S_E[len(S_E)-1]-S_E[i+1]
    w_count = S_W[i]
    ans = min(ans, e_count+w_count)
   
print(ans)

Submission status: Accepted

Solution code:

n=int(input())
s=input()
lens=len(s)

ec=[0]*lens
wc=[0]*lens
if s[0]=="W":
    wc[0]=1
else:
    ec[0]=1
for i in range(1,len(s)):
    if s[i]=="W":
        wc[i]=wc[i-1]+1
        ec[i]=ec[i-1]
    else:
        wc[i]=wc[i-1]
        ec[i]=ec[i-1]+1
        
cmin=min(ec[lens-1]-ec[0],wc[lens-2])
for i in range(1,n-1):
    c=ec[lens-1]-ec[i]+wc[i-1]    
#    print(i,c,cmin,ec[lens-1]-ec[i],wc[i-1])    
    cmin=min(cmin,c)
print(cmin)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
east = [0]*N
west = [0]*N
for i in range(N):
    s = S[i]
    if i == 0:
        if s == "E":
            east[i] = 1
        else:
            west[i] = 1
    else:

        if s == "E":
            east[i] = east[i-1]+1
            west[i] = west[i-1]
        else:
            east[i] = east[i-1]
            west[i] = west[i-1]+1
Min = 10000000000
for i in range(N):
    if i == 0:
        num = east[N-1]-east[0]
    elif i == N-1:
        num = west[N-2]
    else:
        num = east[N-1]-east[i]+west[i-1]
    Min = min(Min,num)
print(Min)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
INF = 10**12
li = [INF]*n
c = 0
for i in range(len(s)):
    if i == 0:
        continue
    else:
        if s[i] == 'E':
            c+=1
li[0] = c
for i in range(n):
    if i == 0:
        continue
    else:
        li[i] = li[i-1]
        if s[i] == 'E':
            li[i] -= 1
        if s[i-1] == 'W':
            li[i] += 1

print(min(li))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
S = input()[:-1]
acc = [0]*(N+1)

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

ans = 10**18

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

print(ans)

Submission status: Accepted

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

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

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

for i in range(N):
  oder = min(oder, l[i]+r[i])

print(oder)

Submission status: Accepted

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

oder = N
l = [0 for _ in range(N)]
r = [0 for i in range(N)]

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

for i in range(N):
  oder = min(oder, l[i]+r[i])

print(oder)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    S=list(input())
    S_W=[0]*(N+1)
    S_E=[0]*(N+1)
    for i in range(N):
        S_W[i+1]=S_W[i]+(S[i]=='W')
        S_E[i+1]=S_E[i]+(S[i]=='E')
    ans=INF
    for i in range(N):
        ans=min(ans,S_W[i]+(S_E[N]-S_E[i+1]))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w_c=0
e_c=0
ans=n
for i in s:
  if i == 'E':
    e_c+=1
for i in s:
  if i == 'E':
    e_c-=1
  k=w_c+e_c
  if ans > k:
    ans = k
  if i == 'W':
    w_c+=1
print(ans)

Submission status: Accepted

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

# E リーダー W
e = 0
w = s[1:].count("W")
ans = e+w
for i in range(1,n):
    if s[i-1] == "E":
        e += 1
    if s[i] == "W":
        w -= 1
    if ans < e + w: ans = e+w
print(n-ans-1)

Submission status: Accepted

Solution code:
# coding: utf-8
    
def main():
    N = int(input())
    S = input()[:N]

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

    print(min_count)

if __name__ == '__main__':
    main()