사용언어 : Java
문제

풀이
/*
n = 3 m = 2
n! / (n - m)! * m!
3! / (3 - 2)! * 2!
3! / 1! * 2!
3 * 2 * 1
----------
1 * 2 * 1
=====================
n = 5 m = 3
5! / (5 - 3)! * 3!
5 * 4 * 3 * 2 * 1
------------------
2 * 1 * 3 * 2 * 1
120 / 12 = 10
*/
import java.math.BigInteger;
class Solution {
public int solution(int balls, int share) {
// nCk에서 k가 더 작은 쪽을 쓰면 연산량/크기 감소
int n = balls;
int k = Math.min(share, balls - share);
BigInteger result = BigInteger.ONE;
for (int i = 1; i <= k; i++) {
result = result.multiply(BigInteger.valueOf(n - k + i))
.divide(BigInteger.valueOf(i));
}
return result.intValue();
}
}
Share article