Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created June 1, 2020 00:36
Show Gist options
  • Save Shaddyjr/a07f395cf3eac5b62c2517177f216c8b to your computer and use it in GitHub Desktop.
Save Shaddyjr/a07f395cf3eac5b62c2517177f216c8b to your computer and use it in GitHub Desktop.
# Source - https://www.hackerrank.com/challenges/equal-stacks/problem
def allSame(arr):
return arr[0]["total"] == arr[1]["total"] == arr[2]["total"]
def equalStacks(h1, h2, h3):
if(len(h1) == 0 or len(h2) == 0 or len(h3) == 0):
return 0;
stacks = [
{"arr": h1, "total":sum(h1)},
{"arr": h2, "total":sum(h2)},
{"arr": h3, "total":sum(h3)},
]
if(allSame(stacks)):
return stacks[0]["total"]
while(True):
highest = stacks[0]
for stack in stacks:
if(stack["total"] > highest["total"]):
highest = stack
highest["total"] -= highest["arr"].pop(0)
if(allSame(stacks)):
return stacks[0]["total"]
return stacks[0]["total"]
@rafareyes7
Copy link

rafareyes7 commented Jun 11, 2020

Hi, I would remove the validation at lines 15 and 25 and place it into the while condition like : while (not allSame(stacks)) { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment