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've noticed that the timers on Hackerrank aren't that kind to Python, but you can get around it.
You're problem is in the summing lines. It's O(n2), which gets insane with the large datasets. Instead of summing up everything remaining in the array every time you iterate through it, calculate the total value once (and keep track of it), then, as you iterate through the array, remove the previous value from the total value. This way, you can keep track of the sum at each level, without having to sum up the array N times. Here's my function
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
I've noticed that the timers on Hackerrank aren't that kind to Python, but you can get around it.
You're problem is in the summing lines. It's O(n2), which gets insane with the large datasets. Instead of summing up everything remaining in the array every time you iterate through it, calculate the total value once (and keep track of it), then, as you iterate through the array, remove the previous value from the total value. This way, you can keep track of the sum at each level, without having to sum up the array N times. Here's my function