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())
l = list(input())

ans = 3*10**5

w_cnt = [0]*n 
e_cnt = [0]*n

for i in range(n):
    if i == 0:
        if l[i] == 'W':
            w_cnt[0] = 1
    if i >= 1 and i <= n-1:
        if l[i] == 'W':
            w_cnt[i] = w_cnt[i-1] + 1
        else:
            w_cnt[i] = w_cnt[i-1]
for i in range(n)[::-1]:
    if i == n-1:
        if l[i] == 'E':
            e_cnt[i] = 1
    if 0 <= i and i <= n-2:
        if l[i] == 'E':
            e_cnt[i] = e_cnt[i+1] + 1
        else:
            e_cnt[i] = e_cnt[i+1]
for i in range(1,n-2):
    ans = min(ans,w_cnt[i-1]+e_cnt[i+1])
print(min(ans,e_cnt[0],w_cnt[n-1]))

Submission status: Accepted

Solution code:
import re

def calc(people):
    Ws = 0
    Es = people[1:].count("E")
    ans = Ws + Es

    for i in range(1, len(people)):
        if people[i] == "E":
            Es -= 1
        
        if people[i - 1] == "W":
            Ws += 1

        ans = min(ans, Ws + Es)
    
    return ans


if __name__ == "__main__":
    _ = input()
    people = input()

    print(calc(people))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
W_count = [0] * (N + 1)
E_count = [0] * (N + 1)

for i in range(len(S)):
  W_count[i + 1] = W_count[i] + (1 if S[i] == 'W' else 0)
  E_count[i + 1] = E_count[i] + (1 if S[i] == 'E' else 0)

# print('W_count')
# print(W_count)
# print('E_count')
# print(E_count)

answer = 4 * (10 ** 5)
for i in range(len(S)):
  if i == 0:
    answer = min(answer, E_count[-1] - E_count[1])
  elif i == len(S) - 1:
    answer = min(answer, W_count[len(S) - 1] - W_count[0])
  else:
    answer = min(answer, W_count[i] - W_count[0] + E_count[-1] - E_count[i + 1])

print(answer)

Submission status: Accepted

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

ans = float('inf')
sum_E = [0]
sum_W = [0]
cnt_e = 0
cnt_w = 0

for i in range(N):
	if S[i] == 'E':
		cnt_e += 1
	else:
		cnt_w += 1
	sum_E.append(cnt_e)
	sum_W.append(cnt_w)

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

print(ans)

Submission status: Accepted

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

cnt = s[1:].count("E")
ans = cnt
for ni in range(1,n):
    if s[ni-1] == "W":
        cnt += 1
    if s[ni] == "E":
        cnt -= 1
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

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

res = float('inf')
W_sum = [0]
E_sum = [S.count('E')]
for i in range(0, N):
    W_sum.append(W_sum[i] + 1 if S[i] == 'W' else W_sum[i]) 
    E_sum.append(E_sum[i] - 1 if S[i] == 'E' else E_sum[i])
    res = min(res, W_sum[i] + E_sum[i])

res = min(res, W_sum[-1] + E_sum[-1])
print(res)

Submission status: Accepted

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

W_sum = [0]
E_sum = [S.count('E')]
for i in range(0, N):
    W_sum.append(W_sum[i] + 1 if S[i] == 'W' else W_sum[i]) 
    E_sum.append(E_sum[i] - 1 if S[i] == 'E' else E_sum[i])

res = float('inf')
for i in range(0, N+1):
    res = min(res, W_sum[i] + E_sum[i])
    
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

east = [0] ## east[i]: (N-i)番目の人より東にいる人で、東を向いている人数, len=(N+1)
west = [0] ## west[i]: (i-1)番目の人より西にいる人で、西を向いている人数, len=(N+1)
for i in range(N-1):
  east.append(east[-1] + (S[N-i-1] == 'E'))
  west.append(west[-1] + (S[i] == 'W'))
  
ans = N
for i in range(N):
  cost = east[N-i-1] + west[i]
  ans = min(ans, cost)
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 10**10
s_E = [0]
s_W = [0]
e = 0
w = 0
for i in range(n):
    if s[i] == 'E':
        e += 1
    else:
        w += 1
    s_E.append(e)
    s_W.append(w)

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

print(ans)

Submission status: Accepted

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

#Eが右、Wが左
E = S.count('E')
cost = 0
result = E
for c in S:
    if c == 'W':
        cost += 1
    else:
        E -= 1

    if cost+E < result:
        result = cost+E

print(result)

Submission status: Accepted

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

W = [0] * N
E = [0] * N

W[0] += S[0] == 'W'
E[-1] += S[-1] == 'E'

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

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

Submission status: Accepted

Solution code:
N = int(input())
# 3 * 10^5
S = input()
#S = [{'W':'<', 'E':'>'}[c] for c in S]

w = 0
e = 0
W = [0]*N
E = [0]*N
for i, c in enumerate(S):
    if c == 'W':
        w+=1
    elif c == 'E':
        e+=1
    W[i] = w
    E[i] = e

m = N
D = [0]*N
result = 0
for i in range(N):
    # > < > < >
    #       W E
    #   o   i
    if i == 0:
        D[i] =  E[N-1] - E[i]
    elif i == N-1:
        D[i] = W[i-1]
    else:
        D[i] = W[i-1] + E[N-1] - E[i]
    if m > D[i]:
        m = D[i]
        result = i+1

#print("S=", *list(S))
#print("W=", *W)
#print("E=", *E)
#print("D=",*D)
#print(result)
print(m)

Submission status: Accepted

Solution code:
n=int(input())
S=list(input())
R=[0]
for i in range(n):
    R.append(R[i]+(S[i]=='W'))
print(min([R[i]+n-i-1-(R[-1]-R[i+1]) for i in range(n)] ))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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


ans = n
if n < 10:
	for i in range(n):
		ans = min((s[:i].count("W")+s[i+1:].count("E")),ans)
else:
	change = (s[1:].count("E"))
	ans = change
	for i in range(1,n):

		t = 0
		if s[i-1] == "W":
			t += 1
		if s[i] == "E":
			t -= 1
		change += t
		ans = min(ans,change)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
people = input()
mini = n
for i in range(1,n+1):
    if i ==1:
        left_e = people[:(i-1)].count("E")
        left_w = people[:(i-1)].count("W")
        right_e = people[i:].count("E")
        right_w = people[i:].count("W")
    else:
        if people[i-1] =="E" and people[i-2] =="W":
            right_e = right_e-1
            left_w = left_w+1
        elif people[i-1] =="E" and people[i-2] =="E":
            left_e = left_e +1
            right_e = right_e -1
        elif people[i-1] == "W" and people[i-2] == "E":
            left_e = left_e+1
            right_w = right_w -1
        else:
            left_w = left_w+1
            right_w = right_w -1
    if left_w+right_e < mini:
        mini = left_w+right_e
    #print(left_e, left_w, right_e, right_w)
print(mini)

Submission status: Accepted

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


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

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

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

ans = 300010
for i in range(0,N):
    if i == 0:
        ans = min(ans, right[i+1])
    elif i == N -1:
        ans = min(ans, left[i-1])
    else:
        ans = min(ans, left[i-1] + right[i+1])


print(ans)

Submission status: Accepted

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

w = [0]
e = [0]

# tmp_w = 0
tmp_e = 0

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(w[i-1] + 1)
        # tmp_w += 1
    else:
        w.append(w[i-1])

    if (s[j+1] == 'E'):
        e.append(tmp_e + 1)
        tmp_e += 1
    else:
        e.append(tmp_e)

e.reverse()

ans = 10 ** 6 
for i in range(n):
    ans = w[i] + e[i] if w[i] + e[i] < ans else ans

print(ans)

Submission status: Accepted

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

w = [0]
e = [0]

tmp_w = 0
tmp_e = 0

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(tmp_w + 1)
        tmp_w += 1
    else:
        w.append(tmp_w)

    if (s[j+1] == 'E'):
        e.append(tmp_e + 1)
        tmp_e += 1
    else:
        e.append(tmp_e)

e.reverse()

# ans = 10 ** 6 
# for i in range(n):
#     ans = w[i] + e[i] if w[i] + e[i] < ans else ans

result = []

for i in range(n):
    result.append(w[i] + e[i])

print(min(result))

Submission status: Accepted

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

w = [0]
e = [0]

tmp_w = 0
tmp_e = 0

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(tmp_w + 1)
        tmp_w += 1
    else:
        w.append(tmp_w)

    if (s[j+1] == 'E'):
        e.append(tmp_e + 1)
        tmp_e += 1
    else:
        e.append(tmp_e)

e.reverse()

ans = 10 ** 6 
for i in range(n):
    ans = w[i] + e[i] if w[i] + e[i] < ans else ans

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    N = readi()
    a = reads()
    e = arr(N + 1)
    w = arr(N + 1)
    for i in range(N):
        e[i + 1] = e[i] + (1 if a[i] == "E" else 0)
        w[i + 1] = w[i] + (1 if a[i] == "W" else 0)

    dbg(e=e)
    dbg(w=w)

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
E_count = 0
W_count = 0
lis = []
for s in S:
    lis.append(W_count)
    if s == 'W':
        W_count += 1
for index, s in enumerate(S[::-1]):
    lis[N-1-index] += E_count
    if s == 'E':
        E_count += 1
minimam = 3*10**5
for count in lis:
    minimam = min(minimam, count)
print(minimam)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = input()

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

    for i in range(1, N):
        if S[i-1] == 'W' and S[i] == 'W':
            new = old + 1
        elif S[i-1] == 'E' and S[i] == 'E':
            new = old - 1
        else:
            new = old

        ans = min(ans, new)
        old = new

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=input()
R=[0]
for i in range(N):
    R.append(R[i]+(A[i]=="W"))
print(min([R[i]+(N-i-1)-(R[-1]-R[i+1]) for i in range(N)]))

Submission status: Accepted

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


def solve(N: int, S: str):

    wc = 0
    ec = S.count("E") 

    min_res = float("inf")
    for s in S:
        if s == "E":
            ec -= 1
        min_res = min(min_res,wc+ec)
        if s == "W":
            wc += 1

    print(min_res)
    

    


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
temp = S[1:].count('E')
ans = [temp]

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

print(min(ans))

Submission status: Accepted

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

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  s=list(S())

  l1=[]
  l2=[]

  a=s[0]
  if a=='W':
    l1.append([1,0])
  else:
    l1.append([0,1])

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

  _s=s[:]
  _s.reverse()

  a=_s[0]
  if a=='W':
    l2.append([1,0])
  else:
    l2.append([0,1])

  for i in range(1,len(_s[1:])+1):
    if _s[i]=='W':
      l2.append([l2[i-1][0]+1,l2[i-1][1]])
    else:
      l2.append([l2[i-1][0],l2[i-1][1]+1])

  l2.reverse()

  # print(l1)
  # print(l2)

  mn=inf
  for i in range(n):
    a=s[i]
    if a=='W':
      l1[i][0]-=1
      l2[i][0]-=1
    else:
      l1[i][1]-=1
      l2[i][1]-=1
    mn=min(mn,l1[i][0]+l2[i][1])

  return mn

print(main())

Submission status: Accepted

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

N = int(input())
S = input()
cnt = [0] * (N+1)
cnt[0] = 0
ans = N

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

for j in range(1, N+1):
    ans = min(ans, cnt[j-1] + (N - j) - (cnt[N]-cnt[j]))

print(ans)

Submission status: Accepted

Solution code:
N = input()
S = input()
cnt = S.count('E')
ans = cnt

for v in S:
  if v == 'E':
    cnt -= 1
  else:
    cnt += 1
  ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
lis = []
Wcounter = 0
Ecounter = 0
for s in S:
    lis.append(Ecounter)
    if s == 'E':
        Ecounter += 1
for index, s in enumerate(S[::-1]):
    lis[N - 1 - index] += Wcounter
    if s == 'W':
        Wcounter += 1
max_num = 0
for num in lis:
    max_num = max(max_num, num)
print(N-1-max_num)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

num_w = 0
num_e = s[1:].count("E")
ans = num_e
for i in range(n):
    if i == 0:
        continue
    if s[i] == "E":
        num_e -= 1
    if s[i-1] == "W":
        num_w += 1        
    ans = min(ans, num_w + num_e)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
e = [0]
w = [0]
for l, r in zip(S[:N-1], S[::-1]):
  nw = w[-1] + (1 if l == 'W' else 0)
  ne = e[-1] + (1 if r == 'E' else 0)
  e.append(ne)
  w.append(nw)
print(min([a + b for a, b in zip(e, w[::-1])]))

Submission status: Accepted

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

Submission status: Accepted

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

turn_cnt = []
east_cnt = 0
total_east = arr.count("E")

for i in range(n):
    left_turn = i - east_cnt
    s = arr[i]
    if s == "E":
        east_cnt += 1
    right_turn = total_east - east_cnt
    turn_cnt.append(left_turn + right_turn)

print(min(turn_cnt))

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    s = input()

    lefte= [0] * n
    leftw = [0] * n
    righte = [0] * n
    rightw = [0] * n




    for i in range(n):
        lefte[i] = lefte[i-1] + (s[i] == 'E') if i>=1 else int(s[i] == 'E')
        leftw[i] = leftw[i-1] + (s[i] == 'W') if i>=1 else int(s[i] == 'W')

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

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

    print(ans)


solve()

Submission status: Accepted

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


def solve(N: int, S: str):
    W = 0
    E = 0
    for c in S:
        if c == 'W':
            W += 1
        else:
            E += 1
    w, e = 0, 0
    ret = float('inf')
    for i in range(N):
        tmp = w + E - e
        if S[i] == 'W':
            w += 1
        else:
            e += 1
            tmp -= 1
        ret = min(ret, tmp)
    print(ret)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

lstE = [0] * (N+2)
lstW = [0] * (N+2)

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

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

ans = N
for k in range(1,N+1):
    ans = min(ans, lstE[k-1]+lstW[k+1])

print (ans)

Submission status: Accepted

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


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


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


###############################################################################


def main():
    n = intin()
    s = input()

    now = 0
    for c in s[1:]:
        if c == 'E':
            now += 1

    ans = now

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

west = [0 for _ in range(N+1)]
east = [0 for _ in range(N+1)]

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

west = west[1:]
east = east[1:]

turn_nums = [0 for _ in range(N)]
turn_nums[0] = east[N-1] - east[0]
turn_nums[N-1] = west[N-2]
for i in range(1, N-1):
    turn_nums[i] = west[i-1] + east[N-1] - east[i]

print(min(turn_nums))

Submission status: Accepted

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

left = [0]*n
for i,c in enumerate(s):
    if i == n-1:
        continue
    left[i+1] = left[i] + ( 1 if c == "W" else 0 )

right = [0] * n
for i,c in reversed(list(enumerate(s))):
    if i == 0:
        continue
    right[i-1] = right[i] + ( 1 if c == "E" else 0 )

min_cost=99999999999999999999

for i in range(n):
    min_cost = min(min_cost, left[i] + right[i])

print(min_cost)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    return

#B
def B():
    return

#C
def C():
    n = II()
    s = S()
    dp = [0 for i in range(n)]
    for num,si in enumerate(s):
        if si == "E":
            dp[num] = dp[num-1]+1
        else:
            dp[num] = dp[num - 1]
    ans = float("INF")
    a = max(dp)
    for i in range(n):
        ans = min(ans,  a - dp[i] + i - dp[i] + (s[i] == "E"))
    
    print(ans)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

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

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

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()
S="E"+S+"W"
E=[0]*(N+2)
W=[0]*(N+2)
for i in range(len(S)-1):
    if S[i]=="W":
        E[i+1]=E[i]+1
    else:
        E[i+1]=E[i]

for i in range(len(S)-1,0,-1):
    if S[i]=="E":
         W[i-1]=W[i]+1
    else:
         W[i-1]=W[i]
ans=10**9
for i in range(N):
    ans=min(ans,E[i+1]+W[i+1])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N = int(input())
S = input()
Esum = [0]
tmp = 0
for s in S:
    if s == 'E':
        tmp += 1
    Esum.append(tmp)


E = Esum[-1]
ans = INF
for i in range(N):
    tmp = 0
    leftW = i - Esum[i]
    rightW = E - Esum[i+1]
    tmp = leftW + rightW
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
def main():
    num = int(input())
    data = list(input())
    soppo = data.count('E')

    ans = num

    for i in range(num):
        now_person = data[i]
        kari = soppo
        if now_person == 'E':
            kari -= 1

        if kari < ans:
            ans = kari

        if now_person == 'E':
            soppo -= 1
        else:
            soppo += 1

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
#print(N,S)

# reader position 0
leftSide = 0
rigthSide = S[1:].count("E")

#print(rigthSide)

minChange = rigthSide
crntCnt = rigthSide
for i in range(1, len(S)):
  if S[i-1] == "W":
    crntCnt += 1
  if S[i] == "E":
    crntCnt -= 1
  if crntCnt < minChange:
    minChange = crntCnt
print(minChange)

Submission status: Accepted

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

Submission status: Accepted

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

cnt_E = [0] * (N+2)
cnt_W = [0] * (N+2)

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

ans = 10**7
for i in range(N):
    total = cnt_W[i] + cnt_E[i+2]
    if ans > total:
        ans = total
        
print(ans)

Submission status: Accepted

Solution code:


def read_input():
    n = int(input())
    s = input()

    return n, s

def submit():
    n, s = read_input()

    # [count(w), count(e)]
    westside_count = [0, 0]
    eastside_count = [len([c for c in s if c == 'W']), len([c for c in s if c == 'E'])]
    min_flip = n
    for i in range(n):
        leader = s[i]

        # eastsideの人数を更新
        if leader == 'W':
            eastside_count[0] -= 1
        else:
            eastside_count[1] -= 1

        # 振り返り人数を数える
        flip = westside_count[0] + eastside_count[1]
        if flip < min_flip:
            min_flip = flip

        # westsideを更新
        if leader == 'W':
            westside_count[0] += 1
        else:
            westside_count[1] += 1

    print(min_flip)

if __name__ == '__main__':
    submit()

Submission status: Accepted

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

ruiseki_w = [0]
ruiseki = 0
for i in s[:-1]:
    if i == 'W':
        ruiseki += 1
        ruiseki_w.append(ruiseki)
    else:
        ruiseki_w.append(ruiseki)

ruiseki_e = [0]
ruiseki = 0
for i in s[::-1][:-1]:
    if i == 'E':
        ruiseki += 1
        ruiseki_e.append(ruiseki)
    else:
        ruiseki_e.append(ruiseki)
ruiseki_e.reverse()
        
ninzu = [ruiseki_w[i] + ruiseki_e[i] for i in range(n)]
print(min(ninzu))

Submission status: Accepted

Solution code:
def solve():
    N = int(raw_input())
    S = raw_input() # str
    count = 0
    for i in range(1, N): # count for leader is the 1st person case
        if S[i] == 'E':
            count += 1
    min_need_change = count
    for i in range(1,N): # count for leader is the 2nd, ... (N + 1)th person cases
        if S[i] == 'E':
            count -= 1
        if S[i - 1] == 'W':
            count += 1
        if count < min_need_change:
            min_need_change = count
    print str(min_need_change)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

ruisekiwa = [0]
ans = n

for i in s:
    if i == "W":
        ruisekiwa.append(ruisekiwa[-1]+1)
    else:
        ruisekiwa.append(ruisekiwa[-1])
        
for i in range(1,n+1):
    w = ruisekiwa[i-1]
    e = n-i-(ruisekiwa[-1]-ruisekiwa[i])
    ans = min(ans,w+e)
    
print(ans)