File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed
Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+
4+ public class Main {
5+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
7+ private static boolean flag;
8+ private static int[] sum;
9+ private static char[] input;
10+
11+ public static void main(String[] args) throws IOException {
12+ init();
13+ int answer = solve(flag);
14+
15+ bw.write(answer + "\n");
16+ bw.flush();
17+ bw.close();
18+ br.close();
19+ }
20+
21+ private static void init() throws IOException {
22+ input = br.readLine().toCharArray();
23+ sum = new int[input.length+1];
24+ int a = 0;
25+ int b = 0;
26+
27+ for (int i = 0; i < input.length; i++) {
28+ if (input[i] == '(') a++;
29+ else b++;
30+ }
31+
32+ // true -> '('가 더 많은 경우
33+ if (a > b) flag = true;
34+ // false -> ')'가 더 많은 경우
35+ }
36+
37+ private static int solve(boolean flag) {
38+ int result = 0;
39+
40+ if (flag) {
41+ for (int i = 1; i <= input.length; i++) {
42+ if (input[input.length-i] == ')') sum[i] = sum[i-1]+1;
43+ else sum[i] = sum[i-1]-1;
44+ if (sum[i] == -1) {
45+ result = (i+1)/2;
46+ break;
47+ }
48+ }
49+ } else {
50+ for (int i = 1; i <= input.length; i++) {
51+ if (input[i-1] == '(') sum[i] = sum[i-1]+1;
52+ else sum[i] = sum[i-1]-1;
53+ if (sum[i] == -1) {
54+ result = (i+1)/2;
55+ break;
56+ }
57+ }
58+ }
59+
60+ return result;
61+ }
62+ }
63+ ```
You can’t perform that action at this time.
0 commit comments