Solve ABC098-C in Python

Introduction

The person from PyBegi solved it, so I'll piggyback on it.

ABC098-C Attention

** Thoughts ** Due to the constraints of the problem, it must be stored at the maximum of about $ O (N) $, so it is not enough to calculate for each leader. Therefore, we use the cumulative sum. Now you can calculate for each reader without having to $ sum $ each time. By the way, the computational complexity of $ sum $ in Python is $ O (N) $. If you understand this far, just do it.

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

count_e = [0] * n
count_w = [0] * n
for i in range(n):
    if i == 0:
        if s[i] == 'E':
            count_e[i] = 1
        else:
            count_w[i] = 1
        continue
    if s[i] == 'E':
        count_e[i] = count_e[i-1] + 1
        count_w[i] = count_w[i-1]
    else:
        count_e[i] = count_e[i-1]
        count_w[i] = count_w[i-1] + 1

ans = 10 ** 9 #e+w is at most 3*10**It's about 5, but I have a margin
for i in range(n):
    if s[i] == 'E':
        e = count_e[-1] - count_e[i] #Use cumulative sum
        w = count_w[i]
    else:
        e = count_e[-1] - count_e[i] #Use cumulative sum
        w = count_w[i] - 1 #Subtract your share
    ans = min(ans,e+w)
print(ans)

Summary

I'm glad that I've come up with algorithms that can be used by looking at the constraints. See you again, good night.

Recommended Posts

Solve ABC098-C in Python
Solve ABC168D in Python
Solve ABC167-D in Python
Solve ABC159-D in Python
Solve ABC169 in Python
Solve ABC160-E in Python
Solve ABC176 E in Python
Solve Wooldridge exercises in Python
Solve ABC175 D in Python
Solve optimization problems in Python
Solve Atcoder ABC169 A-D in Python
Solve ABC036 A ~ C in Python
Solve ABC037 A ~ C in Python
Solve ordinary differential equations in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Solve ABC175 A, B, C in Python
ABC 157 D --Solve Friend Suggestions in Python!
I wanted to solve ABC159 in Python
Solve ABC165 A, B, D in Python
Solve the maximum subarray problem in Python
Sorted list in Python