Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

5
WEEWW

Sample Output 1

1

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

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Accepted

Solution code:
def main(input):
    N = int(input())
    S = input().rstrip()
    ansE = [0] * N
    ansW = [0] * N

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

    if S[-1] == "W":
        ansW[-1] += 1
    for i in range(N-2, -1, -1):
        if S[i] == "W":
            ansW[i] = ansW[i+1] + 1
        else:
            ansW[i] = ansW[i+1]
    ans = N - max(x + y for x, y in zip(ansE, ansW))
    print(ans)


import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "local":
        with open("stdin.txt") as stdin:
            main(stdin.readline)
    else:
        main(sys.stdin.readline)

Submission status: Accepted

Solution code:
N = int(input())
S = 'E' + input() + 'W'
rcnt = []
lcnt = []

prercnt = 0
prelcnt = 0
for i in range(N+2): #0~N+1
  if S[i] == 'W': prelcnt += 1
  else: prercnt += 1
  rcnt.append(prercnt)
  lcnt.append(prelcnt)
    
#1~Nをリーダーにした時の向く方向が変わる人数の配列
print(min([lcnt[i-1] + rcnt[-1] - rcnt[i] for i in range(1, N+1)]))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
en = [1 if s[0]=="E" else 0] #S0からSiまでのうち東を向いている人の数
for i in range(1,n):
    en.append(1 if s[i]=="E" else 0)
    en[-1] += en[-2]
ans = en[-1]-en[0]
for i in range(1,n):
    ans = min(ans, (en[-1]-en[i]) + (i-en[i-1]))
print(ans)

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(n-1):
    W[i+1] += W[i]
    E[-i-2] += E[-i-1]

ans = float("inf")
for i in range(n):
    if i == 0:
        ans = min(ans, E[i+1])
    elif i == n-1:
        ans = min(ans, W[i-1])
    else:
        ans = min(ans, E[i+1]+W[i-1])

print(ans)
#print(E)
#print(W)

Submission status: Accepted

Solution code:
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

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

ws = []
cnt = 0
for c in S:
    if c == 'W':
        cnt += 1
    ws.append(cnt)

es = []
cnt = 0
for c in reversed(S):
    if c == 'E':
        cnt += 1
    es.append(cnt)
es.reverse()

ans = min(es[0], ws[-1])
for i in range(1, N - 1):
    ans = min(ans, ws[i - 1] + es[i + 1])
print(ans)

Submission status: Accepted

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

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

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

print(min(counts))

Submission status: Accepted

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


def solve(N: int, S: str):
    ws = 0
    es = 0
    for s in S:
        ws += int(s == 'W')
        es += int(s == 'E')

    ce = 0
    cw = 0
    ret = N
    for s in S:
        ce += int(s == 'E')
        ret = min(ret, cw + (es - ce), )
        cw += int(s == 'W')
    print(ret)




# Generated by 1.1.4 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():
    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
input = sys.stdin.readline

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

x = [0]*n
temp = 0
for i in range(n):
    if s[i] == 'W':
        temp += 1
    x[i] = temp

ans = n - 1 - (x[-1] - x[0])
ans = min(ans, x[-2])

for j in range(1, n-1):
    temp = x[j-1] + n - 1 - j - (x[-1] - x[j])
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

s = input()


a = [0] * (n+1)
b = [0] * (n+1)
ans = 3 * 10 ** 5

aN = s.count("E")
for i in range(n):
  if s[i] == "E":
    a[i+1] = a[i] + 1
    b[i+1] = b[i]
  else:
    a[i+1] = a[i]
    b[i+1] = b[i] + 1
  #print(ans, aN-a[i+1]+b[i])  
  ans = min(ans, aN-a[i+1]+b[i])
  
  
  
print((ans))

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	S = input()
	dpe = [0]*(N+1)
	dpw = [0]*(N+1)

	for i in range(N):
		if S[i] == "E" and i > 0:
			dpe[i-1] += 1
		elif S[i] == "W" and i < N-1:
			dpw[i+1] += 1

	for i in range(N-1, 0, -1):
		dpe[i-1] += dpe[i]

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

	ans = 3*(10**5)
	for i in range(N):
		ans = min(ans, dpe[i]+dpw[i])

	print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate
INF = float("inf")


def solve(N: int, S: str):
    ss = [1 if c == "E" else 0 for c in S]
    ss = list(accumulate(ss))

    mi, m = -1, INF
    for i in range(N):
        b = 0
        if i - 1 >= 0:
            b += i - ss[i-1]
        if i+1 <= N-1:
            b += ss[N-1]-ss[i]
        # print(i, b)
        if b < m:
            m = b
            mi = i
    print(m)
    # print(mi)

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

x=[0]*N

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

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

print(min(x))

Submission status: Accepted

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

W = [0]

for i in S:
    if i == 'W':
        W.append(W[-1]+1)
    else:
        W.append(W[-1])

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

print(answer)

Submission status: Accepted

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

ans = S[1:].count('E')
minans = ans
for i in range(1, N):
    if S[i-1] == 'E':
        ans -= 0
    else:
        ans += 1
    if S[i] == 'E':
        ans -= 1
    else:
        ans += 0
    minans = min(ans, minans)

print(minans)

Submission status: Accepted

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


def solve(N: int, S: str):
    eCount = [0]
    wCount = [0]
    for i in range(N):
        ce = S[i]
        cw = S[i]
        eCount.append(eCount[i] + (1 if ce == "W" else 0))
        wCount.append(wCount[i] + (1 if cw == "E" else 0))
    result = 10 ** 18
    for i in range(N):
        result = min(result, eCount[i] - wCount[i+1] + wCount[-1])
    print(result)
    return


# Generated by 1.1.4 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():
    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:
n = int(input())
s = input()


e = [0]
w = [0]

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

	e.append(cnt_e)
	w.append(cnt_w)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
west = [ 0 for i in range(n+1) ]
east = [ 0 for i in range(n+1) ]
for i in range(1,n+1):
    west[i] = west[i-1]
    east[i] = east[i-1]
    if s[i-1] == 'W':
        west[i] += 1
    if s[i-1] == 'E':
        east[i] += 1

ans = 1e9
for i in range(1,n+1):
    reqd_east_cnt = i-1
    got_east_cnt = east[i-1]
    reqd_west_cnt = n - i
    got_west_cnt = west[n] - west[i]
    # print(reqd_east_cnt,got_east_cnt,reqd_west_cnt,got_west_cnt)
    ans = min( ans, reqd_east_cnt - got_east_cnt + reqd_west_cnt - got_west_cnt)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
e=[]
w=[]
cnt_e=0
cnt_w=0
e.append(0)
w.append(0)
for x in s:
    if x=="E":
        cnt_e+=1
    else:
        cnt_w+=1
    e.append(cnt_e)
    w.append(cnt_w)

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
list1=[0]*(n+2)
list2=[0]*(n+2)
count1=0
count2=0
min=n
for i in range(1,n+1):
  if s[i-1]=='E':
    count1 += 1
  if s[i-1]=='W':
    count2 += 1
  list1[i] = count1
  list2[i] = count2
for i in range(1,n+1):
  sum=list2[i-1]+(list1[n]-list1[i])
  if sum < min:
    min=sum
print(min)

Submission status: Accepted

Solution code:
N=int(input())
s=input()
a=[0]*N
for i in range(1,N):
    if s[i]=="E": a[0]+=1

for j in range(1,N):
    tmp=a[j-1]
    if s[j-1]=="W": tmp+=1
    if s[j]=="E": tmp-=1
    a[j]=tmp

print(min(a))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

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

subS = [0]
for s in (S):
    subS.append(subS[-1]+int(s=="W"))
subS.append(subS[-1])

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

Submission status: Accepted

Solution code:
# 2019/04/10
# AtCoder Beginner Contest 098 - C

n = int(input())
s = input()
wl = [0]*(n+1)
el = [0]*(n+1)

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

minp = 10**6
for i in range(n+1):
    chp = wl[i] + (el[n]-el[i])
    if chp < minp:
        minp = chp

print(minp)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Lw = [0]*N
Le = [0]*N

cw = 0
ce = 0
for i in range(N):
  if S[i] == "W":
    cw += 1
  Lw[i] = cw
  if S[N - 1 - i] == "E":
    ce += 1
  Le[N-1-i] = ce

min = min(Le[1], Lw[N-2])
for i in range(1, N-1):
  p = Le[i + 1] + Lw[i - 1]
  if p < min:
    min = p

print(min)

Submission status: Accepted

Solution code:
N =int(input())
S = input()
i = 1
W = [0 for i in range(N)] #位置iを含む左側で西を向く人の数
E = [0 for i in range(N)] #位置iを含む左側で東を向く人の数
# 位置iを含まない右側で西を向く人の数はW[N-1]-W[i]
if S[0] == "W":
    W[0] = 1
else:
    E[0] = 1
while i < N:
    if S[i] == "W":
        W[i] = W[i-1]+1
        E[i] = E[i-1]
    else:
        E[i] = E[i-1]+1
        W[i] = W[i-1]
    i += 1
ans = 300001
for i in range(N):
    #位置iの人をリーダーにする
    if i != 0:
        temp = W[i-1]+E[N-1]-E[i]
    else:
        temp = E[N-1]-E[0]
    if temp < ans:
        ans = temp
print(str(ans))

Submission status: Accepted

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

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

cnt_w = 0
cnt_e = 0
for i in range(n):
  if s[i] == "W":
    cnt_w += 1
    w[i] = cnt_w
    e[i] = cnt_e
  else:
    w[i] = cnt_w
    cnt_e += 1
    e[i] = cnt_e
    
ch = n - 1
for i in range(n):
  if i >= 1:
    ch = min(ch, w[i - 1] + e[-1] - e[i])
  else:
    ch = min(ch, e[-1] - e[0])
print(ch)

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left

N = int(input())
S = input()
W = [i for i, s in enumerate(S) if s == 'W']
E = [i for i, s in enumerate(S) if s == 'E']
m = 10**8
for i in range(N):
    turn_E = bisect_left(W, i)
    turn_W = len(E) - bisect_right(E, i)
    m = min(m, turn_E + turn_W)
print(m)

Submission status: Accepted

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

n = int(input())
s = list(input())
w = [0] * n
e = [0] * n

cntw = 0
cnte = 0
for i in range(n):
    if s[i] == "W":
        cntw += 1
        w[i] = cntw
    else:
        w[i] = cntw
    if s[i] == "E":
        cnte += 1
        e[i] = cnte
    else:
        e[i] = cnte

ans = float("inf")

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
l=[]
sh=s[0]
s.remove(sh)
c=0
for i in s:
  if i=='E':
    c+=1
l.append(c)
for i in range(len(s)):
  tsu=s[i]
  if sh=='W':
    c+=1
  if tsu=='E':
    c-=1
  l.append(c)
  sh=tsu
print(min(l))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

# W = 0
# E = 1
N = int(input())
S = input()

X = []
count = 0
for s in S:
    if s == 'W':
        X.append(count)
    else:
        count += 1
        X.append(count)
     
min_person = int(1e10) 
for i, x in enumerate(X):
    num_person = 0
    if i > 0:
        num_person += i - X[i - 1]
    if i < N:
        num_person += X[-1] - X[i]
        
    min_person = min(min_person, num_person)
    
print(min_person)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
cnt=s[1:].count('E')
res=cnt
for i in range(len(s)-1):
    if s[i]=='W' and s[i+1]=='W':
        cnt+=1
    if s[i]=='E' and s[i+1]=='E':
        cnt-=1
    
    if res>cnt:
        res=cnt
print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
a = s.count("E")
b1 = 0
t = 10000000000000
for i in range(n):
    if s[i] == "E":
        a -= 1
        c = b1 + a
    else:
        c = b1 + a
        b1 += 1
    if t > c:
        t = c
print(t)

Submission status: Accepted

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

num_W = 0
num_E = S.count("E")
if S[0] == 'W':
    num_W = 1
else:
    num_E -= 1
minv = num_E
for i in range(1, N):
    if S[i] == 'W':
        num_W += 1
    else:
        num_E -= 1
    minv = min(minv, num_W+num_E)
print(minv)

Submission status: Accepted

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

es = [0] * N # 東を向かないといけない人の累積人数
ws = [0] * N # 西を向かないといけない人の累積人数

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

print(min(es[i] + ws[i] for i in range(N)))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


# globals
N = 0
S = ""


def set_inputs():
    global N, S
    N = get_int()
    S = get_str()
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    turn_e = [0] + list(itertools.accumulate([s == "W" for s in S[:-1]]))
    turn_w = list(reversed(list(itertools.accumulate(
        [s == "E" for s in reversed(S[1:])]
    )))) + [0]
    print(min(e + w for e, w in zip(turn_e, turn_w)))
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


# globals
N = 0
S = ""


def set_inputs():
    global N, S
    N = get_int()
    S = get_str()
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    turn_e = [0] + list(itertools.accumulate([s == "W" for s in S[:-1]]))
    turn_w = list(reversed(list(itertools.accumulate(
        [s == "E" for s in reversed(S[1:])]
    )))) + [0]
    print(min(itertools.starmap(operator.add, zip(turn_e, turn_w))))
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = S.count("E")
c = 0
A = N
for i in range(N):
    a += c
    c = 0
    if S[i] == "E":
        a -= 1
    else:
        c = 1
    A = min(A, a)
print(A)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
w = [0] * (n+1)
e = [0] * (n+1)
ans = n
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]

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

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
sumE = [0]
sumW = [0]
for i, s in enumerate(S):
    if S[len(S)-1-i] == 'E':
        e = 1
    else:
        e = 0
    if S[i] == 'W':
        w = 1
    else:
        w = 0
    
    sumE.append(sumE[i]+e)
    sumW.append(sumW[i]+w)
min = len(S)
for i in range(N):
    sum = sumE[len(S)-1-i] + sumW[i]
    if sum < min:
        min = sum
print(min)

Submission status: Accepted

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

e_cnt, w_cnt = 0, 0
e_arr, w_arr = [0]*(N+1), [0]*(N+1)
for i,s in enumerate(S):
    if s=='E':
        e_cnt += 1
    else:
        w_cnt += 1
    e_arr[i+1] = e_cnt
    w_arr[i+1] = w_cnt

max_ew = -1e6
for i in range(N):
    max_ew =  max(e_arr[i] - w_arr[i+1], max_ew)
print(N - 1 - (max_ew+w_cnt))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
L=[0 if S[i]=="E" else 1 for i in range(N)]
Lmk=0
Rmk=N-1-sum(L[1:])
ans=Rmk
for i in range(1,N):
  Lmk+=1 if L[i-1]==1 else 0
  Rmk-=1 if L[i]==0 else 0
  ans=min(ans,Lmk+Rmk)
print(ans)

Submission status: Accepted

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

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]
ans = float('inf')
for i in range(1, N + 1):
    ans = min(ans, w[i - 1] + e[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = [0]*(n+2)
b = [0]*(n+2)
for i in range(n):
    a[i+1] = a[i]
    b[-i-2] = b[-i-1]
    if s[i] == "W":
        a[i+1] += 1
    if s[-i-1] == "E":
        b[-i-2] += 1
b = b[2:n+2]
c = [a[i] + b[i] for i in range((n))]
print(min(c))

Submission status: Accepted

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

w_sum, e_sum = [0] * n, [0] * n

for i, item in enumerate(s):
    if i == 0:
        continue
    if s[i-1] == "W":
        w_sum[i] = w_sum[i-1] + 1
    else:
        w_sum[i] = w_sum[i-1]

s = s[::-1]

for i, item in enumerate(s):
    if i == 0:
        continue
    if s[i-1] == "E":
        e_sum[i] = e_sum[i-1] + 1
    else:
        e_sum[i] = e_sum[i-1]

e_sum = e_sum[::-1]
ans = float("inf")

for i, item in enumerate(s):
    ans = min(ans, e_sum[i] + w_sum[i])

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

W=[1 if s=='W' else 0 for s in S]
E=[1 if s=='E' else 0 for s in S]

print(
min((np.cumsum(W)[:-1] + np.cumsum(E[::-1])[::-1][1:]).min(),
    np.cumsum(W)[-1],
    np.cumsum(E[::-1])[::-1][0])
)

Submission status: Accepted

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

ecum = [0] * n

for i, item in enumerate(s):
  if i == 0:
    if item == "E":
      ecum[i] = 1
  else:
    if item == "E":
      ecum[i] = ecum[i-1] + 1
    else:
      ecum[i] = ecum[i-1]

ans = n
for i, item in enumerate(s):
  if i == 0:
    cost = ecum[-1]
  else:
    cost = i - ecum[i-1] + ecum[-1] - ecum[i]
  ans = min(ans, cost)
  
print(ans)

Submission status: Accepted

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

cost = S.count("E")
ans = cost

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ws = [0]*N
w_c = 0
for i in range(0, N):
    if S[i] == "W":
        w_c += 1
    ws[i] += w_c
es = [0]*N
e_c = 0
for i in reversed(range(0, N)):
    if S[i] == "E":
        e_c += 1
    es[i] += e_c
res = 3*10**5
for i in range(0, N):
    if i - 1 == 0:
        if es[0] < res:
            res = es[1]
        continue
    if i + 1 == N:
        if ws[i] < res:
            res = ws[i-1]
        continue
    t = ws[i-1] + es[i+1]
    if t < res:
        res = t
print(res)

Submission status: Accepted

Solution code:

N = int(input())
S = list(input())
 
numE = S.count('E')
numTurn = numE
numW = 0
 
for dir in S:
    if dir == 'E':
        numE -= 1
    else:
        numW += 1
    if numTurn > numE + numW:
        numTurn = numE + numW
 
print(numTurn)

Submission status: Accepted

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

numE = S.count('E')
numTurn = numE
numW = 0

for dir in S:
    if dir == 'E':
        numE -= 1
    else:
        numW += 1
    if numTurn > numE + numW:
        numTurn = numE + numW

print(numTurn)

Submission status: Accepted

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

numE = S.count('E')
numTurn = numE
numW = 0

for i in range(N):
    if S[i] == 'E':
        numE -= 1
    else:
        numW += 1
    if numTurn > numE + numW:
        numTurn = numE + numW

print(numTurn)

Submission status: Accepted

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

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

    if S[N-i-1] == "E":
        right_E[N-i-2] = right_E[N-i-1] + 1
    else:
        right_E[N-i-2] = right_E[N-i-1]


min_count = 100000000

for i in range(N):
    change_count = left_W[i] + right_E[i]
    if change_count < min_count:
        min_count = change_count
print(min_count)

Submission status: Accepted

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

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

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


min_count = 100000000
#print(left_W)
#print(right_E)

for i in range(N):
    change_count = left_W[i] + right_E[i]
    if change_count < min_count:
        min_count = change_count
print(min_count)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
string = input()

lst_w = []
lst_e = []

count = 0
for i in string:
    if i == 'W':
        lst_w.append(count)
        count += 1
    else:
        lst_w.append(count)
        
count = 0
for i in string[::-1]:
    if i == 'E':
        lst_e.append(count)
        count += 1
    else:
        lst_e.append(count)
lst_e = lst_e[::-1]

print(min(np.array(lst_w) + np.array(lst_e)))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
l=list(S)
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:
        E[i]=E[i-1]
        W[i]=W[i-1]+1
#print(E)
#print(W)
ans=N
ans=min(ans,E[N-1]-E[0])#i=0 is a leader

#i=1 to N-1 is a leader
for i in range(1,N):
    ans=min(ans,E[N-1]-E[i]+W[i-1])
    #ruisekiwa
print(ans)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-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 pf(s): return print(s, flush=True)

N = I()
S = input()

w_count = S.count('W')
e_count = S.count('E')

result = min(w_count, e_count)
left_w = 0
right_e = e_count
for s in S:
    # i番目がリーダーの場合
    result = min(result, left_w + right_e)
    if s == 'W':
        left_w += 1
    else:
        right_e -= 1

print(result)

Submission status: Accepted

Solution code:
N = int(input())
s = input()
goukei_W = []
goukei_E = []
goukei = []
sum = []

count = 0
for i in range(N):
    if s[i] == 'W':
        count += 1
    goukei_W.append(count)

count = 0
for i in range(N):
    if s[N-i-1] == 'E':
        count += 1
    goukei_E.append(count)

for i in range(N):
    if i-1 < 0:
        goukei.append(goukei_E[N-i-2])
    elif N-i-2 < 0:
        goukei.append(goukei_W[i-1])
    else:
        goukei.append(goukei_W[i-1] + goukei_E[N-i-2])

print(min(goukei))

Submission status: Accepted

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

cntW = 0
cntE = 0
sumW =[]
sumE = []
num_of_turn = []

for i in range(N):
    if S[i] == 'W':
        cntW += 1
    elif S[i] == 'E':
        cntE += 1
    sumW.append(cntW)
    sumE.append(cntE)

num_of_turn.append(sumE[N - 1] - sumE[0])

for i in range(1, N - 1):
    num_of_turn.append(sumW[i - 1] + sumE[N - 1] - sumE[i])

num_of_turn.append(sumW[N - 1])

print(min(num_of_turn))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = 300000
for L in range(1,N+1):
    num = E[N] - E[L] + W[L-1] -W[0]
    if ans > num:
        ans = num

print(ans)

Submission status: Accepted

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

W = 0
E = 0

for c in s:
    if c == 'W':
        W += 1
    if c == 'E':
        E += 1

arr = []

e_left = 0
w_left = 0
for c in s:
    if c == 'W':
        e_right = E - e_left
        arr.append(w_left + e_right)
        w_left += 1
    if c == 'E':
        e_right = E - e_left - 1
        arr.append(w_left + e_right)
        e_left += 1

print(min(arr))

Submission status: Accepted

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

    else:
        num=w[i-1]+e[n-1]-e[i]
    if min>num:
        min=num
print(min)

Submission status: Accepted

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

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

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

min_value = 1000000000000
for i in range(0, N):
    t = 0
    if i > 0:
        t += W[i - 1]
    if i < N - 1:
        t += E[i + 1]
    if min_value > t:
        min_value = t
print(min_value)

Submission status: Accepted

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

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

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

ans = N
for i in range(N):
  t = 0
  if i>0:
    t += L[i-1]
  if i<N-1:
    t += R[i+1]
  ans = min(ans, t)
print ans