Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. /** Runtime: 1 ms, faster than 46.42% of Java online submissions for Minimum Add to Make Parentheses Valid.
  2. Memory Usage: 34.5 MB, less than 100.00% of Java online submissions for Minimum Add to Make Parentheses Valid.**/
  3. class Solution {
  4.     public int minAddToMakeValid(String S) {
  5.         char[] arr = S.toCharArray();
  6.         Queue<Character> queue = new LinkedList<>();
  7.        
  8.         int count = 0;
  9.        
  10.         for(char c : arr) {
  11.             if(c == '(') {
  12.                 queue.add(c);
  13.             }
  14.            
  15.             if(c == ')') {
  16.                if(queue.isEmpty()) {
  17.                    count++;
  18.                } else {
  19.                    queue.poll();
  20.                }
  21.             }
  22.         }
  23.        
  24.         return count + queue.size();
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement