Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Counting
- hash table
- dynamic programming
- java
- Method
- bit manipulation
- 구현
- string
- Binary Search
- two pointers
- 파이썬
- Data Structure
- Class
- Binary Tree
- Tree
- Math
- Number Theory
- 자바
- SQL
- implement
- 코딩테스트
- Stack
- Matrix
- greedy
- sorting
- array
- database
- simulation
- geometry
- 코테
Archives
- Today
- Total
코린이의 소소한 공부노트
Binomial Coefficient (iterative) 본문
1. Problem
- nCk를 계산해보자.
2. Input
1) 음이 아닌 정수 n
2) 음이 아닌 정수 k
- 이때 k <= n
3. Output
1) nCk의 값
- C는 수학에서 조합을 나타내는 기호로, nCk는 n!/(k!(n-k)!)으로 계산된다.
4. PseudoCode
int bin2(int n, int k){
index i, j;
int B[0..n][0..k]
for(i=0 ; i<=n ; i++){
for(j=0 ; j<=minimum(i,k) ; j++){
if(j==0 || j==i)
B[i][j] = 1;
else
B[i][j] = B[i-1][j-1] + B[i-1][j];
}
}
return B[n][k];
}
5. Example
class AlgoTest {
public static void main(String[] args){
System.out.println(bin2(5,3)); // 10
}
static long bin2(int n, int k) {
int[][] B = new int[n+1][k+1];
for(int i=0 ; i<=n ; i++) {
for(int j=0 ; j<=Math.min(i, k) ; j++) {
if(j==0 || j==i)
B[i][j] = 1;
else
B[i][j] = B[i-1][j-1] + B[i-1][j];
}
}
return B[n][k];
}
}
'Back-End > Algorithm' 카테고리의 다른 글
Search Binary Tree (no example) (0) | 2023.03.08 |
---|---|
Floyd's Algorithm for shortest paths (0) | 2023.03.07 |
Binomial Coefficient (recursive) (0) | 2023.03.07 |
Dynamic Programming (0) | 2023.03.07 |
Strassen's Matrix Multiplication Algorithm (no example) (0) | 2023.03.03 |