알고리즘 문제/Programmers
올바른 괄호의 개수
parkit
2020. 6. 25. 23:28
728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/12929
카탈랑(또는 카탈란) 수이다.
f(n) = (2nCn) / (n+1)
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import java.io.*; import java.util.*; public class Solution { static BufferedReader br; static BufferedWriter bw; static int c(int n, int r) { if(r == 0 || r == n) { return 1; } else { return c(n - 1, r -1) + c(n - 1, r); } } static int solution(int n) { return c(2*n, n) / (n+1); } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; System.out.println(solution(3)); bw.write("\n"); bw.flush(); bw.close(); } } class pair { int first, second; pair(int a, int b) { this.first = a; this.second = b; } } class tuple { int first, second, third; tuple(int a, int b, int c) { this.first = a; this.second = b; this.third = c; } } class PQ implements Comparable<PQ> { int first, second; PQ(int f, int s) { this.first = f; this.second = s; } public int compareTo(PQ p) { if(this.first < p.first) { return -1; // 오름차순 } else if(this.first == p.first) { if(this.second < p.second) { return -1; } } return 1; // 이미 this.first가 더 큰 것이 됐으므로, 1로 해준다. // -1로 하면 결과가 이상하게 출력됨. } } | cs |
728x90
반응형