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 = [0]*n
E = [0]*n

if s[0] == 'W':
	W[0] = 1
if s[-1] == 'E':
	E[-1] = 1

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

for j in range(n-2, -1, -1):
	if s[j] == 'E':
		E[j] = E[j+1] + 1
	else:
		E[j] = E[j+1]

ans = [0]*n

for k in range(0, n):
	if k == 0:
		ans[0] = E[1]
	elif k == n-1:
		ans[n-1] = W[-2]
	else:
		ans[k] = W[k-1] + E[k+1]

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def resolve():
        input = sys.stdin.readline
        N=int(input().rstrip())
        #S=input()
        #a=len(S)
        S=list(input().rstrip().replace("W","1").replace("E","0"))

        csum=["0"]+S
        csum=list(map(int,csum))
        csum=list(accumulate(csum))#Wの数
        ans=N
        for i in range(1,N+1,1):
            numWleft=csum[i-1]-csum[0]
            numEright=(N-i)-(csum[N]-csum[i])
            ans=min(ans,numWleft+numEright)
        print(ans)

from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5
WEEWW"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """12
WEWEWEEEWWWE"""
        output = """4"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """8
WWWWWEEE"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
A = [0] * (N + 1)
mx = -2
pos = 0 
for i in range(N):
        if S[i] == "E":
                A[i + 1] = A[i] + 1   
        else:
                A[i + 1] = A[i] - 1   
        if mx < A[i + 1]: 
                pos = i + 1 
                mx = A[i + 1]
print(S[:pos - 1].count("W") + S[pos:].count("E"))

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
S = input()

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

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

Submission status: Accepted

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

w = 0
e = 0
for i in s:
  if i =="W":
    pass
  else:
    e +=1
n=e
for i in s:
  if i=="E":
    e-=1
    n = min(n,e+w)
  if i =="W":
    w+=1
print(n)

Submission status: Accepted

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

ans=float("inf")
#まずいちばん西の人がリーダーの時、何人が向く方向を変えなきゃいけないか求める
defa=0
for i in range(1,n):
  if s[i]=="E":
    defa+=1
ans=min(ans,defa)

for i in range(1,n):
  if s[i-1]=="W":
    defa+=1
  if s[i]=="E":
    defa-=1
  ans=min(ans,defa)
print(ans)

Submission status: Accepted

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

e = [0]
ei = 0

for c in s:
    if c == "E":
        ei += 1
    e.append(ei)

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

Submission status: Accepted

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

e = [0]
w = [0]
ei = 0
wi = 0

for c in s:
    if c == "E":
        ei += 1
    else:
        wi += 1
    e.append(ei)
    w.append(wi)
e.append(ei)
w.append(wi)

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

Submission status: Accepted

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

tbl = [0] * n

cnt = 0
for i in range(1, n):
    if s[i - 1] == 'W':
        cnt += 1
    tbl[i] += cnt

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

print(min(tbl))

Submission status: Accepted

Solution code:
N = int(input())
s = input()
Sww = 0
Se = 0
for i in range(N) :
    if s[i] == 'E' :
        Se+=1
sum = N+10
for i in range(N) :
    if s[i] == 'E' :
        Se-=1
    sum1 = Sww + Se
    sum = min(sum1,sum)
    if s[i] == 'W' :
        Sww+=1
print(sum)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
E=[0]*n
W=[0]*n

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

Submission status: Accepted

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

L = [0] * N
R = [0] * N

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

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

LR = [0] * N
for i in range(N):
  LR[i] = L[i] + R[i]
print(N - max(LR) - 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
S = list(input())
dp = [[0,0] for _ in range(N)]
cnt = 0
ans = []
for i in range(1,N):
    if S[i] == "E":
        cnt += 1
dp[0][1] = cnt
ans.append(sum(dp[0]))
for j in range(1,N):
    if S[j-1] == "W":
        dp[j][0] = dp[j-1][0] + 1
    else:
        dp[j][0] = dp[j-1][0]
    if S[j] == "E":
        dp[j][1] = dp[j-1][1] - 1
    else:
        dp[j][1] = dp[j-1][1]
    ans.append(sum(dp[j]))
print(min(ans))

Submission status: Accepted

Solution code:
# coding: utf-8
#累積和
N=int(input())
S=[i for i in input()]

pls_E=[0]*N
pls_W=[0]*N

#初期値
if S[N-1]=='E':
    pls_E[N-1]=1
else:
    pls_E[N-1]=0
    
if S[0]=='W':
    pls_W[0]=1
else:
    pls_W[0]=0

#Wの累積和(左から)
for i in range(1,N):
    if S[i]=='W':
        pls_W[i]=pls_W[i-1]+1
        
    else:
        pls_W[i]=pls_W[i-1]

#Eの累積和（右から）
for i in range(N-2,-1,-1):
    if S[i]=='E':
        pls_E[i]=pls_E[i+1]+1
    else:
        pls_E[i]=pls_E[i+1]

#print(pls_W,pls_E)
ans=float('inf')
for i in range(1,N-1):
    ans=min(ans,pls_W[i-1]+pls_E[i+1])
    
print(min(ans,pls_E[1],pls_W[N-2]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
leftW = 0
leftE = 0
allW = s.count("W")
allE = s.count("E")
cost = 0
mincost = n
for si in s:
  if si == "W":
    rightW = allW - leftW - 1
    rightE = allE - leftE
    cost = leftW + rightE
    leftW += 1
  else:
    rightW = allW - leftW
    rightE = allE - leftE - 1
    cost = leftW + rightE
    leftE += 1
  mincost = min(mincost, cost)
print(mincost)

Submission status: Accepted

Solution code:
n=int(input()) #n<=3*(10**5)
s=list(input()) #E=右向いてる　W=左向いてる　リーダーより左側の人は全員Eになる。右側はWになる。
left,right=[0],[0] #一番端のときは隣居ない
left_num,right_num=0,0
for i in range(1,n): #左にいるWの人数　右にいるEの人数を調べる
    if s[i-1]=="W": #左側　0-(n-2)
        left_num+=1
    left.append(left_num) 
    if s[n-i]=="E": #右側　(n-1)-1
        right_num+=1
    right.append(right_num)
#print(left)
#print(right)
ans=1000000
for i in range(n): #リーダーによって振り向かせる人数を調べれ、minを更新
    num=left[i]+right[n-(i+1)]
    ans=min(ans,num)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

S = [0] * n
countW = 0
for i in range(n):
    if s[i] == "W":
        S[i] = 1
        countW += 1

ans = []
countE = n - countW
left = 0
right = 0

for i in range(n):
    if i == 0:
        if S[0] == 0:
            ans.append(countE - 1)
        else:
            ans.append(countE)
    elif 0 < i < n-1:
        left += S[i-1]
        right = n - i - 1 - countW + left + S[i]
        ans.append(left + right)
    elif i == n-1:
        ans.append(countW - S[-1])
print(min(ans))

Submission status: Accepted

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

S = [0] * n
countW = 0
for i in range(n):
    if s[i] == "W":
        S[i] = 1
        countW += 1

ans = []
countE = n - countW
left = 0
right = 0

for i in range(n):
    if i == 0:
        if S[0] == 0:
            ans.append(countE - 1)
        else:
            ans.append(countE)
    elif 0 < i < n-1:
        left += S[i-1]
        # right = countE - i - 1 + left
        right = n - i - 1 - countW + left + S[i]
        ans.append(left + right)
        # print("------------")
        # print("i: " + str(i))
        # print("left: " + str(left))
        # print("right: " + str(right))
    elif i == n-1:
        ans.append(countW - S[-1])
# print("*********")
# print(countE)
# print(ans)
print(min(ans))

Submission status: Accepted

Solution code:
from itertools import accumulate
N=int(input())
A=list(input())
E=[1 if A[i] =="E" else 0 for i in range(N) ]
W=[1 if A[i] =="W" else 0 for i in range(N) ]
E=list(accumulate(E[::-1]))[::-1]
W=list(accumulate(W))
min_=N
for i in range(N):
    min_=min(min_,E[i]+W[i]-1)
print(min_)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
S='E'+S+'W'

Es=[0]*(N+2)
Ws=[0]*(N+2)

for i in range(1, N+1):
  if S[i-1]=='E':
    Es[i]=Es[i-1]
  else:
    Es[i]=Es[i-1]+1
    
for i in range(N, 0, -1):
  if S[i+1]=='W':
    Ws[i]=Ws[i+1]
  else:
    Ws[i]=Ws[i+1]+1
    
s=[Es[i]+Ws[i] for i in range(1, N+1)]
print(min(s))

Submission status: Accepted

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

cumsum = [0] * n

# 西を向いている人数の累積和
for i in range(n):
    dir = s[i]
    prev = cumsum[i - 1] if i > 0 else 0
    cur = 1 if dir == 'W' else 0
    cumsum[i] = prev + cur


num_min = 1000000


for i in range(n):
    # i 番目の人がリーダーになる場合
    # リーダーより西の人で向きを変える人の数
    # = i番目より前で西を向いている人数
    w = cumsum[i - 1] if i > 0 else 0
    # リーダーより東の人で向きを変える人の数
    # = i番目より後で東を向いている人数
    e = n - (i + 1) - (cumsum[-1] - cumsum[i])

    num_min = min(num_min, w + e)

print(num_min)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
l_w = [0]
l_e = [0]
cnt = 0
lists = []
for i in range(n):
    if s[i] == "W":
        cnt += 1
        l_w.append(cnt)
    else:
        l_w.append(cnt)
cnt = 0
for i in range(n):
    if s[i] == "E":
        cnt += 1
        l_e.append(cnt)
    else:
        l_e.append(cnt)
for i in range(len(l_e)-1):
    ans = l_w[i] + (l_e[-1] - l_e[i+1])
    lists.append(ans)
print(min(lists))

Submission status: Accepted

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

w_cnt = 0
e_cnt = 0
E = s.count("E")
ans = 10 ** 9

for i in s:
    if i == "E":
        e_cnt += 1
    ans = min(ans, w_cnt + E - e_cnt)
    if i == "W":
        w_cnt += 1

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
# 左からi文字目までに存在するW,Eの累積和
east = [0] * (n + 1)
west = [0] * (n + 1)
for i in range(n):
    east[i + 1] = east[i]
    west[i + 1] = west[i]
    if s[i] == 'E':
        east[i + 1] += 1
    else:
        west[i + 1] += 1

ans = float('inf')
for i in range(1, n):
    tmp = west[i - 1] + east[-1] - east[i]
    ans = min(ans, tmp)
# N番目
ans = min(ans, west[n - 1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
# 左からi文字目までに存在するW,Eの累積和
east = [0] * (n + 1)
west = [0] * (n + 1)
for i in range(n):
    east[i + 1] = east[i]
    west[i + 1] = west[i]
    if s[i] == 'E':
        east[i + 1] += 1
    else:
        west[i + 1] += 1

ans = float('inf')
# 1番目がリーダー
ans = min(ans, east[-1] - east[2])
# 2~N-1番目
for i in range(2, n):
    tmp = west[i - 1] + east[-1] - east[i]
    ans = min(ans, tmp)
# N番目
ans = min(ans, west[n - 1])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
dpE=[0]*n #その人より右で、Eを向いている人
dpW=[0]*n #その人より左で、Wを向いている人
for i in range(1,n):
    if s[i-1]=="W":
        dpE[i]=dpE[i-1]+1
    else:
        dpE[i]=dpE[i-1]
    if s[n-i]=="E":
        dpW[n-i-1]=dpW[n-i]+1
    else:
        dpW[n-i-1]=dpW[n-i]
print(min([dpE[i]+dpW[i] for i in range(n)]))

Submission status: Accepted

Solution code:


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


num_W = [0]*N # ある人の左にいるWの数
num_E = [0]*N # ある人の右にいるEの数

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

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

ans = N
for i in range(N):
    ans = min(ans, num_E[i] + num_W[i])

print(ans)

Submission status: Accepted

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

# 左から
l = 0
li = 0
lmax = -(10**8)
for i in range(N):
    if S[i] == 'W':
        l -= 1
    else:
        l += 1
    if l > lmax:
      lmax = l
      li = i

lans = 0
for i in range(N):
    if i < li:
        if S[i] == 'W':
            lans += 1
    if li < i:
        if S[i] == 'E':
            lans += 1

# 右から
r = 0
ri = 0
rmax = -(10**8)
for i in reversed(range(N)):
    if S[i] == 'E':
        r -= 1
    else:
        r += 1
    if r > rmax:
      rmax = r
      ri = i

rans = 0
for i in range(N):
    if i < ri:
        if S[i] == 'W':
            rans += 1
    if ri < i:
        if S[i] == 'E':
            rans += 1

# print(li, ri)
print(min(lans, rans))

Submission status: Accepted

Solution code:
#098c
N = int(input())
S = "0" + str(input())

#累積和を使う
cumsum_E = [0]
cumsum_W = [0]

for i in range(1,N+1):
    if S[i]=="E":
        cumsum_E.append(cumsum_E[i-1]+1)
        cumsum_W.append(cumsum_W[i-1])
    elif S[i]=="W":
        cumsum_E.append(cumsum_E[i-1])
        cumsum_W.append(cumsum_W[i-1]+1)

cumsum_E.pop(0)
cumsum_W.pop(0)
        
#print("E:",cumsum_E)
#print("W:",cumsum_W)

ans = N

for i in range(0,N):
    E_to_W = cumsum_E[N-1] - cumsum_E[i]
    if i!=0:
        W_to_E = cumsum_W[i-1]
    else:
        W_to_E = 0
    
    change = E_to_W + W_to_E
    #print(i,E_to_W,W_to_E,change)
    if change < ans:
        ans = change
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
w = 0
e = 0
for s in S:
  if s == "E":
    e += 1
ans = e
for i in range(1,N):
  if S[i-1] == "W":
    w += 1
    if S[i] == "E":
      ans = min(ans, abs(w+e-1))
      continue
  else:
    e -= 1
    if S[i] == "E":
      ans = min(ans, abs(w+e-1))
      continue
  ans = min(ans, abs(w+e))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
wr=s.count("W")
er=s.count("E")
wl=0
el=0
cur=None
ans=len(s)
for i in range(n):
  if cur=="W":
    wl+=1
  elif cur=="E":
    el+=1
  cur=s[i]
  if cur=="W":
    wr-=1
  else:
    er-=1
    
  ans=min(ans,wl+er)
print(ans)

Submission status: Accepted

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

#選んだリーダーよりも左 -> E, 右 -> W をとる
#数えるのはリーダーよりも左にいるWと右にいるE
E = []
W = []
e, w = 0, 0

for i in s:
    if i == 'E':
        e += 1
    elif i == 'W':
        w += 1
    E.append(e)
    W.append(w)

#k番目がリーダーとすると
ans = 10 ** 10
W.insert(0,0)

for k in range(n):
    can = W[k] + (E[-1]-E[k])
    if can <= ans:
        ans = can

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
s=list(input())
ans=10**10
wl=0
el=0
wr=s.count('W')
er=s.count('E')
for i in range(n):
    if wl+er<ans:
        ans=wl+er
    if s[i]=='E':
        el+=1
        er-=1
    else:
        wl+=1
        wr-=1
    if wl+er<ans:
        ans=wl+er
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))
#W,Eどちらかしか無い場合
if len(dic)==1:
  print(0)
  exit()
left_w = 0
right_e = dic["E"]

#(左にあるWの数+右にあるEの数)の最小値が答え
ans = n
for i in range(n):
  if s[i] == "W":
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    ans = min(ans,left_w+right_e)
    right_e -= 1

#(右にあるWの数+左にあるEの数)の最小値が答えの場合もあるか
left_e = 0
right_w = dic["W"]

for i in range(n-1,-1,-1):
  if s[i] == "E":
    ans = min(ans,left_e+right_w)
    left_e += 1
  elif s[i] == "W":
    ans = min(ans,left_e + right_w)
    right_w -= 1

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))
#W,Eどちらかしか無い場合
if len(dic)==1:
  print(0)
  exit()
left_w = 0
right_e = dic["E"]
cnt = [[] for _ in range(n)]
#(左にあるWの数+右にあるEの数)の最小値が答え


ans = n
for i in range(n):
  if s[i] == "W":
    cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    right_e -= 1

#(右にあるWの数+左にあるEの数)の最小値が答えの場合もあるか
left_e = 0
right_w = dic["W"]

for i in range(n-1,-1,-1):
  if s[i] == "E":
    ans = min(ans,left_e+right_w)
    left_e += 1
  elif s[i] == "W":
    ans = min(ans,left_e + right_w)
    right_w -= 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=input()
import collections 
e=a.count("E")
w=0
mini=n
for i in range(n):
    if a[i]=="E":
        e-=1
    if i!=0 and a[i-1]=="W":
        w+=1
    mini=min(mini,e+w)
print(mini)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = list(input())
S_C = Counter(S)
left_num = S_C["E"]
if S[0] == "E":
    left_num -= 1
right_num = 0
ans = left_num + right_num
for i in range(1,N):
    # リーダーが左を向いていた時、-1
    if S[i] == "E":
        left_num -= 1
    if S[i-1] == "W":
        right_num += 1
    tmp_ans = left_num + right_num
    if ans > tmp_ans:
        ans = tmp_ans
print(ans)

Submission status: Accepted

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

E=[0]*N
W=[0]*N
c=0
for i in range(N):
  W[i] =c
  if S[i]=='W':
    c +=1
c=0
for j in range(N):
  E[-1-j]=c
  if S[-1-j]=='E':
    c +=1
E=[E[0]]+E+[0]
W=[0]+W+[W[-1]]
answer=float('inf')
for i in range(N+2):
  answer=min(answer,E[i]+W[i])
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
ws = [0]*N
es = [0]*N
# 自分より前のWと自分より後のEの数だけ振り向かせる
for i in range(N-1):
    if S[i] == 'W':
        ws[i+1] += ws[i]+1
    else:
        ws[i+1] = ws[i]
for i in range(N-1, 0, -1):
    if S[i] == 'E':
        es[i-1] += es[i]+1
    else:
        es[i-1] = es[i]
ans = []
for w, e in zip(ws, es):
    ans.append(w+e)
print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
wcount = 0
wecount = 0
prev = ""
for c in s:
    if c == "W":
        if prev == "E": wcount = 0
        wcount += 1
    if wcount and c == "E":
        wcount -= 1
        wecount += 1
print(wecount)

Submission status: Accepted

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

count = [float('inf')] * n

# 左端
count[0] = 0
for i in range(1, n):
    if s[i] == 'E':
        count[0] += 1

# 尺取
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:
n = int(input())
s = input()

w_to_e = [0]*n
e_to_w = [0]*n

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

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

change = [w_to_e[i] + e_to_w[i] for i in range(n)]

print(min(change))

Submission status: Accepted

Solution code:

def examC():
    N = I()
    S = SI()
    cumE = [0]*(N+1)
    for i in range(N):
        cur = 0
        if S[i]=="E":
            cur = 1
        cumE[i+1] = cumE[i] + cur
    ans = N-1
    for i in range(1,N+1):
        cur = (i-1-cumE[i-1]) + (cumE[N]-cumE[i])
        ans = min(ans,cur)
    print(ans)



import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Accepted

Solution code:
#C
N = int(input())
S = list(str(input()))
look_east = [0]
E = 0
W = 0
for s in S:
    if s == "E":
        look_east.append(look_east[-1]+1)
        E+=1
    else:
        look_east.append(look_east[-1])
        W+=1

ans = N
for i in range(N):
    we = look_east[i]
    if S[i] == "E":
        change = i-we + E-(we+1)
    else:
        change = i-we + E-(we)
        
    ans = min(ans,change)
        
print(ans)

Submission status: Accepted

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

w, e = 0, S.count('E')
ans = n

for s in S:
    if s == 'W':
        ans = min(ans, (w+e))
        w += 1
    else:
        e -= 1
        ans = min(ans, (w+e))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
str_to_int = {"W":0, "E":1}
input_ = input()
row = [str_to_int[input_[n]] for n in range(N)]

min_change_count = N+2

west_cum_sum = 0
east_cum_sum = 0
west_cum_sums = []
east_cum_sums = []
for n in range(N):
    if row[n] == 0:
        west_cum_sum += 1
    if row[N-n-1] == 1:
        east_cum_sum += 1
    west_cum_sums.append(west_cum_sum)
    east_cum_sums.append(east_cum_sum)
    
east_cum_sums = east_cum_sums[::-1]
    
min_count = min(east_cum_sums[1], west_cum_sums[N-2])
for n in range(1, N-1):
    count = west_cum_sums[n-1] + east_cum_sums[n+1]
    if count < min_count:
        min_count = count
        
print(min_count)

Submission status: Accepted

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

e=[1 if c=='E' else 0 for c in s]
w=[1 if c=='W' else 0 for c in s]

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

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

print(m)

Submission status: Accepted

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

ans = float('inf')

W = {'W':0, 'E':0}
n = S.count('E')
E = {'W':N-n, 'E':n}

for s in S:
  E[s] -= 1

  ans = min(ans, E['E'] + W['W'])

  W[s] += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = list(input())

c = Counter(S)
if S[0] == "E":
    change = c["E"] - 1
else:
    change = c["E"]
min_change = change
for i in range(1, N):
    mae = S[i - 1]
    if S[i] == "E":
        change -= 1
    if mae == "W":
        change += 1
    min_change = min(min_change, change)

print(min_change)

Submission status: Accepted

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

w = [0]
e = []
w_tmp = 0
e_tmp = 0
for s in sl:
    if s == "W":
        w_tmp += 1
    else:
        e_tmp += 1
    w.append(w_tmp)
    e.append(e_tmp)

ans = float("inf")
for i in range(n):
    tmp = w[i] + e[-1] - e[i]
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

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

Wlist=[0]*(N+1)
Elist=[0]*(N+1)
for i in range(N):
    Wlist[i+1]=Wlist[i]
    if people[i]=='W':
        Wlist[i+1]+=1
    Elist[i+1]=Elist[i]
    if people[i]=='E':
        Elist[i+1]+=1

#print(Wlist)
#print(Elist)

List=[]
for i in range(N):
    List.append(Wlist[i]+(Elist[N]-Elist[i+1]))

print(min(List))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N =  INT()
S = input()

e = [0]*N
w = [0]*N

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

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

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

Submission status: Accepted

Solution code:
#C-attention
n=int(input())
s=input()
import collections
numberw=s.count("W")
numbere=n-numberw
start=0
if s[0]=="W":
    start=numbere
    
elif s[0]=="E":
    start=numbere-1
ans=n
for i in range(n-1):
    ans=min(ans,start)
    if s[i]=="W" and s[i+1]=="W":
        start+=1
    elif s[i]=="W" and s[i+1]=="E":
        start=start
    elif s[i]=="E" and s[i+1]=="W":
        start=start
    elif s[i]== "E"and s[i+1]=="E":
        start-=1  
    ans=min(ans,start)
print(ans)

Submission status: Accepted

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

#累積和を求める
wcum=[];wcount=0
ecum=[];ecount=0
for i in range(n):
  if s[i]=="W":
    wcount+=1
  else:
    ecount+=1
  ecum.append(ecount)
  wcum.append(wcount)
ans=ecum[n-1]-ecum[0]
for i in range(1,n):
  v=wcum[i-1]+(ecum[n-1]-ecum[i])
  if ans>v:
    ans=v
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import bisect
from itertools import accumulate
import numpy as np

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

mi = 10**10

li = [0]*n
lin = [0]*n

for i in range(len(s)):
    if s[i] == "W":
        li[i] = 1
    if s[n-i-1] == "E":
        lin[i] = 1

li = np.asarray(list(accumulate(li)))
lin = np.asarray(list(accumulate(lin)))[::-1]
lis = li+lin

lis.sort()

print(lis[0]-1)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ne_r = S.count('E')
nw_l = 0
ans = N
for i,c in enumerate(S):
    if c=='E': ne_r-=1
    ans = min(ans, ne_r + nw_l)
    if c=='W': nw_l+=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
lst1=[0]*n
lst2=[0]*n
if s[0]=='W':
  lst1[0]=1
if s[0]=='E':
  lst2[0]=1
for i in range(1,n):
  if s[i]=='W':
    lst1[i]=lst1[i-1]+1
    lst2[i]=lst2[i-1]
  if s[i]=='E':
    lst2[i]=lst2[i-1]+1
    lst1[i]=lst1[i-1]
l=[]
l.append(lst2[n-1]-lst2[0])
for j in range(1,n-1):
  l.append(lst1[j-1]+lst2[n-1]-lst2[j])
l.append(lst1[n-2])

print(min(l))

Submission status: Accepted

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

leader = S[0]
right = 0
left = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E = [0] * N
t = 0
for i, s in enumerate(S[len(S)-1:0:-1]):
    if s == 'E':
        t += 1
    E[-i-2] = t
    
t = 0
for i, s in enumerate(S[:len(S) - 1]):
    if s == 'W':
        t += 1
    E[i+1] += t
    
print(min(E))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
imosw = []
imose = []
imosw.append(0);
imose.append(0);
for i in range(n):
    if s[i]=='W':
        imosw.append(imosw[i]+1)
        imose.append(imose[i])
    else:
        imosw.append(imosw[i])
        imose.append(imose[i]+1)
res = 100000000000
for i in range(n):
    res = min(imosw[i]+imose[n]-imose[i+1],res)
print(res)