[코테 풀이] Distribute Candies Among Children I

시내·2024년 6월 28일

Q_2928) Distribute Candies Among Children I

출처 : https://leetcode.com/problems/distribute-candies-among-children-i/

You are given two positive integers n and limit.

Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.

class Solution {
    public int distributeCandies(int n, int limit) {
        int count = 0;
        for (int a = 0; a <= limit; a++) {
            int total = n;
            total -= a;
            for (int b = 0; b <= limit; b++) {
                total -= b;
                for (int c = 0; c <= limit; c++) {
                    total -= c;
                    if (total == 0) {
                        count++;
                    }
                    total += c;
                }
                total += b;
            }
        }
        return count;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글