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.
I use a map for elements index and sums and increment index on the list with the lowest sum each loop
time O(n)
space O(1)
defequalStacks(h1,h2,h3):# Write your code heremax_val=0index={1:-1,2:-1,3:-1}sums={1:0,2:0,3:0}h1.reverse()h2.reverse()h3.reverse()list_map={1:h1,2:h2,3:h3}whileTrue:ifnotindex:breaknext_index_sum=min([vfork,vinsums.items()ifkinindex])list_key={v:kfork,vinsums.items()ifkinindex}[next_index_sum]next_block_index=index[list_key]+1ifnext_block_index<=list_map[list_key].__len__()-1:sums[list_key]+=list_map[list_key][next_block_index]index[list_key]+=1else:delindex[list_key]iflen(set(sums.values()))==1:max_val=max(max_val,sums[1])returnmax_val
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 →
I use a map for elements index and sums and increment index on the list with the lowest sum each loop
time O(n) space O(1)