The problem is from codeforces: http://www.codeforces.com/problemset/problem/245/E
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) —
Last Post: A Faster Exponentiation Algorithm by Squaring (Power Function)
Next Post: Fermat Prime Test Algorithm