Whenever you're ready, walk me through how you'd approach this.
Explain the Big-O time and space complexity of your proposed solution and discuss how you would optimize it for memory-constrained environments.
Be explicit about which operations and data structures determine time and auxiliary space. Explain the trade-offs of any optimization, including whether it changes runtime, readability, correctness, or implementation complexity. We are evaluating how clearly you reason about resource usage and how you respond when constraints require revisiting an initial approach.