Skip to content

Instantly share code, notes, and snippets.

@arishal
Created July 12, 2018 21:03
Show Gist options
  • Select an option

  • Save arishal/0d72e5c134aef77a22d8f7f3e62f26c8 to your computer and use it in GitHub Desktop.

Select an option

Save arishal/0d72e5c134aef77a22d8f7f3e62f26c8 to your computer and use it in GitHub Desktop.
# https://www.hackerrank.com/challenges/equal-stacks/problem
# You have three stacks of cylinders where each cylinder has the same diameter, but they may
# vary in height. You can change the height of a stack by removing and discarding its topmost
# cylinder any number of times.
# Find the maximum possible height of the stacks such that all of the stacks are exactly the
# same height. This means you must remove zero or more cylinders from the top of zero or more
# of the three stacks until they're all the same height, then print the height. The removals
# must be performed in such a way as to maximize the height.
# Note: An empty stack is still a stack.
# INPUT FORMAT:
# The first line contains three space-separated integers, n1, n2, and n3, describing the
# respective number of cylinders in stacks 1, 2, and 3. The subsequent lines describe the
# respective heights of each cylinder in a stack from top to bottom:
# * The second line contains n1 space-separated integers describing the cylinder heights
# in stack 1. The first element is the top of the stack.
# * The third line contains n2 space-separated integers describing the cylinder heights
# in stack 2. The first element is the top of the stack.
# * The fourth line contains n3 space-separated integers describing the cylinder heights
# in stack 3. The first element is the top of the stack.
def cumsum_from_input():
a = [0]
tmp = [int(i) for i in input().split()]
for i in range(len(tmp)):
a.append(a[-1] + tmp[-i-1])
return a
input()
s = [cumsum_from_input(), cumsum_from_input(), cumsum_from_input()]
t = False
while True:
if not t:
vals = [s[0].pop(), s[1].pop(), s[2].pop()]
if vals[0] == vals[1] == vals[2]:
print(vals[0])
break
else:
loc = vals.index(max(vals))
vals[loc] = s[loc].pop()
t = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment