티스토리 뷰

https://www.acmicpc.net/problem/11899


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String input = br.readLine();
        System.out.println(getMinBracket(input));
    }

    static int getMinBracket(String str) {
        Stack<Character> open = new Stack<>();
        Stack<Character> close = new Stack<>();

        int cnt = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == ')') {
                if (!open.isEmpty()) {
                    open.pop();
                } else {
                    close.push(c);
                }
            } else {
                if (!close.isEmpty()) {
                    cnt += close.size();
                    close.clear();
                }
                open.push(c);
            }
        }

        cnt += open.size() + close.size();

        return cnt;
    }
}
Total
Today
Yesterday
최근에 올라온 글
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30