Problem:


Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

2 \leq N \leq 3 \times 10^5

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Accepted

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

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

Submission status: Accepted

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

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

val = (n-1 - (A[-1] - A[0]))
min_ = min(min_, val)
for i in range(1, n):
  val = A[i-1] + (n-1 - i - (A[-1] - A[i]))
  min_ = min(val, min_)
print(min_)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
e = w = 0
m = 0
t = 0
for i in range(N):
    if S[i] == "W":
        w += 1
    else:
        e += 1
for i in range(N):
    if t == 1:
        w += 1
        t = 0
    if S[i] == "W":
        w -= 1
    else:
        t = 1
    if m < w:
        m = w
print(N-m-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = float("inf")
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

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

print(ans)

Submission status: Accepted

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

cl = [0] * N
cl[0] = S[1:].count('E')

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

print(min(cl))

Submission status: Accepted

Solution code:
import collections
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        S = input().rstrip()

        w_cnt = 0
        e_cnt = 0
        w = collections.deque()
        e = collections.deque()
        for i in range(N):
            if S[i] == 'W':
                w_cnt += 1
            w.append(w_cnt)

            if S[-1 * (i + 1)] == 'E':
                e_cnt += 1
            e.appendleft(e_cnt)
        w.appendleft(0)
        e.append(0)
        w.pop()
        e.popleft()

        ans = N
        for _ in range(N):
            ans = min(ans, w.popleft() + e.popleft())
        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

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

len_e = 0
len_w = 0
for i in range(N):
    if S[i] == 'W':
        len_e += 1
    else:
        len_w += 1
# 今東の総数が求まった
ans = [[0, len_e]]  # 西の数が求まった
for i in range(N):
    if S[i] == 'W':
        ans.append([ans[-1][0], ans[-1][1]-1])
    else:
        ans.append([ans[-1][0]+1, ans[-1][1]])

hoge = 1e+9
for a in ans:
    hoge = min(hoge, len_w+len_e-sum(a))

print(hoge)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = 0
for i in range(1,len(S)):
    if S[i] == 'E':
        a += 1
ans = a
for i in range(1,len(S)):
    if S[i] == 'E':
        a -= 1
    if S[i-1] == 'W':
        a += 1
    if a < ans:
        ans = a
print(ans)

Submission status: Accepted

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

e = S.count('E')
w = 0
ans = e

for s in S:
    if s=='E':
        e -= 1
    elif s=='W':
        w += 1
    ew = e + w
    ans = min(ans, ew)

print(ans)

Submission status: Accepted

Solution code:
num = input()
#direction = input()
ar_direction = list(input())

res = []

west = False
last_val = len([d for d in ar_direction[1:] if d == 'E'])


for i, d in enumerate(ar_direction):
  current_res = last_val
  if west:
    current_res += 1
  
  west = d == 'W'

  if not i == 0 and not west:
    current_res -= 1

  res.append(current_res)
  last_val = current_res

print(min(res))

Submission status: Accepted

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

east = [0]*N
west = [0]*N
for i in range(N):
    if S[i] == 'E':
        east[i] = 1
    else:
        west[i] = 1

sum_east = [0]*(N+1)
sum_west = [0]*(N+1)
for i in range(N):
    sum_west[i+1] = sum_west[i] + west[i]
    sum_east[N-i-1] = sum_east[N-i] + east[N-i-1]
#print(sum_west, sum_east)

ans = 10**6
for i in range(N):
    ans = min(ans, sum_west[i] + sum_east[i+1])
    #print(ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    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)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
W = [0] * (N)
E = [0] * (N)
count = 0
ans = N + 1
for i in range(N):
  if S[i] == "W":
    count += 1
  W[i] = count
#print(W)
count = 0
for id,i in enumerate(range(N-1,-1,-1)):
  if S[i] == "E":
    count += 1
  E[id] = count
#print(E)
for leader in range(N):
  if leader == 0:
    ans = min(ans,E[-1])
  if leader == N - 1:
    ans = min(ans,W[-1])
  else:
    ans = min(ans,W[leader-1]+E[N-leader-2])
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
s=list(input())
ans=10**6
tmp=collections.Counter(s[1:])['E']
ans=min(tmp,ans)
i=1
while i<n:
  if s[i-1]=='W':
    tmp+=1
  if s[i]=='E':
    tmp-=1
  ans=min(tmp,ans)
  i+=1

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    s=input()
    A=[0]*n # A[i]: i番目までのWの数
    count=0
    for i in range(n):
        if s[i]=='W': count+=1
        A[i]=count
    ans=INF
    for i in range(n):
        if i==0:
            ans=min(ans,n-1-(A[n-1]-A[0]))
        elif i==n-1:
            ans=min(ans,A[n-2])
        else:
            ans=min(ans,A[i-1]+n-i-1-(A[n-1]-A[i]))
    print(ans)
resolve()

Submission status: Accepted

Solution code:
n = int(input())
S = input()
e_sum = [0] * n
w_sum = [0] * n
ew_sum = [0] * n
for i in range(2, n + 1):
    e_sum[-i] = e_sum[-i + 1] + 1 if S[-i + 1] == 'E' else e_sum[-i + 1]
    ew_sum[-i] = e_sum[-i]
for i in range(1, n):
    w_sum[i] = w_sum[i - 1] + 1 if S[i - 1] == 'W' else w_sum[i - 1]
    ew_sum[i] += w_sum[i]

print(min(ew_sum))

Submission status: Accepted

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

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

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

wr = w[::-1]

res = 999999
for i in range(n):
    tmp = (i-e[i]) + (n-i-1-wr[i])
    res = min(res, tmp)

print(res)

Submission status: Accepted

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

# 左端からリーダーにしていく
# リーダーを1つ右隣に移すとき、振り向く合計人数は、リーダーと右隣の人の向きが(W,W)なら+1、(E,E)なら-1、(E,W)or(W,E)なら変化なし
n_turn_list = [S[1:].count("E")] # 左端リーダーのときに振り向く人数

for leader_position in range(N-1): # 右端の1つ左まで
    if S[leader_position+1] == "W" and S[leader_position] == "W":
        n_turn_list.append(n_turn_list[leader_position]+1)
    elif S[leader_position+1] == "E" and S[leader_position] == "E":
        n_turn_list.append(n_turn_list[leader_position]-1)
    else:
        n_turn_list.append(n_turn_list[leader_position])

print(min(n_turn_list))

Submission status: Accepted

Solution code:
_ = input()
S = 'E'+input().strip()+'W'

E = [0]*len(S)
now = 0
for i in range(len(S)):
  E[i] = now + int(S[i]=='E')
  now = E[i]

W = [0]*len(S)
now = 0
for i in range(len(S)):
  W[i] = now + int(S[i]=='W')
  now = W[i]

ans = float('inf')
# i人目をリーダーにする
for i in range(1,len(S)):
  ans = min(ans, W[i-1]+E[-1]-E[i])
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
WEST = [0]*N
EAST = [0]*N

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

ans = 100000000
for i in range(N):
    if i == 0:
        n = EAST[1]
    elif i == N-1:
        n = WEST[N-2]
    else:
        n = WEST[i-1]+EAST[i+1]
    if ans > n:
        ans = n

print(ans)

Submission status: Accepted

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

ss1 = S[1:].count('E')
ss = ss1

for i in range(1, N):
  ss1 = ss1 + (S[i-1]=='W') - (S[i]=='E')
  if ss1 < ss:
    ss = ss1
  
print(ss)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ma = s[1:].count("E")
maa = ma

for i in range(n-1):
  if s[i] == "W" and s[i+1] == "W":
    ma += 1
  elif s[i] == "E" and s[i+1] == "E":
    ma -= 1
    
  maa = min(maa,ma)
  
print(maa)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
wcou=0
answer=0

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

answer=wcou
if s[0]=='E':
    answer-=1

now=answer
#print(answer)

for i in range(n-1):
    if s[i]=='W':
        now+=1
    if s[i+1]=='E':
        now-=1
    if now<=answer:
        answer=now
    #print(now)

print(answer)

Submission status: Accepted

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

left = [0]
for c in s[:-1]:
  if c == 'W':
    left.append(left[-1] + 1)
  else:
    left.append(left[-1])

right = [0]
for c in s[:0:-1]:
  if c == 'E':
    right.append(right[-1] + 1)
  else:
    right.append(right[-1])
right = right[::-1]

sums = [i + j for i, j in zip(left, right)]
mini = min(sums)
print(mini)

Submission status: Accepted

Solution code:
# PDF解説を参考に
N = int(input())
S = input()

ans = S[1:N].count("E")
mn_ans = ans
for i in range(1, N):
    if S[i-1] == "E" and S[i] == "E":
        ans -= 1
    elif S[i-1] == "W" and S[i] == "W":
        ans += 1
    if mn_ans > ans:
        mn_ans = ans

print(mn_ans)

Submission status: Accepted

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

w_rotation = 0
e_rotation = S.count("E")
ans = w_rotation + e_rotation
for i in range(N):
    if S[i] == "W":
        w_rotation += 1
    else: # S[i] == "E":
        e_rotation -= 1
    if ans > w_rotation + e_rotation:
        ans = w_rotation + e_rotation

print(ans)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

def main():
    N = int(input())
    S = input().strip()
    
    E = [0]*N
    e = 0
    for i in range(0,N):
        E[i] = e
        if S[i] == "W":
            e += 1
    W = [0]*N
    w = 0
    for i in range(N-1,-1,-1):
        W[i] = w
        if S[i] == "E":
            w += 1

    X = [0]*N
    for i in range(N):
        X[i] = W[i] + E[i]
    print(min(X))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N = int(input())
    S = input().strip()
    
    E = [0]*N
    W = [0]*N
    for i in range(1,N):
        E[i] = E[i-1]
        if S[i-1] == "W":
            E[i] += 1
    for i in range(N-2,-1,-1):
        W[i] = W[i+1]
        if S[i+1] == "E":
            W[i] += 1

    X = [0]*N
    for i in range(N):
        X[i] = W[i] + E[i]
    print(min(X))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = 0
r = 0
L = [0]
R = [0]
for i in range(n):
  if s[i] == 'W':
    l += 1
  L += [l]
for i in range(n):
  if s[-1-i] == 'E':
    r += 1
  R += [r]
C = []
for i in range(n+1):
  C += [L[i]+R[-1-i]]
print(min(C))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
s = input().strip()
ls = []
prev = 'W'
index = 0
count = 0
while index <= len(s):
    if index == len(s):
        ls.append(count)
        break
    c = s[index]
    if c == prev:
        count += 1
    else:
        ls.append(count)
        count = 1
        prev = s[index]
    index += 1
accw = list(itertools.accumulate(ls[::2]))
acce = list(itertools.accumulate(ls[1::2][::-1]))[::-1]

res = float('inf')
for w, e in zip([0] + accw, acce + [0]):
    res = min(res, w + e)
print(res)

Submission status: Accepted

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

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

    if S[-i] == 'E':
        ecnt += 1
    wLst[-i - 1] = ecnt

ans = 10 ** 6
for i in range(N):
    tmp = eLst[i] + wLst[i]
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [0] + [s for s in input()]
W_nums = [0 for _ in range(N+2)]
E_nums = [0 for _ in range(N+2)]

for idx in range(1, N+1):
  if S[idx] == 'W': W_nums[idx]  = W_nums[idx-1] + 1
  else: W_nums[idx]  = W_nums[idx-1]

for idx in range(N, 0, -1):
  if S[idx] == 'E': E_nums[idx]  = E_nums[idx+1] + 1
  else: E_nums[idx]  = E_nums[idx+1]

print(min([W_nums[i-1] + E_nums[i+1] for i in range(1,N+1)]))

Submission status: Accepted

Solution code:


def main():

   N = int(input())
   seList = input()
   a_seList = []
   ans = len(seList)-1
   reW = seList.count('W')
   reE = seList.count('E')
   if seList[0] == 'W':
      reW -= 1
      a_seList.append(1+reE-reW)
   else:
      reE -= 1
      a_seList.append(-1+reE-reW)
   
   for k,i in enumerate(seList[1:]):
      if i=='W':
         reW -= 1
         a_seList.append(a_seList[k]+1)
      else :
         reE -= 1
         a_seList.append(a_seList[k]-1)
         
   
   leader = a_seList.index(min(a_seList))         
   ans=seList[:leader].count('W')+seList[leader+1:].count('E')
   print(ans)








if __name__== "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

e=s.count('E')
w=0

ans = float('inf')

for i in s:
    if i=='E':
        e-=1
        ans=min(ans,e+w)
    else:
        w+=1
        ans=min(ans,e+w-1)
print(ans)

Submission status: Accepted

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

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


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

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

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(len(S)):
    j = N-1-i
    if S[i] == "E" and i < N-1:
        E[i+1] = E[i] + 1
    elif i < N-1:
        E[i+1] = E[i]
    if S[j] == "W" and 0 < j:
        W[j-1] = W[j] + 1
    elif 0 < j:
        W[j-1] = W[j]
ans = float("inf")
for i in range(N):
    tmp = E[i] + W[i]
    if (N-tmp-1) < ans:
        ans = N-tmp-1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = 's' + input()
dp1 = [0] * (N+1)
dp2 = [0] * (N+1)
dp = [0] * (N+1)
for i in range(1, N):
    if S[i] == 'E':
        dp1[i+1] = dp1[i] + 1
    else:
        dp1[i+1] = dp1[i]
    if S[-i] == 'W':
        dp2[-(i+1)] = dp2[-i] + 1
    else:
        dp2[-(i+1)] = dp2[-i]
    dp[i+1] += dp1[i+1]
    dp[-(i+1)] += dp2[-(i+1)]
print(N-1-max(dp))

Submission status: Accepted

Solution code:
import sys
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    persons = list(inputs[0])

    count = [persons[1:].count("E")]

    for i in range(1, len(persons)):
        if i == 0:
            continue

        tmp = count[-1]
        if persons[i - 1] == 'W':
            tmp += 1
        if persons[i] == 'E':
            tmp -= 1
        count.append(tmp)

    return min(count)


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Accepted

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10**7)
#input = sys.stdin.readline
def solve():
  n = int(input())
  s = input()
  lefte = 0
  righte = s.count("E")
  leftw = 0
  rightw = n-righte
  ans = 10**10
  for i in range(n):
    if s[i] == "E":
      righte -= 1
      ans = min(ans,righte+leftw)
      lefte += 1
    else:
      rightw -= 1
      ans = min(ans,righte+leftw)
      leftw += 1
  print(ans)
      
      
solve()

Submission status: Accepted

Solution code:
n=int(input())
S=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-1,0,-1):
  if S[i]=='E':
    E[i-1]=E[i]+1
  else:
    E[i-1]=E[i]
print(min([W[i]+E[i] for i in range(n)]))

Submission status: Accepted

Solution code:
N=int(input())
text=input()
ls=[0 for i in range(N)]
if text[0]=="W":
  ls[0]=0
else:
  ls[0]=1
for i in range(1,N):
  if text[i]=="W":
    ls[i]=ls[i-1]
  else:
    ls[i]=ls[i-1]+1
ans=min([ls[N-1]-ls[0],N-1-ls[N-2]])
for i in range(1,N-1):
  x=0
  x+=i-ls[i-1]
  x+=ls[N-1]-ls[i]
  if x<ans:
    ans=x
print(ans)

Submission status: Accepted

Solution code:

n = int(input())
s = list(input())
w_count, e_count = 0, 0
w_list, e_list = [], []
count = []
for i in range(n):
    if s[i] == "W":
        w_count += 1
    w_list.append(w_count)
    if s[n - i - 1] == "E":
        e_count += 1
    e_list.append(e_count)
e_list.reverse()
count.append(e_list[0])
for i in range(1, n - 1):
    count.append(w_list[i - 1] + e_list[i + 1])
count.append(w_list[n - 1])
print(min(count))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC 098 C Attention

#いちいち全部数え直さない方がいい説
n = int(input())
s = input()
cnt = 0
cnt_list = []

for i in range(1,n):
    if s[i] == "E":
        cnt += 1
cnt_list.append(cnt)
for j in range(1,n):
    if s[j-1] == "W":
        cnt += 1
    if s[j] == "E":
        cnt -= 1
    cnt_list.append(cnt)

print(min(cnt_list))

Submission status: Accepted

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

e = [0]
w = [0]
for let in s:
    if let == "E":
        e.append(e[-1] + 1)
        w.append(w[-1])
    else:
        e.append(e[-1])
        w.append(w[-1] + 1)

ans = len(s)
for i in range(1, len(s) + 1):
    l = w[i - 1]
    r = e[n] - e[i]
    ans = min(ans, l + r)

print(ans)

Submission status: Accepted

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


"""考え方
「右側のE」+「左側のW」の合計が一番小さくなるインデックスを探索する。
"""
left_w = 0
right_e = S.count('E')
ans = 10**6
## 西の端から東に向かって確認していく
for n in range(0,N):
  if S[n] == "W":
    cnt = left_w + right_e
    left_w += 1
  else:
    cnt = left_w + right_e - 1
    right_e -= 1
    
  ans = min(ans,cnt)

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    left = [0] * (N+1)
    right = [0] * (N+1)
    tmp = 0
    for i in range(N):
        if S[i] == "W":
            tmp += 1
        left[i+1] = tmp
    tmp = 0
    for i in range(N-1, -1, -1):
        if S[i] == "E":
            tmp += 1
        right[i] = tmp
    ans = int(1e10)
    for i in range(N):
        ans = min(ans, left[i]+right[i+1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

print(min(rs))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = input()

result = n+1
change_sum_w = [0]*n
change_sum_e = [0]*n

change_sum_w[0] = 0
change_sum_e[0] = a[1:n].count("E")

for i in range(n):
    tmp_count = change_sum_e[i] + change_sum_w[i]
    if i < n-1:
        change_sum_w[i+1] = change_sum_w[i] + 1 if a[i] == "W" else change_sum_w[i]
        change_sum_e[i+1] = change_sum_e[i] - 1 if a[i+1] == "E" else change_sum_e[i]

    if result > tmp_count:
        result = tmp_count

print(result)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc098/tasks/arc098_a
# C - Attention
import collections

N = int(input().split()[0])
S = input().split()[0]
min_c = float('inf')
count_dict = {}
now_w = 0
w_count_list = []

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

now_e = 0
e_count_list = []
r_S = list(reversed(S))
for i in range(N):
    e_count_list.append(now_e)
    if r_S[i] == 'E':
        now_e += 1

e_count_list = list(reversed(e_count_list))

ans = min([w + e for w, e in zip(w_count_list, e_count_list)])
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

# リーダよりW (左)→ E(右)
# リーダよりE (右)→ W (左)
# W, Eの数を累積和として持っておく

N =  int(sys.stdin.readline())
S =  list(sys.stdin.readline().rstrip())

Wn = [0] * (N + 1)
En = [0] * (N + 1)

Wc = 0
Ec = 0
for i, s in enumerate(S):
    if s == "W":
        Wc += 1
    else:
        Ec += 1
    Wn[i + 1] = Wc
    En[i + 1] = Ec

ans = 10 ** 10
for i, (w, e) in enumerate(zip(Wn[1::], En[1::])):
    ans = min(Wn[i] + En[N] - En[i + 1], ans)

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

# リーダよりW (左)→ E(右)
# リーダよりE (右)→ W (左)
# W, Eの数を累積和として持っておく

N =  int(sys.stdin.readline())
S =  list(sys.stdin.readline().rstrip())

Wn = [0] * N
En = [0] * N

Wc = 0
Ec = 0
for i, s in enumerate(S):
    if s == "W":
        Wc += 1
    else:
        Ec += 1
    Wn[i] = Wc
    En[i] = Ec

ans = 10 ** 10
Wn = [0] + Wn
En = [0] + En
for i, (w, e) in enumerate(zip(Wn[1::], En[1::])):
    ans = min(Wn[i] + En[N] - En[i + 1], ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

w = 0
e = s.count('E')
ans = s.count('E')
for i in range(n):
  if i:
    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:
N = int(input())
S = input()

dp = [0 for _ in range(N)]
dp[0] = S[1:].count("E")

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

print(min(dp))

Submission status: Accepted

Solution code:
import sys
from collections import deque
from collections import  Counter
import statistics
import math

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
s = input()
cnt_w = s.count("W")
cnt_e = n - cnt_w
l = 0
r = cnt_e
ans = n
for i in range(n):
    if s[i] == "E":
        r -= 1
        ans = min(ans, l + r)
    else:
        ans = min(ans, l + r)
        l += 1
print(ans)

Submission status: Accepted

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

ec = [0] * (n + 1)
wc = [0] * (n + 1)
for i in range(n):
    ec[i + 1] = ec[i] + (s[i] == 'E')
    wc[i + 1] = wc[i] + (s[i] == 'W')

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

print(ans)

Submission status: Accepted

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

def cumsum_arr(s, w):
  arr=[]
  cnt=0
  for s_ in s:
    if s_==w: cnt+=1
    arr.append(cnt)
  return arr

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

Submission status: Accepted

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

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

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

ans=[]
for i in range(N):
  ans.append(w[i]+e[i]-1)
print(min(ans))

Submission status: Accepted

Solution code:
n,s=open(0);w=e=0
v=a=s.count("E")
for c in s:
    if c=="E":e+=1
    a=min(a,w+v-e)
    if c=="W":w+=1
print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N = int(input())
S = input()
ans = S[1:N].count('E')
tmp = ans
for i in range(N-1):
    if S[i] == 'W':
        tmp += 1
    if S[i + 1] == 'E':
        tmp -= 1
    ans = min(tmp, ans)
print(str(ans))

Submission status: Accepted

Solution code:
nums = int(input())
direct = list(input())
ary_w = [0 for i in range(nums)]
ary_e = [0 for i in range(nums)]
count_w = 0
count_e = 0
count = 0
result = 100000000

for i in range(nums):
    if direct[i] == "W":
        count_w += 1
    elif direct[i] == "E":
        count_e += 1

    ary_w[i] = count_w
    ary_e[i] = count_e

for i in range(nums):
    w = ary_w[i-1]
    e = ary_e[-1] - ary_e[i]
    if i == 0:
        w = 0
        e = ary_e[-1]
    count = w+e
    if count < result:
        result = count
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N = I()
S = str(input())

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

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

x = [0]*N
for i in range(N):
    x[i] = nw[i] + ne[i]

print(min(x))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = [0] * n
e = [0] * n
ans = 10**9

if s[0] == 'W':
    w[0] = 1
if s[-1] == 'E':
    e[-1] = 1

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


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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ss = [0] * n
rr = [0] * n
ans_i = 0
ans = 0
tmp = 0
max = -10**9
for i in range(n):
    if s[i] == 'E':
        tmp += 1
    else:
        tmp -= 1
    ss[i] = tmp

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

for i in range(n):
    if ss[i] + rr[i] > max:
        max = ss[i] + rr[i]
        ans_i = i

for i in range(0,ans_i):
    if s[i] == 'W':
        ans += 1
for i in range(ans_i+1,n):
    if s[i] == 'E':
        ans += 1
print(ans)