Skip to content

Instantly share code, notes, and snippets.

@cfc1020
Created June 12, 2020 18:22
Show Gist options
  • Select an option

  • Save cfc1020/f95bfbb21754b34deaaf49595c665f6f to your computer and use it in GitHub Desktop.

Select an option

Save cfc1020/f95bfbb21754b34deaaf49595c665f6f to your computer and use it in GitHub Desktop.
Minimum Add to Make Parentheses Valid
# @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