https://school.programmers.co.kr/learn/courses/30/lessons/120840
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
나의 풀이
class Solution {
public int solution(int balls, int share) {
return combination(balls, share);
}
public static int combination(int balls, int share) {
if (balls == share || share == 0) return 1;
return combination((balls - 1), (share - 1)) + combination(balls - 1, share);
}
}
다른 풀이
import java.math.BigInteger;
class Solution {
public BigInteger solution(int balls, int share) {
return factorial(balls).divide(factorial(balls - share).multiply(factorial(share)));
}
public BigInteger factorial(int n) {
BigInteger result = new BigInteger("1");
BigInteger from = new BigInteger("1");
BigInteger to = new BigInteger(String.valueOf(n));
for (BigInteger i = from; i.compareTo(to) <= 0; i = i.add(BigInteger.ONE)) {
result = result.multiply(i);
}
return result;
}
}
class Solution {
public long solution(int balls, int share) {
share = Math.min(balls - share, share);
if (share == 0)
return 1;
long result = solution(balls - 1, share - 1);
result *= balls;
result /= share;
return result;
}
}