Algorithms, Blockchain and Cloud

Codeforces: E. Mishap in Club


The problem is from codeforces: http://www.codeforces.com/problemset/problem/245/E
245E Codeforces: E. Mishap in Club codeforces implementation programming languages python

The minimal number of people can be obtained by greedy approach, assuming that at each possible cases, the same people entered and left the club. Therefore, we can count the final answer by how many entered and left distinctly.

#!/usr/bin/env python

p, m = 0, 0
for _ in raw_input():
    if _ == '+':
        p += 1
        if m:
            m -= 1
    else:
        m += 1
        if p:
            p -= 1
print p + m

Or we can count the maximum and minimal number of people in the club. The difference gives the answer.

st = raw_input().strip()
cur,mincur,maxcur = 0,0,0
for i in range(len(st)):
    if st[i]=='+':
        cur +=1
        maxcur = max(maxcur,cur)
        continue
    cur-=1
    mincur = min(mincur,cur)
print maxcur-mincur

–EOF (The Ultimate Computing & Technology Blog) —

192 words
Last Post: A Faster Exponentiation Algorithm by Squaring (Power Function)
Next Post: Fermat Prime Test Algorithm

The Permanent URL is: Codeforces: E. Mishap in Club (AMP Version)

Exit mobile version