-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathequal-stacks.py
39 lines (30 loc) · 928 Bytes
/
equal-stacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Data Structures > Stacks > Equal Stacks
# Equalize the piles!
#
# https://www.hackerrank.com/challenges/equal-stacks/problem
#
# principe:
# on calcule la hauteur cumulée
# on part de la stack qui contient le moins d'éléments
# et on la dépile jusqu'à ce que la valeur soit présente dans toutes les autres stacks
import itertools
def get_stack():
s = list(itertools.accumulate(reversed(list(map(int, input().split())))))
return (len(s), s)
def max_height(oh):
oh = list(s[1] for s in oh)
while oh[0]:
h = oh[0].pop()
ok = True
for i in range(1, len(oh)):
while oh[i] and oh[i][-1] > h:
oh[i].pop()
if not oh[i]:
return 0
ok = ok and oh[i][-1] == h
if ok:
return h
return 0
n1, n2, n3 = map(int, input().split())
oh = sorted([get_stack() for _ in range(3)])
print(max_height(oh))