Many Scores Force Many Reads: An Optimality Proof for Score of Parentheses
Andrew Au
Abstract
LeetCode Problem 856, Score of Parentheses, admits a one-pass algorithm using O(n) time and O(1) 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 k, we construct 2k balanced parenthesis strings of the same length 4k+2, all having distinct scores. The construction encodes each bit using one of two operations, changing a current score v to either 2v+1 or 2v+2. Consequently, any deterministic character-query decision tree computing the score must have depth at least k. A two-character padding argument extends the result to every even input length, proving an Ω(n) worst-case time lower bound. The one-pass algorithm is therefore asymptotically optimal in the unit-cost RAM model.
Create a lesson
Related papers
Social Learning of Ranking: Getting the Top Right despite Local Herding
Shahrzad Haddadan, Jie Gao
Adaptive Sorting, also in the BST Model
Ivor van der Hoog, Eva Rotenberg, Daniel Rutschmann
Exact and Asymptotic Analysis of Greedy Online Facility Assignment on Cycle Metrics
Md. Tanzeem Rahat, Md. Rawha Siddiqi Riad, Md. Manzurul Hasan
Metric Planarity Testing is Hard
Anubhav Dhar, Debajyoti Kar, Sándor Kisfaludi-Bak et al.
Almost Lossless Transfer for Cuttable Distributed Problems
Wojciech Wawrzyniak
Approximation Algorithms for Dual-Memory Request Scheduling in Disaggregated LLM Inference
Xin Wang, Hong Shen, Hui Tian et al.