Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

5
WEEWW

Sample Output 1

1

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

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Accepted

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

w_accumulate = [0]
e_accumulate = [0]

for i in range(1, N):
    w_accumulate.append(w_accumulate[-1] + (S[i - 1] == "W"))
    e_accumulate.append(e_accumulate[-1] + (S[-i] == "E"))

e_accumulate.reverse()

print(min([c1 + c2 for c1, c2 in zip(w_accumulate, e_accumulate)]))

Submission status: Accepted

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

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

count_min = E[-1]
for i in range(1, n):
    count = W[i-1] + (E[-1] - E[i]) 
    count_min = min(count_min, count)

print(count_min)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
def main(N, S):
    num_e = S.count("E")
    num_w = 0
    res = num_e
    for s in S:
        if(s=="E"):
            num_e -= 1
        else:
            num_w += 1
        res = min(res, num_e + num_w)
    print(res)
    
if(__name__ == "__main__"):
    N = int(input())
    S = input()
    
    main(N, S)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/3/16
Solved on 2019/3/
@author: shinjisu
"""


# ABC 098 C Attention
#import math
#import numpy as np

def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]
# def getIntList(): return np.array(input().split(), dtype=np.longlong)

def zeros(n): return [0]*n
# def zeros(n): return np.zeros(n, dtype=np.longlong)

def getIntLines(n): return [int(input()) for i in range(n)]
"""
def getIntLines(n):
    data = zeros(n)
    for i in range(n):
        data[i] = getInt()
    return data
"""

def zeros2(n, m): return [zeros(m)]*n # obsoleted zeros((n, m))で代替

def getIntMat(n, m):  # n行に渡って、1行にm個の整数
    #mat = zeros((n, m))
    mat = zeros2(n, m)
    for i in range(n):
        mat[i] = getIntList()
    return mat

ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**18

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                print(cmt, ':  ', end='')
            print(x)
        return x


def prob():
    d = Debug()
    d.off()
    N = getInt()
    d.dmp((N))
    A = input()
    d.dmp(A)
    west, east = 0, 0
    for i in range(N):
        if A[i] == 'W':
            west += 1
        else:
            east += 1
    minTurn = east
    turn = east
    for i in range(N):
        if A[i] == 'W':
            pass
        else:
            turn -= 1
        d.dmp(turn,'turn')
        minTurn = min(minTurn, turn)
        # 向きを元に戻して、立場を変える
        if A[i] == 'W':
            turn += 1
        else:
            turn += 1-1
        d.dmp(minTurn,'minTurn')
    return minTurn


ans = prob()
if ans is None:
    pass
elif type(ans) == tuple and ans[0] == 1:  # 1,ans
    for elm in ans[1]:
        print(elm)
else:
    print(ans)

Submission status: Accepted

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

temp = s[1:].count('E')
ans = temp
for i in range(1, n):
    if s[i] == 'E':
        temp -= 1
    if s[i - 1] == 'W':
        temp += 1
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E_cost = S.count("E")
W_cost = 0
ans = N
for s in S:
    if s == "E":
        E_cost -= 1
    ans = min(ans, E_cost + W_cost)
    if s == "W":
        W_cost += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
answer = n
dpa = [-1]*(n+1)
dpa[0] = 0
dpb = [-1]*(n+1)
dpb[0] = s.count('E')

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

for i in range(n+1):
	answer = min(answer, dpa[i]+dpb[i])
	
print(answer)

Submission status: Accepted

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

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

res = 10**9
for i in range(n):
    res = min(res,le[i][1]+ri[i][0])
print(res)

Submission status: Accepted

Solution code:
def solve(string):
    n, s = string.split()
    num_e = s.count("E")
    count_e = count_w = 0
    ans = int(n)
    for _s in s:
        tmp = count_w
        if _s == "E":
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

Solution code:
def solve(string):
    n, s = string.split()
    num_e = s.count("E")
    count_e = 0
    count_w = 0
    ans = int(n)
    for _s in s:
        tmp = count_w
        if _s == "E":
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

Solution code:
def solve(string):
    n, s = string.split()
    n = int(n)
    es = {i for i, _s in enumerate(s) if _s == "E"}
    num_e = len(es)
    count_e = 0
    count_w = 0

    ans = n
    for i, _s in enumerate(s):
        tmp = count_w
        if i in es:
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

Solution code:
def solve(string):
    n, s = string.split()
    n = int(n)
    es = {i for i, _s in enumerate(s) if _s == "E"}
    num_e = len(es)
    num_w = n - num_e
    count_e = 0
    count_w = 0

    ans = n
    for i, _s in enumerate(s):
        tmp = [count_e, count_w]
        if i in es:
            count_e += 1
        else:
            count_w += 1
        tmp += [num_e - count_e, num_w - count_w]
        ans = min(ans, tmp[1] + tmp[2])
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

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

original_w = []
prev = 0
for i in range(N):
    if S[i] == 'W':
        prev += 1
    original_w.append(prev)

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

score = []
score.append(s[1:].count("E"))

for i in range(1, n):
    diff = -1 if s[i] == "E" else 0
    diff += 1 if s[i-1] == "W" else 0
    score.append(score[-1]+diff)

print(min(score))

Submission status: Accepted

Solution code:
# /user/bin/python
# -*- coding: utf-8 -*-


N = int(input())
S = str(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(ans, num)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

input = stdin.readline

MAX_N = 300001

N = int(input())

S = input()


def main():
    # 東を向いている人の累積和
    E = [0] * N

    if S[0] == 'E':
        E[0] += 1

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

    ans = MAX_N

    for i in range(N):
        # 東を向いている人 西に向ける
        num = E[-1] - E[i]

        if (0 < i):
            # 西を向いている人 東に向ける
            num += i - E[i - 1]

        ans = min(ans, num)

    print(ans)
    return


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def main():
	N = int(input())
	tmp = input()
	lines = list(tmp)
	
	nisi = [0 for _ in range(N)]
	higasi = [0 for _ in range(N)]
	for i, val in enumerate(lines):
		if val == "W":
			if i != 0:
				nisi[i] = nisi[i-1] + 1
			else:
				nisi[i] = 1
		else:
			if i != 0:
				nisi[i] = nisi[i-1]
			else:
				nisi[i] = 0
		
	for i in range(N-1, -1, -1):
		if lines[i] == "E":
			if i < N-1:
				higasi[i] = higasi[i+1] + 1
			else:
				higasi[i] = 1
		else:
			if i < N-1:
				higasi[i] = higasi[i+1]
			else:
				higasi[i] = 0
	
	ans_min = -1
	for i, val in enumerate(lines):
		ans = 0
		ans1 = 0
		ans2 = 0
		if i < (N - 1):
			ans1 = higasi[i+1]
		if i > 0:
			ans2 = nisi[i-1]
		ans = ans1 + ans2
			
		if -1 == ans_min:
			ans_min = ans
		if ans_min > ans:
			ans_min = ans
	
	print(ans_min)
		
if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = S[1:].count('E')  # n=0のとき
a = ans
for i in range(1,N):
    if S[i] == 'E':
        a -= 1
    if S[i-1] == 'W':
        a += 1
    #print(a)
    ans = min(ans,a)
print(ans)

#  W E E W W
#  * 1 1 0 0  2
#  1 * 1 0 0  2
#  1 0 * 0 0  1
#  1 0 0 * 0  1
#  1 0 0 1 *  2

Submission status: Accepted

Solution code:
def attention(N: int, S: str) -> int:
    # 西から順に i 人までのうち西を向いている人の数
    Se = [0] * (N + 1)
    for i, c in enumerate(S):
        Se[i + 1] = Se[i] + (1 if c == 'W' else 0)

    min_people = float('inf')
    for i, c in enumerate(S):
        w2e = Se[i]  # 西から東に向きを変える人数
        e2w = (N - (i + 1)) - (Se[N] - Se[i + 1])
        min_people = min(min_people, w2e + e2w)

    return min_people


if __name__ == "__main__":
    N = int(input())
    S = input()
    ans = attention(N, S)
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
L=[[0,0]]*(N+2)
numW=0
numE=0
for i in range(0,N):
  if S[i]=='W':
    numW+=1
  else:
    numE+=1
  L[i+1]=[numW,numE]

L[N+1]=L[N]
  
INF=10**6
ans=INF

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

Submission status: Accepted

Solution code:
import numpy as np
def main():
    N = int(input())
    S = list(input())
    S_W = [1 if S[i] == "W" else 0 for i in range(N)]
    S_E = [1 if S[N-i-1] == "E" else 0 for i in range(N)]
    S_W_rui = np.cumsum(S_W)
    S_E_rui = np.cumsum(S_E)
    
    #print(S_W_rui,S_E_rui)
    cnt = N
    cnt_new = N
    
    for i in range(N):
        if i == 0:
            cnt_new = S_E_rui[N-i-2]
        elif i == N-1:
            cnt_new = S_W_rui[i-1]
        else:
            cnt_new = S_W_rui[i-1] + S_E_rui[N-i-2]
        if cnt_new < cnt:
            cnt = cnt_new
            #print(cnt)
    print(cnt)
    
    
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
turn = S.count('E')
min_turn = 300000
for i in range(N):
    if S[i] == 'E':
        turn -= 1
    if (S[i-1] == 'W') and (1 <= i):
        turn += 1
    if min_turn > turn:
        min_turn = turn
print(min_turn)

Submission status: Accepted

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

N=n-1-(a[-1]-a[0])
M=min(n,N)
for k in range(1,n):
  N=a[k-1]+n-(k+1)-(a[-1]-a[k])
  M=min(M,N)

print(M)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
min_turn = 300000
wnum = 0
for i in range(N):
    if i == 0:
        enum = S[1:].count('E')
    if (1 <= i) and (S[i] == 'E'):
        enum -= 1
    if (1 <= i) and (S[i-1] == 'W'):
        wnum += 1
    turn = enum + wnum
    if min_turn > turn:
        min_turn = turn
print(min_turn)

Submission status: Accepted

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

Submission status: Accepted

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

result = 10 ** 6
E = S.count('E')
W = S.count('W')
for i in range(N):
    temp = 0
    if 'E' == S[i]:
        temp = E-1
    else:
        temp = E
    if 'W' == S[i]:
        E += 1
    else:
        E -= 1
    result = min(temp, result)
print(result)

Submission status: Accepted

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

e = S.count("E")
ans = 1e9
for s in S:
  if(s == "E"):
    e -= 1
  else:
    ans = min(ans, e)
    e += 1
ans = min(ans, e)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

d=np.zeros(N)
d[0]=S[1:].count("E")

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
e = s.count("E")
w = s.count("W")
res = 10**7
for i in range(n):
    if s[i] == "E":
        tmp = e - 1
    else:
        tmp = e
    if s[i] == "W":
        e += 1
    else:
        e -= 1
    res = min(res, tmp)
print(res)

Submission status: Accepted

Solution code:
int_n = int(input())
str_s = input()

li_west = [0] * int_n
for i in range(int_n - 1):
    if str_s[i] == 'W':
        li_west[i + 1] = li_west[i] + 1
    else:
        li_west[i + 1] = li_west[i]
li_east = [0] * int_n

for i in range(int_n - 1, 0, -1):
    if str_s[i] == 'E':
        li_east[i - 1] = li_east[i] + 1
    else:
        li_east[i - 1] = li_east[i]

minimum = 999999
for i in range(int_n):
    int_w = li_west[i] + li_east[i]
    minimum = min(minimum, int_w)

print(minimum)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s = [s[i:i+1] for i in range(n)]
lcost = [0 for _ in range(n)]
rcost = [0 for _ in range(n)]
if s[0] == 'W':
  lcost[0] = 1
if s[-1] == 'E':
  rcost[-1] = 1
for i in range(1, n):
  if s[i] == 'W':
    lcost[i] = lcost[i - 1] + 1
  else:
    lcost[i] = lcost[i - 1]
for i in range(2, n + 1):
  if s[-i] == 'E':
    rcost[-i] = rcost[-i + 1] + 1
  else:
    rcost[-i] = rcost[-i + 1]
mincost = min(lcost[-1], rcost[0])
#print(lcost)
#print(rcost)
for i in range(1, n - 1):
  hoge = lcost[i - 1] + rcost[i + 1]
  if hoge < mincost:
    mincost = hoge
print(mincost)

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
num=int(input())
n=input()
m=len(n)
cnt=[0 for i in range(m)]
n1=n[1:]
cnt[0]=n1.count('E')

for i in range(m-1):
    point=0
    if n[i]=='W':
        point+=1
    if n[i+1]=='E':
        point-=1
    cnt[i+1]=cnt[i]+point
    
print(min(cnt))

Submission status: Accepted

Solution code:
n = int(input())
pp = list(input())
cnt = 0
lis = [0]*n
w = [0]*n
e = [0]*n
if pp[0] =="W":
    w[0] =1
else:
    e[0] =1
for i in range(1,n):
    if pp[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]
for i in range(1,n):
    lis[i] = w[i-1]+e[n-1]-e[i]
lis[0] = e[n-1]-e[0]

print(min(lis))

Submission status: Accepted

Solution code:
n = int(input())
slist = input().strip()
e, w = 0, 0
for s in slist:
  if s == 'E':
    e += 1
  else:
    w += 1

le, lw, re, rw = 0, 0, e, w
minr = n + 1
for s in slist:
  if s == 'E':
    re -= 1
    minr = minr if minr <= re + lw else re + lw
    le += 1
  else:
    rw -= 1
    minr = minr if minr <= re + lw else re + lw
    lw += 1

print(minr)

Submission status: Accepted

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

Submission status: Accepted

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

listSeeWSum=[]
index=0
wSum=0
while index < n:
    if index==0:
        listSeeWSum.append(0)
    else:
        if listS[index-1] == "W":
            wSum+=1
        listSeeWSum.append(wSum)
    index+=1

listSeeESum=[]
listS.reverse()
index = 0
eSum = 0
while index < n:
    if index == 0:
        listSeeESum.append(0)
    else:
        if listS[index-1] == "E":
            eSum += 1
        listSeeESum.append(eSum)
    index += 1
listSeeESum.reverse()

listSum=[]
index=0
while index < n:
    listSum.append(listSeeESum[index]+listSeeWSum[index])
    index+=1

print(min(listSum))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=len([i for i in range(1,n) if s[i]=='E'])
l=0
i=1
x=r+l
while i<=n-1:
    if s[i]=='E' and s[i-1]=='E':
        r=r-1
        l=l
    elif s[i]=='W' and s[i-1]=='W':
        r=r
        l=l+1
    elif s[i]=='W' and s[i-1]=='E':
        r=r
        l=l
    elif s[i]=='E' and s[i-1]=='W':
        r=r-1
        l=l+1
    x=min(l+r,x)
    i+=1
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def main():
  input = sys.stdin.readline
  N = int(input())
  S = input().strip()
  dp = [0 for _ in range(N)]
  nE = S.count('E')
  nW = 0
  for i in range(N):
    if S[i] == 'E':
      dp[i] = nE - 1 + nW
      nE -= 1
    else:
      dp[i] = nE + nW
      nW += 1
  print(min(dp))


if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

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

l=[S.count('E')]
n=l[0]

for x in S:
    if x == 'W':
        n += 1
    else:
        n -= 1
    l.append(n)

print(min(l))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
if S[0] == "E":
    e = 0
    w = S.count("E") - 1
    before = "E"
else:
    e = 0
    w = S.count("E")
    before = "W"
now = w
for s in S[1:]:
    if s == "E":
        w -= 1
    if before == "W":
        e += 1
    before = s
    now = min(e + w, now)
print(now)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
W_count = 0
W_cumulative = []
for i in range(N):
    if S[i] == "W":
        W_count += 1
    W_cumulative.append(W_count)

ans = N
W_cumulative.insert(0,0)
for i in range(1,N+1):
    count = W_cumulative[i-1]
    count += N - W_cumulative[-1] - i + W_cumulative[i]
    ans = min(ans,count)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
list = []
E = s.count("E")
L = 0
check = 0 # 直前がWだったかの記憶装置

for i in s:
    if i == "E":
        E -= 1
        if check == 0:
            list.append(E+L) # E+Lが答えの候補
        elif check == 1:
            check = 0
            L += 1
            list.append(E+L)
    elif i == "W":
        if check == 0: # 一つ前がEのとき
            check = 1
            list.append(E+L)
        elif check == 1: # 一つ前がWのとき
            L += 1
            list.append(E+L)

print(min(list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
a = []
a.append(s[1:].count("E"))
for i in range(n-1):
    if s[i+1] == "E" and s[i] == "E":
        a.append(a[i]-1)
    elif s[i+1] == "W" and s[i] == "E":
        a.append(a[i])
    elif s[i+1] == "E" and s[i] == "W":
        a.append(a[i])
    elif s[i+1] == "W" and s[i] == "W":
        a.append(a[i]+1)
print(min(a))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cum = [0] * (n+1)
cum[0] = 0
for i in range(n):
    cum[i+1] = cum[i] + int(s[i] == 'W')
cost = [0] * n
for i in range(n):
    cost[i] = cum[i] + (n-i-1) - (cum[n] - cum[i+1])
print(min(cost))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
answer = S.count('E')
if S[0] =='E':
    test = S.count('E') - 1
else:
    test = S.count('E')

for x in range(0,N-1):
    if S[x] == 'W':
        test += 1
    if S[x+1] =='E':
        test -= 1
    answer = min (answer,test)

print(answer)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
e = 0
for i in s:
    if i == 'E':
        e += 1
        
w = 0
a = []
for i in range(n):
    if i == 0:
        if s[i] == 'E':
            e -= 1
    else:
        if s[i] == 'E':
            e -= 1
        if s[i-1] == 'W':
            w += 1
    a.append(w+e)

print(min(a))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
#入力

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
num_s=[]
value=[]
left=0
for f_s in s:
    if(f_s=="E"):
        num_s.append(0)
    else:
        num_s.append(1)
right=sum(num_s[1:])
len_s=n-1
value.append(len_s-right)
for p in range(1,n):
    left+=num_s[p-1]
    right-=num_s[p]
    len_s-=1
    value.append(left+len_s-right)
print(min(value))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = np.array(list(input()))
n = []
for i in range(N):
    if S[i] == 'E':
        S[i] = 1
    else:
        S[i] = 0
S = np.array(S, dtype=int)
S = np.cumsum(S)
last = S[N-1]
n.append(last-S[0])
for i in range(1, N):
    ls = i - S[i-1]
    rs = last - S[i]
    n.append(ls+rs)
print(min(n))

Submission status: Accepted

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

a = [(0,0) for _ in range(n + 1)]
b = []
for i in range(n):
    if s[i] == "W":
        a[i+1] = (a[i][0] + 1,a[i][1])
    else:
        a[i+1] = (a[i][0],a[i][1] + 1)
    b.append(a[i+1][1]-a[i+1][0])
    
ans = s[0:b.index(max(b))].count('W') + s[b.index(max(b))+1:].count('E')
print(ans)

Submission status: Accepted

Solution code:
def solve(s):
    n = len(s)
    w = [0]*n
    e = [0]*n

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

    tot = [x+y for x, y in zip(w, e)]
    return min(tot)


input()
print(solve(input()))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N = int(lines.pop(0))
    S = lines.pop(0)
    RS = S[::-1]
    S = 'x' + S
    RS = 'x' + RS
    slist = []
    rslist = []
    ss = 0
    rss = 0
    for i in range(N):
        if S[i] == 'W':
            ss += 1
        if RS[i] == 'E':
            rss += 1
        slist.append(ss)
        #rslist.insert(0,rss)
        rslist.append(rss)
    #print(S)
    #print(RS)
    #print(list( zip(slist, rslist)))
    rslist.reverse()
    print(min([s + r for s, r in zip(slist, rslist)]))

#
# main
#
# 2. use stdin

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Accepted

Solution code:
N=int(input())
stringth =input()
back = None
ans = len(stringth)
REcount = stringth.count("E")
LWcount = 0
for now in stringth:
    if back == "W":
        LWcount += 1
    if now == "E":
        REcount -=1
    if ans > LWcount+REcount:
        ans = LWcount+REcount
    back = now
print(ans)

Submission status: Accepted

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

num = s[1:].count('E')
ans = num

for k in range(1, n):
    now = s[k-1]
    next = s[k]
    num += 1 if now == 'W' else 0
    num -= 1 if next == 'E' else 0
    ans = min(ans, num)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
rev_counter = []
rev_num = s.count("E")
if s[0] == "E":
    rev_num -= 1
rev_counter.append(rev_num)
for i in range(1, n):
    if s[i] == "E" and s[i-1] == "E":
        rev_num -= 1
    elif s[i] == "W" and s[i-1] == "W":
         rev_num += 1
    rev_counter.append(rev_num)
print(min(rev_counter))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
data=[]
data.append(s[1:].count('E'))
for i in range(1,n):
    d=data[-1]
    if s[i-1]=='W':d+=1
    if s[i]=='E':d-=1
    data.append(d)
    
print(min(data))

Submission status: Accepted

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

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

cnt=ans

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

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
s = np.array([True if i=='E' else False for i in input()])
t = np.array([False if i else True for i in s])
t = np.cumsum(t)
t = np.hstack(([0], t[1:]))
s = np.cumsum(s[::-1])[::-1]
s = np.hstack((s[1:], [0]))
print(np.min(s+t))