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()

total = 0
 
total_list = []

# 事前に計算しておく
count_w_from_left = [0]
pre_count = 0
for i in range(1, N):
  if S[i-1:i] == 'W':
    pre_count += 1
  count_w_from_left.append(pre_count)

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

# 算出処理
for i in range(N):
    to_E = count_w_from_left[i]
    to_W = count_e_from_right[i]
    total_list.append(to_E + to_W)
 
print(min(total_list))

Submission status: Accepted

Solution code:
import bisect
n = int(input())
s = input()
elist = []
wlist = []
for i in range(n):
	if s[i] == "E":
		elist.append(i)
	elif s[i] == "W":
		wlist.append(i)
ans = 10**18
lenelist = len(elist)
"""
西にいるWを数える
東にいるEを数える
"""
for i in range(n):
	tmp = lenelist-bisect.bisect_right(elist, i)+bisect.bisect_left(wlist, i)
	ans = min(tmp, ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
table_e = []
count_e = 0
table_w = []
count_w = 0
for s in S:
    if s == 'E':
        count_e += 1
    else:
        count_w += 1
    table_e.append(count_e)
    table_w.append(count_w)
ans = float('inf')
for i in range(N):
    tmp = table_w[i - 1] if i else 0
    tmp += count_e - table_e[i]
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

from collections import defaultdict
l = defaultdict(int)
r = defaultdict(int)
for si in s:
    r[si] += 1
r[s[0]] -= 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


if __name__ == "__main__":
    n = int(input())
    s = input()
    sum_w = s.count("W")
    sum_e = s.count("E")
    ans = 10**9
    w,e = 0,0
    for i in range(n):
        if s[i] == "W":
            sum_w-=1
        else:
            sum_e-=1
        chenge = w + sum_e
        if s[i] == "W":
            w += 1
        else:
            e += 1
        ans = min(ans,chenge)
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
countw = 0
counte = 0
ans = [0]*n
for i in range(1, n):
  if s[i-1] == "W":
    countw += 1
  if i != n:
    ans[i] += countw
  if s[-i] == "E":
    counte += 1
  ans[n-i-1] += counte

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
S = input()
re = 0
for s in S:
    if s == 'E':
        re += 1
lw = 0
c = 3*10**5
for i in range(n):
    if S[i]  == 'E':
        re -= 1
    c = min(c, re+lw)

    if S[i] == 'W':
        lw += 1
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
# when 0-th person is the leader
for i in range(1, n):
    if s[i] == 'E':
        ans += 1

tmp = ans
# when i-th person is the leader
for i in range(1, n):
    if s[i-1] == 'W':
        tmp += 1
    if s[i] == 'E':
        tmp -= 1
    ans = min(ans, tmp)


print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
s = s.replace("W","1")
s = s.replace("E","0")
fl = sum(list(map(int,s)))
k = 0
p = 0
for i in range(n):
    if i == 0:
        p = fl - int(s[0])
        k = p
    elif i == n-1:
        p = n-1-fl+int(s[n-1])
        if p > k:
            k = p
    else:
        if (s[i-1] == "1" and s[i] == "1"):
            p -= 1
        elif (s[i-1] == "1" and s[i] == "0"):
            continue
        elif (s[i-1] == "0" and s[i] == "1"):
            continue
        else:
            p += 1
            if p > k:
                k = p
print(n-k-1)

Submission status: Accepted

Solution code:
import sys

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

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

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
S = input()
re = 0
for s in S:
    if s == 'E':
        re += 1
lw = 0
c = len(S)
for i in range(n):
    if S[i]  == 'E':
        re -= 1
    c = min(c, re+lw)

    if S[i] == 'W':
        lw += 1
print(c)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
S = input()
 
tmp_W = 0
tmp_E = 0
 
cnt = [0]*len(S)
for i in range(len(S)):
	if S[i] == "W":
		tmp_W += 1
	cnt[i] += tmp_W
 
for i in range(len(S)-1, -1, -1):
	if S[i] == "E":
		tmp_E += 1
	cnt[i] += tmp_E
 
print(min(cnt)-1)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
S = input()

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

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

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

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
S = input()

tmp_W = 0
tmp_E = 0

cnt = [0]*len(S)
for i in range(len(S)):
	if S[i] == "W":
		tmp_W += 1
	cnt[i] += tmp_W

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

print(min(cnt)-1)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
S = input()

tmp_W = 0
tmp_E = 0
W = []
E = []
ans = INF

cnt = [0]*len(S)
for i in range(len(S)):
	if S[i] == "W":
		tmp_W += 1
	cnt[i] += tmp_W

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

print(min(cnt)-1)

Submission status: Accepted

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

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

W_num = [0] * N
E_num = [0] * N

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

print(min([W_num[i] + E_num[i] for i in range(N)]))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e_count = [0]*(n+1)
w_count = [0]*(n+1)
for i in range(n):
    if s[i] == "E":
        e_count[i+1] = e_count[i] + 1
        w_count[i+1] = w_count[i]
    else:
        w_count[i+1] = w_count[i] + 1
        e_count[i+1] = e_count[i]
#print(w_count)
#print(e_count)
ans = n+1
for i in range(1, n+1):
    res = 0
    res += w_count[i-1]
    res += e_count[n] - e_count[i]
    ans = min(ans, res)
print(ans)

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N = int(input())
S = input().rstrip()

# l[i] : i番目からみて, 左側にいる W の人数
# r[i] : i番目からみて, 右側にいる E の人数
l = [0]*N
r = [0]*N

cnt = 0
for i in range(N-1):
    if S[i]=="W":
        cnt += 1
    l[i+1] = cnt
cnt = 0
for i in range(N-1):
    if S[N-1-i]=="E":
        cnt += 1
    r[N-1-i-1] = cnt

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
left = 0
right = s[1:].count('E')
cnt = [left + right]

for i in range(1, n):
    if s[i-1] == 'W':
        left += 1
    if s[i] == 'E':
        right -= 1
    cnt.append(left + right)

print(min(cnt))

Submission status: Accepted

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

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

cnt = [left+right]

for i in range(1,len(S)):
    if S[i-1] == 'W':
        left += 1
    
    if S[i] == 'E':
        right -= 1
        
    cnt.append(left+right)

print(min(cnt))

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = list(input())
cnt = 0

ans = [0]*N

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

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

Submission status: Accepted

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

ans = [0] * n
left = 0
right = s[1:len(s)].count('E')
ans[0] = right

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

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
e = [0]*(N+1)
w = [0]*(N+1)
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 = input()
right_w = 0
right_E = 0
left_E = 0
left_W = 0
ans = len(s)
for i in s:
    if i == "W":
        right_w += 1
    else:
        right_E += 1
for i in range(len(s)):
    tmp = s[i]
    if tmp == 'W':
        right_w -= 1
        tmp_ans = left_W+ right_E
        if tmp_ans < ans:
            ans = tmp_ans
        left_W += 1
    else:
        right_E -= 1
        tmp_ans = left_W+ right_E
        if tmp_ans < ans:
            ans = tmp_ans
        left_E += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
a=S.count("E")
m=a
for s in S:
    if s=="E":
        a-=1
    m=min(m,a)
    if s=="W":
        a+=1
print(m)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
W = [0] * (n+1)
w = 0
ans = float("inf")

for i in range(n):
    if S[i] == "W":
        w += 1
    
    W[i+1] = w
    
for i in range(n):
    res = W[i] + ((n-W[n]) - (i+1-W[i+1]))
    ans = min(ans,res)
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = input()
WE = [[] for _ in range(n)]
w = 0
e = 0

for i in range(n):
    if S[i] == "W":
        w += 1
    else:
        e += 1
    
    WE[i].append(w)
    WE[i].append(e)
    
ans = WE[n-1][1] - WE[0][1]

for i in range(1,n):
    res = WE[i-1][0] + (WE[n-1][1] - WE[i][1])
    ans = min(ans,res)
    
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] = 1
    
    if S[i] == "E":
        e[i+1] = 1

for i in range(N):
    w[i+1] += w[i]
    e[i+1] += e[i]

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

print(ans)

Submission status: Accepted

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

accum_w = [0] * (n+1)  # accum_w[i] = (s[:i].count('W'))
accum_e = [0] * (n+1)  # accum_e[i] = (s[i:].count('E'))

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

cnt_e = 0
for j in range(n-1, -1, -1):
  if s[j] == 'E':
    cnt_e += 1
  accum_e[j] = cnt_e

# print(accum_w)
# print(accum_e)

ans = 2**64-1
for i in range(n):
  ans = min(ans, accum_w[i]+accum_e[i+1])

print(ans)

Submission status: Accepted

Solution code:
def resolve():
	n = int(input())
	s = input()
	west_sum = [0]*(n+1)
	east_sum = [0]*(n+1)

	for i in range(n):
		if s[i] == 'E':
			east_sum[i+1] = east_sum[i] + 1
			west_sum[i+1] = west_sum[i]
		else:
			west_sum[i+1] = west_sum[i] + 1
			east_sum[i+1] = east_sum[i]
	ans = float('inf')
	for i in range(1, n):
		if i == 1:
			ans = min(ans, east_sum[-1] - east_sum[1])
		elif i == (n - 1):
			ans = min(ans, west_sum[-2])
		else:
			v = west_sum[i-1] + (east_sum[-1] - east_sum[i])
			ans = min(ans, v)
	print(ans)
resolve()

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7
N = getN()
S = input()
# up 左にいるWの人数
up = [0 for i in range(N)]
# down 右にいるEの人数
down = [0 for i in range(N)]

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

for i in range(2, N + 1):
    if S[-i + 1] == "E":
        down[-i] = down[-i + 1] + 1
    else:
        down[-i] = down[-i + 1]
ans = float('inf')
for i in range(N):
    opt = up[i] + down[i]
    ans = min(ans, opt)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
cnt_E = [S.count("E")]
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
    flag = False
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    SUM.append(cnt_W[i]+cnt_E[i])
print(min(SUM)-1)

Submission status: Accepted

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

#n = 12
#s = 'WEWEWEEEWWWE'

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

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

#  w  ([0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 6],
#  e   [0, 0, 1, 1, 2, 2, 3, 4, 5, 5, 5, 5, 6])

ans = n
for i in range(1,n+1):
  # 自分より左にいるwの数 w[i-1]
  # 自分より右にいるeの数 e[n] - e[i]
  ans = min(ans, w[i-1] + (e[n] - e[i]) )

print(ans)

Submission status: Accepted

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

i = S[1:].count('E')
r = [i]

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

    if S[n - 1] == 'W':
        i += 1
    r.append(i)

print(min(r))

Submission status: Accepted

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

i = S[1:].count('E')
ans = [i]

for n in range(N):
    if n == 0:
        continue

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

    if S[n - 1] == 'W':
        i += 1
    ans.append(i)

print(min(ans))

Submission status: Accepted

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

Ecnt = S.count('E')
Wcnt = S.count('W')

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

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

for i in range(1, 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]
        
E = [0] + E
W = [0] + W
# print('E', E, 'W', W)

score=[]
for i in range(N):
        score.append(W[N]-W[i+1] + E[i]-E[0])
print(N-max(score)-1)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1

#############
# Main Code #
#############

N = I()
S = S()

X = []
Y = []

count = 0
for i in range(N):
  if S[i] == "W":
    count += 1
  X.append(count)
  
count = 0
for i in range(N):
  if S[N-1-i] == "E":
    count += 1
  Y.append(count)

P(min(X[i]+Y[N-1-i] for i in range(N))-1)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
#ある場所を決めたときに、右側はすべてW,左側はすべてEになる必要がある
#この変更の数が少なければよい

#最初から最後までのwとeの累積和を計算する
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
	else:
		sum_W[i+1]=sum_W[i]
		
	if S[i]=="E":
		sum_E[i+1]+=sum_E[i]+1
	else:
		sum_E[i+1]=sum_E[i]
ans=N
count=0
for i in range(1,N+1):
	#仮に3番目がリーダーだとすると,左右で動かなければならない人数は
	tmp=sum_W[i-1]+(sum_E[-1]-sum_E[i])
	if tmp<ans:
		ans=tmp
		count=i
print(ans)
#仮に3番目がリーダーだとすると,左右で動かなければならない人数は
#print(sum_W[2],sum_E[5]-sum_E[3])

Submission status: Accepted

Solution code:
def resolve():
    # 整数 1 つ
    n = int(input())
    s = input()
    # 整数複数個
    # a, b = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

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

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

    print(n - cntmax -1)


resolve()

Submission status: Accepted

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

tot = S[1:].count('E')
tot_list = [tot]
former_leader = S[0]

for l in range(1, N):
    leader = S[l]
    if (former_leader, leader)  == ('E', 'E'):
        tot -= 1
    if (former_leader, leader)  == ('W', 'W'):
        tot += 1
        
    former_leader = leader  
    tot_list.append(tot)
    
print(min(tot_list))

Submission status: Accepted

Solution code:
N = int(input())
S = [d for d in input()]

tot = S[1:].count('E')
tot_list = [tot]
former_leader = S[0]

for l in range(1, N):
    leader = S[l]
    if (former_leader, leader)  == ('E', 'E'):
        tot -= 1
    if (former_leader, leader)  == ('W', 'W'):
        tot += 1
        
    former_leader = leader  
    tot_list.append(tot)
    
print(min(tot_list))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left = [0]
right = [S.count('E')]
for i in range(N):
    if S[i] == 'W':
        left.append(left[i]+1)
        right.append(right[i])
    else:
        left.append(left[i])
        right.append(right[i]-1)
ans = 1000000
for i in range(N):
    ans = min(ans, left[i]+right[i+1])
print(ans)

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()
w_cnt = [0] * (n + 2)
e_cnt = [0] * (n + 2)
for i in range(1, n + 1):
    if s[i - 1] == 'W':
        w_cnt[i] = 1 + w_cnt[i - 1]
    else:
        w_cnt[i] = w_cnt[i - 1]

for i in range(1, n + 1):
    if s[-i] == 'E':
        e_cnt[n + 1 - i] = 1 + e_cnt[n + 2 - i]
    else:
        e_cnt[n + 1 - i] = e_cnt[n + 2 - i]
ans = n
for i in range(1, n + 1):
    tmp = w_cnt[i - 1] + e_cnt[i + 1]
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

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

answer_sum = [0] * n
e_sum = [0] * (n+1)
w_sum = [0] * (n+1)

for i in range(n):
    w_sum[i+1] = (s[i] == 'W') + w_sum[i]
    e_sum[i+1] = (s[i] == 'E') + e_sum[i]

for i in range(n):
    answer_sum[i] = w_sum[i] + (e_sum[n] - e_sum[i+1])

print(min(answer_sum))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
L = 0
R = S.count('E')
ans = L + R
for i in range(0, N):
    if S[i] == 'E':
        R -= 1
        R = max(0, R)
        ans = min(ans, L+R)
    else:
        L += 1
print(ans)

Submission status: Accepted

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

w=s.count('W')
e=s.count('E')
r_w=w
r_e=e
l_w=0
l_e=0
ans=n

for i in range(n):
    if s[i]=='W':
        r_w-=1
        tmp=l_w+r_e
        l_w+=1
    else:
        r_e-=1
        tmp=l_w+r_e
        l_e+=1
    if tmp<ans:
        ans=tmp
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    west_counts = [0 for _ in range(N)]
    east_counts = [0 for _ in range(N)]
    for i, c in enumerate(S):
        if c == 'W':
            west_counts[i] = 1
        else:
            east_counts[i] = 1
    for i in range(1, N):
        west_counts[i] += west_counts[i - 1]  # cumsum from leftside
        east_counts[N - 1 - i] += east_counts[N - i]  # cumsum from rightside
    ans = N
    for i in range(N):
        d = 0
        if i < N - 1:
            d += east_counts[i + 1]  # rightside
        if i > 0:
            d += west_counts[i - 1]  # leftside
        ans = min([ans, d])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

    ans = 10 ** 6
    sum_count = S.count('E')
    for s in S:
        aft = 0
        if s == 'W':
            aft = 1
        else:
            sum_count -= 1
        ans = min(ans, sum_count)
        sum_count += aft
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = 1001001
wn = 0
en = sum([1 for i in s[1:] if i == 'E'])
for i in range(n):
    ans = min(ans, wn + en)
    if s[i] == 'W':
        wn += 1
    if i != n - 1:
        if s[i + 1] == 'E':
            en -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	if n == 2:
		print(1 if len(Counter(list(s)).keys()) == 2 else 0)
		return
	e_w = sum_e[-1] - sum_e[1]
	e_e = sum_w[n-1]
	ans = min(e_w, e_e)
	for i in range(n):
		v = sum_w[i-1] + (sum_e[-1] - sum_e[i])
		ans = min(ans, v)
	print(ans)
resolve()

Submission status: Accepted

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

N = int(input())
S = input().rstrip()
W = [0] * N
E = [0] * N

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

min = float("inf")
for i in range(N):
    if W[i] + E[i] < min:
        min = W[i] + E[i]
print(min)