Many Scores Force Many Reads: An Optimality Proof for Score of Parentheses
arXiv:2610.14213
Abstract
LeetCode Problem 856, Score of Parentheses, admits a one-pass algorithm using time and auxiliary registers. Its correctness follows from a local interpretation: every occurrence of () contributes a power of two determined by its nesting depth. The space bound is immediately optimal in the usual programming-challenge model, but the linear-time bound is less obvious. In particular, a direct adversary that forces an algorithm to inspect every character is difficult to maintain under the promise that the input is balanced. We instead prove a counting lower bound. For every , we construct balanced parenthesis strings of the same length , all having distinct scores. The construction encodes each bit using one of two operations, changing a current score to either or . Consequently, any deterministic character-query decision tree computing the score must have depth at least . A two-character padding argument extends the result to every even input length, proving an worst-case time lower bound. The one-pass algorithm is therefore asymptotically optimal in the unit-cost RAM model.