I think a stack-based solution is the most efficient and readable method, to be fair...
If we think about the complexity it should be around O(n)
(n (to compute first) + n (to compute second) + n(to go once over both))
Which is ofc not as good as the O(log(n)), but I am not sure there's a way to cut times.
But... to be 100% correct, in order to determine if this is helpful or just added time for our average case we would need to know the context a bit more in depth.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think a stack-based solution is the most efficient and readable method, to be fair...
If we think about the complexity it should be around O(n)
(n (to compute first) + n (to compute second) + n(to go once over both))
Which is ofc not as good as the O(log(n)), but I am not sure there's a way to cut times.
Also we can do a sneaky trick such as
But... to be 100% correct, in order to determine if this is helpful or just added time for our average case we would need to know the context a bit more in depth.