String
1541. Minimum Insertions to Balance a Parentheses String
The asymmetry is the whole problem. A ( is worth two ), so I stopped thinking about matching brackets and started thinking about debt measured in units of ): every ( adds 2 to the debt, every ) pays 1 of it off. The answer is whatever I had to insert to keep that debt payable, plus whatever is still owed when the string runs out.
Loading…