구슬을 나누는 경우의 수

han.user();·2023년 4월 6일
0

프로그래머스

목록 보기
43/87
post-thumbnail

class Solution {
    public int solution(int balls, int share) {
        if (balls < share) {
            return 0;
        }
        if (balls == share || share == 0) {
            return 1;
        }
        int[][] pascal = new int[balls + 1][share + 1];
        for (int i = 0; i <= balls; i++) {
            pascal[i][0] = 1;
        }
        for (int i = 1; i <= balls; i++) {
            for (int j = 1; j <= Math.min(i, share); j++) {
                pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
            }
        }
        return pascal[balls][share];
    }
}
profile
I'm still hungry.

0개의 댓글