We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Reverse prefix Sum for each array, pop elements until last prefix sum values are equal
defequalStacks(h1,h2,h3):# Write your code herepre_fix_h1=[]pre_fix_h2=[]pre_fix_h3=[]deffill_pre_fix(pre_fix_arr,h):pSum=0forninreversed(h):pSum+=npre_fix_arr.append(pSum)fill_pre_fix(pre_fix_h1,h1)fill_pre_fix(pre_fix_h2,h2)fill_pre_fix(pre_fix_h3,h3)whilepre_fix_h1andpre_fix_h2andpre_fix_h3:ifpre_fix_h1[-1]==pre_fix_h2[-1]andpre_fix_h2[-1]==pre_fix_h3[-1]:returnpre_fix_h1[-1]# find max and remove itmx=max(pre_fix_h1[-1],pre_fix_h2[-1],pre_fix_h3[-1])ifpre_fix_h1[-1]==mx:pre_fix_h1.pop()elifpre_fix_h2[-1]==mx:pre_fix_h2.pop()else:pre_fix_h3.pop()return0
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Reverse prefix Sum for each array, pop elements until last prefix sum values are equal