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.
Not a python coder, but it looks like you're re-calculating the sum of the elements in the array each time through the loop. This is very computationally expensive when the arrays are very large (some of the later test cases have nearly a hundred thousand elements). Instead, store the heights in variables the first time through, and subtract the element at index 0 from them before popping the element. This is much cheaper, and should solve the problem.
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Not a python coder, but it looks like you're re-calculating the sum of the elements in the array each time through the loop. This is very computationally expensive when the arrays are very large (some of the later test cases have nearly a hundred thousand elements). Instead, store the heights in variables the first time through, and subtract the element at index 0 from them before popping the element. This is much cheaper, and should solve the problem.