Buggy python submission (verdict: Time Limit Exceeded):

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

print(min([len([x for x in s[:i] if x == 'W']) + \
           len([x for x in s[i+1:] if x == 'E']) \
           for i in range(n)]))


Fixed submission:
n = int(input())
s = input()

turn_east = [0 for x in range(n)]
turn_west = [0 for x in range(n)]
total = [0 for x in range(n)]

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

    if s[n-i] == 'E':
      turn_west[n-1-i] = turn_west[n-i]+1
    else:
      turn_west[n-1-i] = turn_west[n-i]

for j in range(n):
  total[j] = turn_east[j] + turn_west[j]

print(min(total))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

mem_left_len = [len([x for x in s[:i] if x == 'W']) for i in range(n)]
mem_right_len = [len([x for x in s[i+1:] if x == 'E']) for i in range(n)]
  
numpeople = 3 * 10 ** 5 + 1
for leader in range(n):
  numpeople = min(numpeople, mem_left_len[leader] + mem_right_len[leader])

print(numpeople)

Fixed submission:
n = int(input())
s = input()

turn_east = [0 for x in range(n)]
turn_west = [0 for x in range(n)]
total = [0 for x in range(n)]

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

    if s[n-i] == 'E':
      turn_west[n-1-i] = turn_west[n-i]+1
    else:
      turn_west[n-1-i] = turn_west[n-i]

for j in range(n):
  total[j] = turn_east[j] + turn_west[j]

print(min(total))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

mem_left_len = [-1 for i in range(n)]
mem_right_len = [-1 for i in range(n)]

def len_left(leader):
  length = mem_left_len[leader]
  if length > 0:
    return length
  else:
    length = len([x for x in s[:leader] if x == 'W'])
    mem_left_len[leader] = length
    return length

def len_right(leader):
  length = mem_right_len[leader]
  if length > 0:
    return length
  else:
    length = len([x for x in s[leader+1:] if x == 'E'])
    mem_right_len[leader] = length
    return length
  
  
numpeople = 3 * 10 ** 5 + 1
for leader in range(n):
  left = len_left(leader)
  right = len_right(leader)
  numpeople = min(numpeople, left + right)

print(numpeople)

Fixed submission:
n = int(input())
s = input()

turn_east = [0 for x in range(n)]
turn_west = [0 for x in range(n)]
total = [0 for x in range(n)]

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

    if s[n-i] == 'E':
      turn_west[n-1-i] = turn_west[n-i]+1
    else:
      turn_west[n-1-i] = turn_west[n-i]

for j in range(n):
  total[j] = turn_east[j] + turn_west[j]

print(min(total))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

numpeople = 3 * 10 ** 5 + 1
for leader in range(n):
  left = len([x for x in s[:leader] if x == 'W'])
  right = len([x for x in s[leader+1:] if x == 'E'])
  numpeople = min(numpeople, left + right)

print(numpeople)

Fixed submission:
n = int(input())
s = input()

turn_east = [0 for x in range(n)]
turn_west = [0 for x in range(n)]
total = [0 for x in range(n)]

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

    if s[n-i] == 'E':
      turn_west[n-1-i] = turn_west[n-i]+1
    else:
      turn_west[n-1-i] = turn_west[n-i]

for j in range(n):
  total[j] = turn_east[j] + turn_west[j]

print(min(total))