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().strip())
S=input().strip()
inf=10**8
ecr=0
for i in range(N):
    if S[i]=='E':
        ecr+=1
ans=inf
wcl=0
for i in range(len(S)):
    if i==0:
        if S[i]=='E':
            ecr-=1
        spre=S[i]
        ans=min(ans,ecr+wcl)
    else:
        if S[i]=='E':
            ecr-=1
        if spre=='W':
            wcl+=1
        spre=S[i]
        ans=min(ans,ecr+wcl)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
count_E = S.count("E")
count_W = 0
ans = count_E
for i in range(N):
    if S[i] == "E":
        count_E -= 1
    if S[i-1] == "W" and i != 0:
        count_W += 1
    if count_E + count_W < ans :
        ans = count_E + count_W
print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter

input = sys.stdin.readline
INF = float('inf')


def inpl(): return list(map(int, input().split()))


N = int(input())
S = input().strip()

num_E = Counter(S)['E']  # リーダより右側にいるEの数
num_W = 0  # リーダより左側にいるWの数

ans = N - 1
for i in range(N):
    if S[i] == 'E':
        num_E -= 1
        ans = min(ans, num_W + num_E)
    else:
        ans = min(ans, num_W + num_E)
        num_W += 1

print(ans)

Submission status: Accepted

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

W = [0] * (N+2)
E = [0] * (N + 2)

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

ret = 10**10
for leader in range(1, N+1):
    leftW = W[leader-1]
    rightE = E[N]-E[leader]
    # print("{}, {}".format(leftW, rightE))
    sum = leftW + rightE
    ret = min(ret, sum)

print(ret)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = list(input())
    w = 0
    e = 0
    ans = 1<<32
    for ss in s:
        if ss=="W":
            w+=1
        else:
            e+=1
    ww = 0
    for ss in s:
        if ss=="W":
            w -= 1
            ans = min(ans,e+ww)
            ww += 1
        else:
            e -= 1
            ans = min(ans,e+ww)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

W = [0 for _ in range(N + 1)]
E = [0 for _ in range(N + 1)]

for i, s in enumerate(S):
    if s == 'W':
        W[i + 1] = W[i] + 1
        E[i + 1] = E[i]
    else:
        W[i + 1] = W[i]
        E[i + 1] = E[i] + 1

ret = 10 ** 6
for i in range(1, N+1):
    w = W[i - 1]
    e = E[N] - E[i]
    ret = min(ret, w + e)

print(ret)

Submission status: Accepted

Solution code:
N = int(input())
S = '0' + input()
west = [0 for i in range(N+1)]

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

change = N

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

print(change)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(str(input()))
list_check = []
num_east = 0
for i in range(0,n):
    if s[i] == "E":
        num_east += 1
    list_check.append(num_east)

calc_min = 10 ** 9
for i in range(0,n):
    if s[i] == "E":
        calc = ( (i+1) - list_check[i] ) + (list_check[-1] - list_check[i])
    else:
        calc = ( (i) - list_check[i] ) + (list_check[-1] - list_check[i])
    if calc_min > calc:
        calc_min = calc

print(calc_min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = input()
ans = float('inf')
w, e = 0, s.count('E')
for c in s:
    e -= (c == 'E')
    ans = min(ans, w + e)
    w += (c == 'W')
print(ans)

Submission status: Accepted

Solution code:
debug = False

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

    tmpCnt=S.count('E')
    if S[0]=='E':
        tmpCnt-=1

    minCnt=tmpCnt

    for i in range(1,N):
        if S[i]=='E':
            tmpCnt-=1
        if S[i-1]=='W':
            tmpCnt+=1
        minCnt=min(minCnt,tmpCnt)
        if debug:
            print(i,S[i],tmpCnt,minCnt)
        
    print(minCnt)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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

from itertools import accumulate as acc
w = [0] + list(acc([int(i == 'W') for i in s]))
e = [0] + list(acc([int(i == 'E') for i in s[::-1]]))

ans = float('inf')
for i in range(n):
    ans = min(ans, w[i] + e[-i-2])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
L = [S[1:].count('E'),S[:N].count('W')]
k = 0
w = 0
e = S[1:].count('E')
while (k <= N-2):
  if (S[k] == 'W' and S[k+1] == 'W'):
    w += 1
  elif (S[k] == 'W' and S[k+1] == 'E'):
    w += 1
    e -= 1
  elif (S[k] == 'E' and S[k+1] == 'W'):
    w += 0
  else:
    e -= 1
  L.append(e+w)
  k += 1
  
print(min(L))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
num = float("inf")
RW = [0] * n
LW = [0] * n
RE = [0] * n
LE = [0] * n
for i in range(1, n):
    w = 0
    if s[i - 1] == "W":
        w += 1

    RW[i] = RW[i - 1] + w

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

    LE[i] = LE[i + 1] + e

num = float("inf")
for i in range(n):
    num = min(num, RW[i] + LE[i])

print(num)

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 = 998244353
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,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 pf(s): return print(s, flush=True)

n = I()
s = S()
r = 0
for i in range(1,n):
    if s[i] == 'E':r+=1
res = r
temp = r
for i in range(1,n):
    if s[i-1] == 'W':temp += 1
    if s[i] == 'E':temp -= 1
    if temp<res:res = temp
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	a = input()
    
	W = [0]*(N+1)
	E = [0]*(N+1)
	for i in range(N):
		if a[i] == 'W':
			W[i+1] = W[i] + 1
			E[i+1] = E[i]
		else:
			W[i+1] = W[i]
			E[i+1] = E[i] + 1
	E_max = E[-1]
	n_min = N
	for i in range(N):
		t = W[i] + E_max - E[i+1]
		if t < n_min:
			n_min = t
	print (n_min)
main()

Submission status: Accepted

Solution code:
_ = input()
S = list(input())

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

temp = 0
right = []
for i, s in enumerate(S[::-1]):
    right.append(temp + left[-i-1])
    if s == 'E':
        temp += 1
print(min(right))

Submission status: Accepted

Solution code:
from bisect import *

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()
S = input()

accWfromL = [0] * (N+1)
accEfromR = [0] * (N+1)

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

print(min(accWfromL[i] + accEfromR[i+1] for i in range(N)))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = [0]
e = [0]

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

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

Submission status: Accepted

Solution code:
_ = input()
aa = input()

west_count = [None for _ in aa]
east_count = [None for _ in aa]

west_count[0] = 0
for i in range(len(aa) - 1):
    if aa[i] == 'W':
        west_count[i + 1] = west_count[i] + 1
    else:
        west_count[i + 1] = west_count[i]

east_count[-1] = 0
for i in range(len(aa) - 1, 0, -1):
    if aa[i] == 'E':
        east_count[i - 1] = east_count[i] + 1
    else:
        east_count[i - 1] = east_count[i]

candidate_count = len(aa)
for i in range(len(aa)):
    candidate_count = min(west_count[i] + east_count[i], candidate_count)

print(candidate_count)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    s = input()

    west_list = []
    east_list = []
    west_list.append(0)
    east_list.append(0)

    for i in range(n - 1):
        prev = west_list.pop()
        now = prev + (1 if s[i] == "W" else 0)
        west_list.append(prev)
        west_list.append(now)
        prev = east_list.pop()
        now = prev + (1 if s[n - 1 - i] == "E" else 0)
        east_list.append(prev)
        east_list.append(now)
    east_list.reverse()

    print(min(a + b for a, b in zip(east_list, west_list)))

Submission status: Accepted

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

Submission status: Accepted

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

lE = [0] * N
lW = [0] * N
le = lw = 0
for i in range(N):
    if S[i] == "E":
        le += 1
    else:
        lw += 1
    lE[i] = le
    lW[i] = lw

rtn = lE[-1] - lE[0]

for i in range(1, N):
    tmp = lW[i-1] + lE[-1] - lE[i]
    if tmp <= rtn:
        rtn = tmp


print(rtn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
if len(S) != N: raise

lcost = [0] * N  # lcost[i]はS[:i]中の'W'の個数
rcost = [0] * N  # rcost[i]はS[i+1:]中の'E'の個数
for i in range(N - 1):
    lcost[i + 1] = lcost[i] + (1 if S[i] == 'W' else 0)
for i in range(N - 1, 0, -1):
    rcost[i - 1] = rcost[i] + (1 if S[i] == 'E' else 0)

print(min(p[0] + p[1] for p in zip(lcost, rcost)))

Submission status: Accepted

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

list = list(s)
count = 0
E_li = [0]

for i in range(0,N):
	if list[i] == 'E':
		count += 1
	E_li.append(count)

all_E = E_li[N]
n_W = 0
n_E = 0
ans = N

for i in range(0,N):
	n_W = i - E_li[i]
	n_E = all_E - E_li[i+1]
	ans = min(ans, n_W + n_E)

print ans

Submission status: Accepted

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

west = S.count("W")
east = S.count("E")

change = east
if S[0] == "E":
    change -= 1
least = change

for i in range(1,N):
    if S[i-1] == "W":
        change += 1
    if S[i] == "E":
        change -= 1
    if change<least:
        least = change

print(least)

Submission status: Accepted

Solution code:
import sys
a = input()
strs = input()
temp = []
for char in strs:
    if char == 'E':
        temp.append(0)
    else:
        temp.append(1)

subsum1 = [0]
subsum2 = [0]
for num in temp[:-1]:
    subsum1.append(subsum1[-1]+num)
for num in temp[:0:-1]:
    subsum2.append(subsum2[-1]+num)
subsum2 = subsum2[::-1]

minturn = sys.maxsize
for i in range(len(temp)):
    minturn = min(minturn, subsum1[i]-subsum2[i]+len(temp)-1-i)

print(minturn)

Submission status: Accepted

Solution code:
from itertools import accumulate

n = int(input())
s = [c == 'E' for c in input()]

l = reversed(tuple(accumulate([0] + s[:0:-1])))
r = accumulate([0] + [not c for c in s[:-1]])

print(min(map(sum, zip(l, r))))

Submission status: Accepted

Solution code:
from itertools import accumulate

n = int(input())
s = [c == 'E' for c in input()]

l = list(accumulate([0] + s[:0:-1]))[::-1]
r = accumulate([0] + [not c for c in s[:-1]])

print(min(map(sum, zip(l, r))))

Submission status: Accepted

Solution code:
input()
S = input()
res = cnt = S.count('E')
for c in S:
    if c == 'E':
        cnt -= 1
        res = min(res, cnt)
    else:
        cnt += 1
print(res)

Submission status: Accepted

Solution code:
input()
S = input()
res = cnt = S.count('E')
for c in S:
    cnt -= c == 'E'
    res = min(res, cnt)
    cnt += c == 'W'
print(res)

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
# C - Attention

# C_Attention_beta.py

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

e = [0 for i in range(N)]
w = [0 for i in range(N)]

# 累積和を求める
# そこからE側の人で、Wを向くべきなのにEを向いている人
for i in range(N):
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1
# そこからW側の人で、Eを向くべきなのにWを向いている人
for j in range(N):
    i = N-1-j
    if i < N-1:
        e[i] += e[i+1]
    if i > 0:
        if S[i] == "E":
            e[i-1] += 1

min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
# C - Attention

# C_Attention.py

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

e = [0 for i in range(N)]
w = [0 for i in range(N)]

# 累積和を求める
for i in range(N):
    # そこからE側の人で、Wを向くべきなのにEを向いている人
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1
    # そこからW側の人で、Eを向くべきなのにWを向いている人
    if i > 0:
        e[N-1-i] += e[N-1-(i-1)]
    if i < N-1:
        if S[N-1-i] == "E":
            e[N-1-(i+1)] += 1

min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
# C - Attention

# C_Attention_beta.py

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

e = [0 for i in range(N)]
w = [0 for i in range(N)]
d = [0 for i in range(N)]

# 累積和を求める
for i in range(N):
    # そこからE側の人で、Wを向くべきなのにEを向いている人
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1


# そこからW側の人で、Eを向くべきなのにWを向いている人
for j in range(N):
    i = N-1-j
    if i < N-1:
        e[i] += e[i+1]
    if i > 0:
        if S[i] == "E":
            e[i-1] += 1


min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
# C - Attention

# C_Attention.py

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

e = [0 for i in range(N)]
w = [0 for i in range(N)]
d = [0 for i in range(N)]

# 累積和を求める
for i in range(N):
    # そこからE側の人で、Wを向くべきなのにEを向いている人
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1
    # そこからW側の人で、Eを向くべきなのにWを向いている人
    if i > 0:
        e[N-1-i] += e[N-1-(i-1)]
    if i < N-1:
        if S[N-1-i] == "E":
            e[N-1-(i+1)] += 1

min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
# C - Attention

# C_Attention.py

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

e = [0 for i in range(N)]
w = [0 for i in range(N)]
d = [0 for i in range(N)]
#print("累積和")

for i in range(N):
    # そこからE側の人で、Wを向くべきなのにEを向いている人
    #if S[i] == "E":
    #    for j in range(0, i):
    #        e[j]+=1
    #        d[j]+=1
    # そこからW側の人で、Eを向くべきなのにWを向いている人
    if i > 0:
        w[i] += w[i-1]
    if i < N-1:
        if S[i] == "W":
            w[i+1] += 1
    if i > 0:
        e[N-1-i] += e[N-1-(i-1)]
    if i < N-1:
        if S[N-1-i] == "E":
            e[N-1-(i+1)] += 1

    #print("i:", i)
    #print("w:", w)
    #print("e:", e)
    #print("")

#w[0] = 0
#e[-1] = 0

#print("w:", w)
#print("e:", e)
min = 10 ** 6
for i in range(N):
    total = e[i] + w[i]
    if total < min:
        min = total
print(min)

Submission status: Accepted

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

ans = line[1:N].count('E')
tmp = ans
for i in range(1,N):
 if line[i] == 'E':
  tmp -= 1
 if line[i-1] == 'W':
  tmp += 1
 if ans>tmp:
  ans = tmp

print(ans)

Submission status: Accepted

Solution code:
#リーダーを向け

n=int(input())
s=input()
k=0
l=[]
for i in range(n):
    if s[i]=="W":
        k+=1
    l.append(k)
#print(l)
#既に追加されたものまで変更されはしなかった
#kが要素かどうか決めつけることはできないからかな
w=n
for i in range(n):
    if i==0:
        #if s[i]=="W":
        #    v=n-l[n-1]+1
        #else:
        #    v=n-l[n-1]
        #下のようにすれば分ける必要はない
        v=n-1-l[n-1]+l[0]
    else:
        v=l[i-1]+n-1-i-(l[n-1]-l[i])


    if v<w:
        w=v
    if v==0:
        w=0
        break    
print(w)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
k=0
l=[]
for i in range(n):
    if s[i]=="W":
        k+=1
    l.append(k)
#print(l)
#既に追加されたものまで変更されはしなかった
w=n
for i in range(n):
    if i==0:
        #if s[i]=="W":
        #    v=n-l[n-1]+1
        #else:
        #    v=n-l[n-1]
        #下のようにすれば分ける必要はない
        v=n-1-l[n-1]+l[0]
    else:
        v=l[i-1]+n-1-i-(l[n-1]-l[i])


    if v<w:
        w=v
print(w)

Submission status: Accepted

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
ss = input()
loss = ss[1:].count("E")
min_loss = loss
ss_before = ss[:-1]
ss_after = ss[1:]
for b, a in zip(ss_before, ss_after):
    loss += b == "W"
    loss -= a == "E"
    if loss < min_loss:
        min_loss = loss
print("{}".format(min_loss))

Submission status: Accepted

Solution code:
'''input
8
WWWWWEEE
'''
n = input()
s =raw_input().strip()
ee = [0]*(n+1)
ww = [0]*(n+1)
for i in xrange(n):
	if i == 0:
		if s[i] == 'E':
			ee[i] = 1
		continue
	if s[i] == 'E':
		ee[i] = ee[i-1]+1
	else:
		ee[i] = ee[i-1]
for i in xrange(n-1,-1,-1):
	if i == n-1:
		if s[i] == 'W':
			ww[i] = 1
		continue
	if s[i] == 'W':
		ww[i] = ww[i+1]+1
	else:
		ww[i] = ww[i+1]
ans = 10**10
for i in xrange(1,n-1):
	ans = min(ans,n-ee[i-1]-ww[i+1]-1)
print min(ans,n-ww[1]-1,n-ee[n-2]-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

position_L = []
position_R = []
for s in S:
    if s == 'W':
        position_R.append(0)
    else:
        position_R.append(1)
        
flips = sum(position_R[1:])
flips_temp = flips
#print(0,flips_temp)
i = 1
while i < N:
    #print(i,flips_temp)
    flips_temp = flips_temp - position_R[i] + ((-1) * position_R[i-1] + 1)
    if flips_temp < flips:
        flips = flips_temp
    i = i + 1
print(flips)

Submission status: Accepted

Solution code:
input()
s = input()

l = 0
r = s[1:].count('E')
x = l + r

for i in range(1, len(s)):
  if s[i - 1] == 'W':
    l += 1

  if s[i] == 'E':
    r -= 1
  x = min(x, l + r)

print(x)

Submission status: Accepted

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

w = 0
e = S[1:].count('E')

leader = S[0]
min_mem=e
cnt_mem=0

for i in range(1, N):
    if leader ==  'W':
        w += 1
    leader = S[i]

    if leader == 'E':
        e -= 1

    cnt_mem = w + e
    if cnt_mem < min_mem:
        min_mem = cnt_mem

print(min_mem)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
left = 0
right = S[1:].count("E")
ans = left + right

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

Submission status: Accepted

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

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

for i in range(1, N):
	if S[i - 1] == "W":
		left += 1
	if S[i] == "E":
		right -= 1
	result = min(result, left + right)
print(result)

Submission status: Accepted

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

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

arr.append(num)

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

print(min(arr))

Submission status: Accepted

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

length = len(S)
min_num = 300000
left = 0
right = S.count("E")

for i in range(length):

  if i > 0:
    left += int(S[i - 1] == "W")

  if i < length:
    right -= int(S[i] == "E")

  if left + right < min_num:
    min_num = left + right

print(min_num)

Submission status: Accepted

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

#前計算
order=0
for c in S:
    if c=='E':
        order+=1

#計算
ans = N
for i in range(N):
    #before
    if S[i]=='E':
        order-=1
    #calc
    ans=min(ans,order)
    #after
    if S[i]=='W':
        order+=1
print(ans)

Submission status: Accepted

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

result = 300000

before = 0
after = 0

for i in range(n):
    if s[i] == "E":
        before = before + 1
    if before > after:
        before = after
    if s[i] == "W":
        after = after + 1

print min(before, after)

Submission status: Accepted

Solution code:
import numpy as np

if __name__ == '__main__':
    N = int(input())
    S = input()
    str_l = np.array(list(S))
    direction_E = (str_l == 'E').astype(int)
    direction_W = (str_l == 'W').astype(int)
    direction = direction_W - direction_E
    min = sum = np.sum(direction_E)
    for i in range(N):
        sum += direction[i]
        if sum < min:
            min = sum
    print(min)

Submission status: Accepted

Solution code:


def west_count(s):
    a = []
    n = 0
    for c in s:
        a.append(n)
        if c == "W":
            n += 1
    return a


def east_count(s):
    a = []
    n = 0
    for c in reversed(s):
        a.append(n)
        if c == "E":
            n += 1
    return list(reversed(a))


n = int(input())
s = input()
w = west_count(s)
e = east_count(s)

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

print(ans)

Submission status: Accepted

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

d = {"W" : 0, "E" : 0}
for i in range(N):
    d[S[i]] += 1

t = {"W" : 0, "E" : 0}
ans = 300000
for i in range(N):
    if S[i] == "W":
        ans = min(ans,t["W"] + d["E"] - t["E"])
    else:
        ans = min(ans,t["W"] + d["E"] - t["E"] - 1)
    t[S[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
length = int(raw_input())
directions = raw_input()

from_west = [None] * length
from_east = [None] * length

# west count
west = 0
east = 0
west_reverse = 0
east_reverse = 0

for n in range(length):
    if directions[n] == 'W':
        west += 1
    else:
        east += 1

    if directions[-(n + 1)] == 'W':
        west_reverse += 1
    else:
        east_reverse += 1

    from_west[n] = (west, east)
    from_east[-(n + 1)] = (west_reverse, east_reverse)


min_length = length + 1
for i in range(length):
    left = None
    right = None
    if i != 0:
        left = from_west[i - 1]
    if i != length - 1:
        right = from_east[i + 1]

    left_turns = 0
    if left:
        left_turns = left[0]
    right_turns = 0
    if right:
        right_turns = right[1]

    if left_turns + right_turns < min_length:
        min_length = left_turns + right_turns

print min_length

Submission status: Accepted

Solution code:
#coding: utf-8
import sys

# 入力
n = int(sys.stdin.readline().strip())
s = list(sys.stdin.readline().strip())


counts = []
counts.append([0] * n)
counts.append([0] * n)

length = len(s)

if (s[0] == 'W'):
     counts[0][0] = counts[0][0] + 1

if (s[length - 1] == 'E'):
    counts[1][length-1] = counts[1][length-1] + 1



for i in range(1, length):
    counts[0][i] = counts[0][i - 1]
    counts[1][length -1 - i] = counts[1][length - i]

    if (s[i] == 'W'):
        counts[0][i] = 1 + counts[0][i]

    if (s[length - i - 1] == 'E'):
        counts[1][length - i - 1] = 1 + counts[1][length - i - 1]

leaders = [0] * n
for i in range(length):
    if (i == 0):
        leaders[i] = counts[1][i + 1]
    elif(i == length - 1):
        leaders[i] = counts[0][i - 1]
    else:
        leaders[i] = counts[0][i - 1] + counts[1][i + 1]


# 出力
print(min(leaders))

Submission status: Accepted

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

l = 0
r = S[1:].count('E')
result = l + r

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

print(result)

Submission status: Accepted

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

ans_list = [0 for _ in range(n)]
ans_list[0] = s[1:].count("E")
ans = ans_list[0]

for i in range(1, n):
	if s[i-1] == "E" and s[i] == "E":
		ans_list[i] = ans_list[i-1] - 1

	elif s[i] == "W" and s[i-1] == "W":
		ans_list[i] = ans_list[i-1] + 1

	else:
		ans_list[i] = ans_list[i-1]

	if ans_list[i] < ans:
		ans = ans_list[i]

print(ans)

Submission status: Accepted

Solution code:
from sys import stderr
from functools import reduce
from operator import add
def f(): return [int(i) for i in input().split()]
def debug(*x): stderr.write(reduce(add,repr(x)).strip("()\',")+'\n')

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

must = [0 for i in range(n)]
# 一番左の人の値を決める
for i in range(1,n):
    if s[i] == "E":
        must[0] += 1
# 2番目以降の人
for j in range(1,n):
    must[j] = must[j-1]
    if s[j-1] == "W":
        must[j] += 1
    if s[j] == "E":
        must[j] -= 1
print(min(must))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
num_E = []
for i in range(N):
  if i == 0 and S[i] == 'E':
    num_E.append(1)
  elif i == 0 and S[i] == 'W':
    num_E.append(0)
  elif S[i] == 'E':
    num_E.append(num_E[i - 1] + 1)
  else:
    num_E.append(num_E[i - 1])
num_turn = [None for _ in range(N)]
ans = 100000000000000000000000
for i in range(N):
  me = 1 if (S[i] == 'E') else 0
  num_turn[i] = i - (num_E[i] - me) + (num_E[N - 1] - num_E[i])
  if ans > num_turn[i]:
    ans = num_turn[i]
print(ans)

Submission status: Accepted

Solution code:
def foo (s):
    ans = 0
    count = 0
    for i in s:
        if i == 'W':
            count += 1
        elif count :
            count -= 1
            ans += 1
        else:
            pass
    return ans

n = input()
s = input()
 
print(foo(s))

Submission status: Accepted

Solution code:
def foo (s):
	ans = 0
	count = 0
	for i in s:
		if   (not count) and (i == 'E'):pass
		elif not count: count += 1
		elif i == 'E': count -= 1; ans += 1
		else: count += 1
	return ans

n = input()
s = input()

print(foo(s))

Submission status: Accepted

Solution code:
n = int(raw_input())
S = raw_input().strip()

face_E = 0
face_W = 0
for i in S:
    if i=='E':
        face_E+=1
    else:
        face_W+=1

change = n
left_E = 0
left_W = 0
right_E = face_E
right_W = face_W
for people in S:
    if people == 'W':
        right_W -= 1
        change = min(change, left_W + right_E)
        left_W += 1
    else:
        right_E -= 1
        change = min(change, left_W + right_E)
        left_E += 1

print change

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:
import sys
 
n = int(sys.stdin.readline())
s = list(sys.stdin.readline())

it = s[0]
w = 0
e = s[1:].count('E')

m = e
j = 0

for i in range(1, len(s)):
	if it == 'W':
		w = w + 1

	it = s[i]

	if it == 'E':
		e = e - 1

	m2 = w + e
	if m2 < m:
		m = m2
		j = i
print(m)

Submission status: Accepted

Solution code:
da  = int(input())

k = list(input())
p = [0]*da
for i in range(da):
  if k[i] == "E" and i < da -1:
    p[i+1] += 1
  elif k[i] == "W":
    p[0] += 1
    p[i] -= 1

for i in range(1,da):
  p[i] += p[i-1]

k = max(p)
if max(p) != p[0] or max(p) != p[da-1]:
  k += 1
print(da - k)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = input()

left = 0
right = S.count('E')

if S[0] == 'E' :
    right -= 1

sum_ = left + right

for i in range(len(S) - 1):
    now = S[i]
    nxt = S[i+1]

    if now == 'W':
        left += 1
    if nxt == 'E':
        right -=1

    if left + right < sum_:
        sum_ = left + right

# when the last one is a leader
left = S.count('W')
right = 0

if S[-1] == 'W':
    left -= 1
if left + right < sum_:
    sum_ = left + right

print(sum_)