Created
June 1, 2020 00:36
-
-
Save Shaddyjr/a07f395cf3eac5b62c2517177f216c8b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I would remove the validation at lines 15 and 25 and place it into the while condition like :
while (not allSame(stacks)) { ... }