Algorithm, Problem Solving/프로그래머스
[codeforces][Kotlin] 구슬을 나누는 경우의 수
태오님
2023. 4. 1. 20:46
목차
문제 정보
https://school.programmers.co.kr/learn/courses/30/lessons/120840
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
난이도 : Lv0
유형 : 조합론
문제 풀이
조합 공식을 활용한 재귀식을 통해 풀이를 했다.
nCr = n-1Cr-1 + n-1Cr
코드
class Solution {
fun combination(n: Int, r: Int): Int {
if (n == r || r == 0) {
return 1
}
return combination(n - 1, r - 1) + combination(n - 1, r)
}
fun solution(balls: Int, share: Int): Int {
return combination(balls, share)
}
}