Created
June 12, 2020 18:22
-
-
Save cfc1020/f95bfbb21754b34deaaf49595c665f6f to your computer and use it in GitHub Desktop.
Minimum Add to Make Parentheses Valid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # @param {String} s | |
| # @return {Integer} | |
| def min_add_to_make_valid(s) | |
| res = 0 | |
| balance = 0 | |
| s.each_char do |c| | |
| balance += c == '(' ? 1 : -1 | |
| if balance < 0 | |
| res += 1 | |
| balance += 1 | |
| end | |
| end | |
| res + balance | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment