2929. Distribute Candies Among Children II Medium
1/**
2 * [2929] Distribute Candies Among Children II
3 *
4 * You are given two positive integers n and limit.
5 * Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
6 *
7 * <strong class="example">Example 1:
8 *
9 * Input: n = 5, limit = 2
10 * Output: 3
11 * Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
12 *
13 * <strong class="example">Example 2:
14 *
15 * Input: n = 3, limit = 3
16 * Output: 10
17 * Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
18 *
19 *
20 * Constraints:
21 *
22 * 1 <= n <= 10^6
23 * 1 <= limit <= 10^6
24 *
25 */
26pub struct Solution {}
27
28// problem: https://leetcode.com/problems/distribute-candies-among-children-ii/
29// discuss: https://leetcode.com/problems/distribute-candies-among-children-ii/discuss/?currentPage=1&orderBy=most_votes&query=
30
31// submission codes start here
32
33impl Solution {
34 pub fn distribute_candies(n: i32, limit: i32) -> i64 {
35
36 }
37}
38
39// submission codes end
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn test_2929() {
47 }
48}
49
Back
© 2025 bowen.ge All Rights Reserved.