Given a parentheses string s containing only the characters '(' and ')'. A parentheses string is balanced if:


	Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'.
	Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.


In other words, we treat '(' as an opening parenthesis and '))' as a closing parenthesis.


	For example, "())", "())(())))" and "(())())))" are balanced, ")()", "()))" and "(()))" are not balanced.


You can insert the characters '(' and ')' at any position of the string to balance it if needed.

Return the minimum number of insertions needed to make s balanced.

&nbsp;
Example 1:

Input: s = "(()))"
Output: 1
Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need to add one more ')' at the end of the string to be "(())))" which is balanced.


Example 2:

Input: s = "())"
Output: 0
Explanation: The string is already balanced.


Example 3:

Input: s = "))())("
Output: 3
Explanation: Add '(' to match the first '))', Add '))' to match the last '('.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 105
	s consists of '(' and ')' only.


class Solution:
    def minInsertions(self, s: str) -> int:
        leftbrackets = insertions = 0
        i, n = 0, len(s)

        while i < n:
            if s[i] == '(':
                leftbrackets += 1
            elif s[i] == ')':
                if i == n-1 or s[i+1] != ')': insertions += 1
                else: i += 1
                    
                if not leftbrackets: insertions += 1
                else: leftbrackets -= 1
                    
            i += 1
        
        return leftbrackets * 2 + insertions
class Solution {
    public int minInsertions(String s) {
        int open=0;
        int ans=0;

        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='('){
                open++;
            }
            else{
                if(i+1<s.length() && s.charAt(i+1)==')'){
                    i++;
                    if(open>0){
                        open--;
                    }
                    else{
                        ans++;
                    }
                }
                else{
                    if(open>0){
                        open--;
                        ans++;
                    }
                    else{
                        ans+=2;
                    }
                }
            }
        }
        ans+=2*open;
        return ans;
    }
}
class Solution {
public:
    int minInsertions(string s) {
        stack<int> st;
        int n = s.size();
        int insert = 0;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '(')
            {
                if(st.empty())
                {
                    st.push(2);
                }
                else
                {
                    if(st.top() != 2)
                    {
                        st.pop();
                        insert++;
                    }
                    st.push(2);
                }
            }
            else
            {
                if(st.empty())
                {
                    insert++;
                    st.push(1);
                }
                else
                {
                    int dummy = st.top();
                    st.pop();
                    dummy--;
                    if(dummy)
                        st.push(dummy);
                }
            }
        }
        while(!st.empty())
        {
            insert += st.top();
            st.pop();
        }
        return insert;
    }
};
var minInsertions = function(s) {
    let rightNeeded = 0;
    let leftNeeded = 0;
    for (const char of s) {
        if (char === "(") {
            if (rightNeeded % 2 === 0) {
                rightNeeded += 2;  
            } else {                
                rightNeeded++;
                leftNeeded++;
            }
        } else {
            rightNeeded--;
            if (rightNeeded === -1 ){
                leftNeeded++;
                rightNeeded = 1;
            }
        }
    }
    return leftNeeded + rightNeeded;
};
Minimum Insertions to Balance a Parentheses String