Remix.run Logo
ww520 2 hours ago

This is an interesting read.

You can solve the same problem with Range Min Query Tree. The query for balanced or unbalanced parentheses is O(log N). A two or three level RMQTree can represent billions of parentheses already (a two-level tree of 65536 branch factor = 4B parentheses). The query is O(65536 + 65536) or effectively O(1). For a four-level tree of 256 branch factor, the query is O(256 + 256 + 256 + 256) or O(1). It becomes a problem of memory access on the number of levels vs the number of entries to process per level. A total = 0 and min > 0 mean the parentheses are balanced.

The parentheses can be broken up into multiple segments, and be processed in parallel. Multiple RMQTrees can be used, one per segment, and the trees can be processed in parallel. Need to have a final pass to sum up the 'total' and 'min' from all the RMQTrees. Parallel processing probably doesn't gain much.

anderskaseorg 34 minutes ago | parent [-]

You have ignored the much larger space and time cost of constructing the range minimum query tree in the first place. Furthermore, nobody would arbitrarily cut off a problem at “billions” and then consider O(√billions) or O(∜billions) to be “effectively O(1)”: a theoretician would complain that O() by definition measures the limiting behavior as the problem size tends to ∞, an engineer would complain that 65536 or 256 is very a important factor in practice, and both types are better served by leaving O(√n) or O(∜n) as-is instead of trying to hand-wave it away.