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()
ans = 10**9
w = s.count('W')
e = s.count('E')
w_count = 0
e_count = 0
for i in range(n):
    if s[i] == 'W':
        w -= 1
        ans = min(ans,e+w_count)
        w_count += 1
    else:
        e -= 1
        ans = min(ans,e+w_count)
        e_count += 1

print(ans)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = input()
ans = 0
wl = []
el = []
for ss in s:
    if ss == 'W':
        wl.append(1)
        el.append(0)
    else:
        wl.append(0)
        el.append(1)
el.reverse()
wll = list(itertools.accumulate(wl))
ell = list(itertools.accumulate(el))
ell.reverse()

INF = float("inf")
ans = INF

for i in range(0, n):
    ans = min(ans, abs(wll[i-1]+ell[i-1]))

print(ans-1)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
#左から数える
W=[0]*N
w = 0
for i in range(N):
    if S[i] == "W":
        w += 1
    W[i] = w
#右から数える
E = [0]*N
e = 0
for i in range(N):
    if S[N-1-i] == "E":
        e += 1
    E[N-1-i] = e
ans = 10**9
for i in range(N):
    count = W[i] + E[i]
    ans = min(ans,count)

print(ans-1)

Submission status: Accepted

Solution code:


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

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

    
    ans = 10**6
    for a, b, in zip(l, r):
        ans = min(a+b, ans)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

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

l = [0]*n
r = [0]*n
for i in range(1,n):
    l[i] = l[i-1]+(s[i-1]=="W")
for i in range(n-1)[::-1]:
    r[i] = r[i+1]+(s[i+1]=="E")

ans = 10**6
for a,b in zip(l,r):
    ans = min(a+b,ans)
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    S = input()
    attention = sum(s != 'W' for s in S[1:])
    min_val = attention
    # print(min_val)
    for i in range(1,N):
        if S[i] == 'E': attention -= 1
        if S[i-1] != 'E':
            attention += 1
        # print(attention)
        min_val = min(min_val, attention)
    
    # print('---')
    print(min_val)
    
solve()

Submission status: Accepted

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

left = [0] * N
right = [0] * N
temp_left = 0
temp_right = 0

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

    right[-1-i] = temp_right
    if S[-1-i] == "E":
        temp_right += 1

ans = float("inf")
for l, r in zip(left, right):
    ans = min(ans, l + r)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(input())
left = []
right = []
left.append(0)
lc = 0
rc = 0
for i in range(n):
    if l[i] == "W":
        lc += 1
    if l[-1-i] == "E":
        rc += 1
    left.append(lc)
    right.append(rc)
right.append(0)
count = 300000

for i in range(n):
    a = left[i]+right[n-2-i]
    count = min(a,count)
print(count)

Submission status: Accepted

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

east_to_west = [0]*len(S)
west_to_east = [0]*len(S)

e = 0
w = 0
for i, s in enumerate(S):
  west_to_east[i] = e
  if s == 'W':
    e += 1

index = len(S) - 1
for i, s in enumerate(reversed(list(S))):
  east_to_west[index] = w
  index -= 1
  if s == 'E':
    w += 1

ans = 3*(10**5)
for n in range(N):
  sum = west_to_east[n] + east_to_west[n]
  if sum < ans:
    ans = sum
    
print(ans)

Submission status: Accepted

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

e_count=0
w_count=0
s_e=[]
s_w=[]

#print(s)

for i in range(n) :
    if s[i] == "W" :
        w_count += 1
        s_e.append(e_count)
        s_w.append(w_count)
    elif s[i] == "E" :
        e_count += 1
        s_e.append(e_count)
        s_w.append(w_count)

#print(s_w)
#print(s_e)
min = n+1

for i in range(n) :
    if i == 0 :
        sum = s_e[n-1] - s_e[i]
    elif i == n-1 :
        sum = s_w[i-1]
    else :
        sum = s_e[n-1] - s_e[i] + s_w[i-1]

    if min > sum :
        min = sum

print(min)

Submission status: Accepted

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

# 累積和を求めておく
cumsum_e = 0
cumsum_w = 0
for i in range(n):
    if s[i] == 'E':
        cumsum_e += 1
    else:
        cumsum_w += 1

min_change = n + 1
current_e = 0
current_w = 0
for i in range(n):
    temp_change = 0
    migi_e = cumsum_e - current_e
    if s[i] == 'E':
        migi_e -= 1
    temp_change = current_w + migi_e

    if temp_change < min_change:
        min_change = temp_change

    if s[i] == 'E':
        current_e += 1
    else:
        current_w += 1

print(min_change)

Submission status: Accepted

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

l = [0]*N
r = [0]*N

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

ans = 10**6
for a,b in zip(l,r):
	ans = min(a+b, ans)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
#i番目の人がリーダーとして
l=[0] #リーダーより左で向きを変える必要がある人数
for i in range(1,n):
  w=l[-1]
  if s[i-1]=='W':
    w+=1
  l.append(w)
r=[0] #リーダーより右で向きを変える必要がある人数
for i in reversed(range(n-1)):
  e=r[-1]
  if s[i+1]=='E':
    e+=1
  r.append(e)
r.reverse()
# print(l)
# print(r)
ans=n
for i in range(n):
  ans=min(r[i]+l[i],ans)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = input()
LW = np.array([0]*N)
RE = np.array([0]*N)
for i in range(1,N):
    LW[i] = LW[i-1] + int(S[i-1]=="W")
    RE[-(i+1)] = RE[-i] + int(S[-i]=="E")
P = LW + RE
print(min(P))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
olle = s.count("E")
cw,ce,p=0,0,10**6
for i in s:
    if i == "E":
        ce += 1
        p = min(p,cw+olle-ce)
    else:
        cw += 1
        p = min(p,cw-1+olle-ce)
print(max(p,0))

Submission status: Accepted

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

w = 0
e = 0

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

ans = 10**10
for i in range(1, n+1):
  ans = min(ans, w_list[i-1] + (e_list[-1] - e_list[i]))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
west_east_chars = input()
west_direction_num = [0] * n
east_direction_num = [0] * n

for i in range(1, n):
    if west_east_chars[i-1] == 'W':
        change = 1
    else:
        change = 0
    west_direction_num[i] += west_direction_num[i-1] + change

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

ans = 10 ** 10
for a, b in zip(west_direction_num, east_direction_num):
    ans = min(ans, a+b)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
A=[0]*N
B=[0]*N
E=0
W=0
for i in range(1,N):
  if S[i-1]=="W":
    W+=1
  if S[N-1-i+1]=="E":
    E+=1
  A[i]+=W
  A[N-1-i]+=E
  
  
print(min(A))

Submission status: Accepted

Solution code:
n = int(input())
s = [int(si == "E") for si in input()]
a = [0] * (n + 1)
for i in range(n):
  a[i + 1] = a[i] + s[i]
ans = n
for i in range(n):
  ans = min(a[-1] - a[i + 1] + i - a[i], ans)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N, S = open(0).read().split()
    pre = S.count("E")
    cur = pre
    for Si in S:
        if cur < pre:
            pre = cur
        if Si == "E":
            cur -= 1
        else:
            cur += 1
    print(pre if pre < cur else cur)
    return


main()

Submission status: Accepted

Solution code:
def main():
    N, S = open(0).read().split()
    pre = S.count("E")
    cur = pre
    for Si in S:
        if pre < cur:
            cur = pre
        if Si == "E":
            pre -= 1
        else:
            pre += 1
    print(pre if pre < cur else cur)
    return


main()

Submission status: Accepted

Solution code:
def main():
    N, S = open(0).read().split()
    pre = S.count("E")
    ans = pre
    for Si in S:
        if pre < ans:
            ans = pre
        if Si == "E":
            pre -= 1
        else:
            pre += 1
    print(pre if pre < ans else ans)
    return


main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w_count = s.count('W')
e_count = s.count('E')
w_count_cum = 0
e_count_cum = 0
ans = n

for i, s_i in enumerate(s):
  if s_i == 'W':
    w_count_cum += 1
    l_w = max(0, w_count_cum-1)
  elif s_i == 'E':
    e_count_cum += 1
    l_w = max(0, w_count_cum)
 
  r_e = max(0, e_count-e_count_cum)
  
  ans = min(ans, l_w+r_e)
    
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter



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

    c = Counter(S)

    ans = float("inf")

    w_c = 0
    for i in range(N):
        tmp = w_c
        if S[i] == 'W':
            w_c += 1
        else:
            c['E'] -= 1
        tmp += c['E']
        ans = min(ans, tmp)

    print(ans)





if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    w = [0]
    e = [sum([i=="E" for i in list(s)[1:]])]
    for i in range(1,n):
        appear = s[i-1]
        delete = s[i]
        ne = e[-1]
        nw = w[-1]
        if delete == "E":
            ne -= 1
        if appear == "W":
            nw += 1
        w.append(nw)
        e.append(ne)
    ans = [i+j for i,j in zip(w,e)]
    print(min(ans))  
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
num_W = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        if S[i] == 'W':
            num_W[i] = 1
    else:
        if S[i] == 'W':
            num_W[i] = num_W[i-1] + 1
        else:
            num_W[i] = num_W[i - 1]

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

ans = float('inf')
for i in range(N):
    if i == 0:
        ans = min(ans, num_E_rev[i + 1])
    elif i == N - 1:
        ans = min(ans, num_W[-1])
    else:
        ans = min(ans, num_W[i-1] + num_E_rev[i+1])
print(ans)

Submission status: Accepted

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

left_w_count = 0
right_e_count = lis[1:].count("E")
ans = right_e_count
for i in range(1,n):
    if lis[i-1] == "W":
        left_w_count += 1
    if lis[i] == "E":
        right_e_count -= 1
    ans = min(ans, left_w_count + right_e_count)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left_w_count = 0
right_e_count = S[1:].count("E")
result = right_e_count
for i in range(1, N):
    if S[i - 1] == "W":
        left_w_count += 1
    if S[i] == "E":
        right_e_count -= 1
    r = left_w_count + right_e_count
    if r < result:
        result = r
print(result)

Submission status: Accepted

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

sum_e = s.count("E")
sum_w = s.count("W")

cnt_e, cnt_w = 0, 0

ans = 10 ** 6
for x in s:
    if x == "E":
        cnt_e += 1
        ans = min(ans, cnt_w + (sum_e - cnt_e))
    if x == "W":
        ans = min(ans, cnt_w + (sum_e - cnt_e))
        cnt_w += 1

print(ans)

Submission status: Accepted

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

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

ans = 10 ** 6
for i in range(n):
    if s[i] == "W":
        r = cumsum[-1][1] - cumsum[i][1]
        l = cumsum[i][0] - 1
        ans = min(ans, r+l)
    elif s[i] == "E":
        r = cumsum[-1][1] - cumsum[i][1]
        l = cumsum[i][0]
        ans = min(ans, r+l)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = s.count("E")
w = s.count("W")
cnt1 = 0
cnt2 = 0
ans = 10e9
for i in s:
    if i=="E":
        cnt1 += 1
        ans = min(ans,cnt2+e-cnt1)
    else:
        ans = min(ans,cnt2+e-cnt1)
        cnt2 += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cnt=s[1:].count('E')
ans=cnt
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:
N=int(input())
li=list(input())

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

for index, i in enumerate(li):
  if i == 'W':
    w += 1
  W[index + 1] = w

newLi = reversed(li)

E = list(reversed(W))
e = 0

for index, i in enumerate(newLi):
  if i == 'E':
    e += 1
  E[index + 1] += e

print(min(E))

Submission status: Accepted

Solution code:
from itertools import accumulate
 
n = int(input())
s = [c == 'W' for c in input()]
l = accumulate([0] + s[:-1])
r = accumulate([0] + [not c for c in reversed(s[1:])])
print(min(e + w for e, w in zip(l, reversed(list(r)))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def Attention():
    N = int(input())
    S = input()
    ans = 300000

    Wcount = [0]
    Ecount = [0]
    cnt = 0
    for i in range(N):
        if S[i] == 'W':
            cnt += 1
        Wcount.append(cnt)
    cnt = 0
    for i in range(N):
        if S[(i + 1) * -1] == 'E':
            cnt += 1
        Ecount.append(cnt)

    for i in range(N):
        cnt = Wcount[i] + Ecount[(i + 2) * -1]
        ans = min(cnt, ans)
    print(ans)

Attention()

Submission status: Accepted

Solution code:
from itertools import accumulate

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

turn_E = [0] + list(accumulate([s=="W" for s in S[:-1]]))
turn_W = list(reversed(list(accumulate([s=="E" for s in S[:0:-1]])))) + [0]

ans = [e+w for e, w in zip(turn_E, turn_W)]
print(min(ans))

Submission status: Accepted

Solution code:
from itertools import accumulate
from operator import add
N = int(input())
S = input()

cnt_w = [0] * (N+1) # 左からWの数
for i in range(N): # cumsum
  if S[i] == "W":
    cnt_w[i+1] = cnt_w[i] + 1
  else:
    cnt_w[i+1] = cnt_w[i]
  
cnt_e = [0] * (N+1) # 右からEの数
for i in range(N-1, -1, -1):
  if S[i] == "E":
    cnt_e[i] = cnt_e[i+1] + 1
  else:
    cnt_e[i] = cnt_e[i+1]
    
ans = [w+e for w, e in zip(cnt_w, cnt_e)]
print(min(ans))

Submission status: Accepted

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

lsw = [0] * n
cntw = 0

lse = [0] * n
cnte = 0

for i in range(n):
    lsw[i] = cntw
    lse[n - 1 - i] = cnte

    if s[i] == "W":
        cntw += 1
    
    if s[n - 1 - i] == "E":
        cnte += 1

min = 3 * (10 ** 5)

for i in range(n):
    wa = lsw[i] + lse[i]
    if wa < min:
        min = wa

print(min)

Submission status: Accepted

Solution code:
from itertools import accumulate
from operator import add
N = int(input())
S = input()
E = list(accumulate("E"==s for s in "W"+S[:0:-1]))[::-1]
W = list(accumulate("W"==s for s in "E"+S[:-1]))
print(min(map(add, E, W)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
num = [0 for _ in range(N)]

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

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = list(input())
c = Counter(S[1:])
ans = 0+c["E"]
now = S[0]
e = c["E"]
w = 0
for i in range(1,N):
    if now == "W":
        w += 1
    now = S[i]
    if now == "E":
        e -= 1
    if w+e < ans:
        ans = w+e
print(ans)

Submission status: Accepted

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

lft = [0]
rgt = [0]

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

cnt = 0
for i in range(1,n):
    if s[(i)*(-1)] == "E":
        cnt += 1
    rgt.append(cnt)

ans = 10**9
for i in range(n):
    ans = min(ans,lft[i]+rgt[(i+1)*(-1)])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
x = 0
a = 0
idx = 0

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

    if x < a:
        a = x
        idx = i

w = S[0:idx].count("W")
e = S[idx + 1 : N].count("E")
print(w + e)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
s = input()
aa = {'E':1, 'W':0}
e = list(map(aa.get, s))
aa = {'E':0, 'W':1}
w = list(map(aa.get, s))

ae = list(accumulate(e[::-1]))[::-1]
aw = list(accumulate(w))

ans = float('INF')
for i in range(1, n-1):
    if ans > ae[i+1] + aw[i-1]:
        ans = ae[i+1] + aw[i-1]
print(min([ans, ae[1], aw[-2]]))
# print(ae, aw)

Submission status: Accepted

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

all_W_E = [0, 0]
for s in S:
    if s == "W":
        all_W_E[0] += 1
    else:
        all_W_E[1] += 1

now_W = 0
now_E = 0
ans_list = [0] * N

for i in range(N):
    if S[i] == "W":
        ans_list[i] = (now_W + all_W_E[1] - now_E)
        now_W += 1
    else:
        ans_list[i] = (now_W + all_W_E[1] - now_E - 1)
        now_E += 1

##print(ans_list)
print(min(ans_list))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
#------------------------------
#########タイムオーバー########
#------------------------------
# minimum = 300000
# for i in range(N):
    # leftWestNum = S[:i].count('W')
    # rightEstNum = S[i+1:N].count('E')
    # tmpMin = leftWestNum + rightEstNum
    # if tmpMin < minimum:
        # minimum = tmpMin
# print(minimum)

minimum = 3000000
numEast = [0] * (N+1)
numWest = [0] * (N+1)
for i in range(N):
    if S[i] == 'E':
        numEast[i+1] = numEast[i] + 1
        numWest[i+1] = numWest[i]
    else:
        numEast[i+1] = numEast[i]
        numWest[i+1] = numWest[i] + 1

for i in range(N):
    tmpMin = numWest[i] + (numEast[N]-numEast[i+1])
    if tmpMin < minimum:
        minimum = tmpMin

print(minimum)

Submission status: Accepted

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

# W,E各々の累積和を求めておく
cumulative_e = [0] * (n+1)
cumulative_w = [0] * (n+1)
for i in range(1, n+1):
  if s[i-1] == 'W':
    cumulative_w[i] = cumulative_w[i-1] + 1
    cumulative_e[i] = cumulative_e[i-1]
  elif s[i-1] == 'E':
    cumulative_e[i] = cumulative_e[i-1] + 1
    cumulative_w[i] = cumulative_w[i-1]

# 誰がリーダーに適任か調べる
result = [0] * n
for j in range(n):
  result[j] = cumulative_w[j] + ( cumulative_e[n] - cumulative_e[min(n,j+1)])

print(min(result))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = [0]*n
cnt_e = [0]*n
cnt_w = [0]*n
e = 0
w = 0
for i in range(n):
    if s[i] == "E":
        e += 1
    else :
        w += 1
    cnt_e[i] = e
    cnt_w[i] = w

cnt[0] =  e - cnt_e[0]
for i in range(1,n):
    cnt[i] = cnt_w[i-1] + e - cnt_e[i]

print(min(cnt))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
cnt = (S[1:]).count("E")
ans = cnt
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:

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

W = [0]
E = [0]

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


ans = float("inf")
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 = input()

w_count = [0] * (n+1)
e_count = [0] * (n+1)

e = 0
w = 0
for i, s in enumerate(S):
    if s == 'E':
        e += 1
    elif s == 'W':
        w += 1

    w_count[i] = w
    e_count[i] = e

ans = 10**6
for i in range(n):
    w_on_my_left = w_count[i-1]
    e_on_my_right = e - e_count[i]
    candidate = w_on_my_left + e_on_my_right
    ans = min(ans, candidate)

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
lst1 = list(input())
c = collections.Counter(lst1) #dict
right = c["E"]
left = 0
ans = 10**9

for i in range(n):
    if lst1[i] == "E":
        right -=1
    if i >= 1 and lst1[i-1] == "W":
        left += 1
    ans = min(ans,left+right)
    

print(ans)

Submission status: Accepted

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


def solve(N: int, S: str):
    # リーダーの方を向く人数＝リーダーより左のWの数＋リーダーより右のEの数
    accum_W = [0]*N #iより左にWが何個あるか
    accum_E = [0]*N #iよりみぎにEが何個あるか

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

    for i in range(N-2,-1,-1):
        accum_E[i] = accum_E[i+1] + int(S[i+1] == "E")
    
    answer = N
    for i in range(N):
        a = accum_W[i]+accum_E[i]
        answer = min(answer,a)
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()

def main():
    l = 0
    r = s.count('E')
    res = r
    for i in range(n):
        cur = s[i]
        if cur == 'E':
            r -= 1
        res = min(res, l + r)
        if cur == 'W':
            l += 1
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

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

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

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

w.append(w[-1])
e.insert(0, e[0])

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

print(ans)

Submission status: Accepted

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

cntw = 0
cnte = 0
lstw = [0] * N
lste = [0] * N

for i in range(N):
    lstw[i] = cntw
    lste[N-i-1] = cnte
    if(S[i] == 'W'):
        cntw += 1
    if(S[N-i-1] == 'E'):
        cnte += 1 
    
ans = N
for i in range(N):
    ans = min(ans,lstw[i]+lste[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = n
lstw = [0]*n
lste = [0]*n
cntw = 0
cnte = 0

for i in range(n):
    lstw[i] = cntw
    if s[i] == "W": cntw += 1
    lste[n-1-i] = cnte
    if s[n-1-i] == "E": cnte += 1

for i in range(n):
    ans = min(lstw[i]+lste[i],ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
origin = S[1:]
summary = [origin.count("E")]
for i in range(N-1):
    s = summary[-1]
    if S[i] == "W":
        if S[i+1] == "E":
            delta = 0
        else:
            delta = 1
    else:
        if S[i+1] == "E":
            delta = -1
        else:
            delta = 0
    summary.append(delta+s)
print(min(summary))

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    S = list(input())
    rw = [0] * N
    re = [0] * N
    
    cntw = 0
    cnte = 0
    for i in range(N-1,-1,-1):
        if S[i] == 'W':
            cntw += 1
        else:
            cnte += 1
        
        rw[i] = cntw
        re[i] = cnte

    ans = N
    lw = 0
    le = 0
    for i in range(N):
        if i == N-1:
                ans = min(ans, lw)

        else:
            if S[i] == 'W':
                ans = min(ans, lw + re[i+1])
                lw += 1
            else:
                ans = min(ans, lw + re[i+1])
                le += 1
        
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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


def solve(N: int, S: str):
    Right_w, Right_e = S.count("W"), S.count("E")
    Left_w, Left_e = 0, 0

    ans = N

    for si in S:
        # update stack
        if si == "W":
            Right_w -= 1
        else:
            Right_e -= 1
        # update ans
        ans = min(ans, Left_w + Right_e)
        # update stack
        if si == "W":
            Left_w += 1
        else:
            Left_e += 1
    print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N = int(input())
    S = str(input())
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
west = [0 for _ in range(n+1)]
east = [0 for _ in range(n+1)]

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

print(ans)

Submission status: Accepted

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

cnt_w = [0] * (N+1) # 左からWの数
cnt_e = [0] * (N+1) # 右からEの数
for i in range(N):
    if S[i] == "W":
        cnt_w[i+1] = cnt_w[i] + 1 
    else:
        cnt_w[i+1] = cnt_w[i]
for i in range(N-1,-1,-1):
    if S[i] == "E":
        cnt_e[i] = cnt_e[i+1] + 1 
    else:
        cnt_e[i] = cnt_e[i+1]

# print("W",cnt_w)
# print("E",cnt_e)
ans = [w+e for w,e in zip(cnt_w,cnt_e)]
print(min(ans))

Submission status: Accepted

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

#西に向いている人の累計と、東に向いている人の累計を計算
wsuml = [0]*n
esuml = [0]*n
for i in range(n):
  esuml[i] = esuml[i-1]
  wsuml[i] = wsuml[i-1]
  if s[i] == 'E':
    esuml[i] += 1
  else:
    wsuml[i] += 1
#コスト計算用のリストを準備
costl = [0]*n
#両端は考えるの面倒なので手動で。
costl[0] = esuml[-1] - esuml[0]
costl[-1] = wsuml[-2]
#あとはO(n)のループをまわす。自分より左でWの人と、自分より右でEの人の和がコスト
for i in range(1,n-1):
  costl[i] = wsuml[i-1] + esuml[-1]-esuml[i]
print(min(costl))

Submission status: Accepted

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

count = [0] * N
e_cnt = 0

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

w_cnt = 0

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

ans = min(count) - 1

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    s = S()
    west = [0] * (N+1)
    east = [0] * (N+1)

    for i, c in enumerate(s):
        if c == 'W':
            west[i] = 1
        else:
            east[i] = 1

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

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

    min_c = inf
    ans_i = -1
    for i in range(N):
        if i == 0:
            tot = east[i+1]
        elif i == N-1:
            tot = west[i-1]
        else:
            tot = west[i-1] + east[i+1]

        min_c = min(min_c, tot)
    print(min_c)
main()

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
LE= [0] * (N+1)
RW= [0] * (N+1)
for i in range(1,N+1):
  if S[i-1] == 'E':
    LE[i] = LE[i-1] + 1
  else:
    LE[i] = LE[i-1]
for i in range(N-1,-1,-1):
  if S[i] == 'W':
    RW[i] = RW[i+1] + 1
  else:
    RW[i] = RW[i+1]

#print(LE)
#print(RW)
ANS = N + 1
for i in range(0,N):
  W = abs((N-1) - i - RW[i+1])
  if i == 0:
    E = 0
  elif i == N - 1:
    W = 0
  else:
  	E = abs(i - LE[i])
  #print("i:{}, W:{}, E:{}, RW[i+1]={}, LE[i]={}".format(i, W, E, RW[i+1], LE[i]))
  ANS = min(ANS, W + E)
print(ANS)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = [1 if i == "E" else 0 for i in input()]

dp = [0] * n

# dp[0] = n - 1 - sum(s[1:])
dp[0] = sum(s[1:])

# print(s, dp)

for i in range(1, n):
    if s[i - 1] == 0:
        dp[i] = dp[i - 1] + 1 - s[i]
    else:
        dp[i] = dp[i - 1] - s[i]

print(min(dp))