|
| 1 | +You are given two positive integers n and limit. |
| 2 | + |
| 3 | +Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies. |
| 4 | + |
| 5 | + ------------------------------------------------------------------- |
| 6 | + |
| 7 | + class Solution: |
| 8 | + def distributeCandies(self, n: int, limit: int) -> int: |
| 9 | + |
| 10 | + res = 0 |
| 11 | + |
| 12 | + for i in range(limit+1): |
| 13 | + cur=0 |
| 14 | + for j in range(n): |
| 15 | + cur+=1 |
| 16 | + if j>=i: |
| 17 | + cur = 0 |
| 18 | + res+=cur |
| 19 | + return res |
| 20 | + |
| 21 | + |
| 22 | +--------------------------------- |
| 23 | +class Solution: |
| 24 | + def distributeCandies(self, n: int, limit: int) -> int: |
| 25 | + def H3(n): |
| 26 | + return 0 if n<0 else (n+2)*(n+1)//2 |
| 27 | + return H3(n)-3*H3(n-limit-1)+3*H3(n-2*(limit+1))-H3(n-3*(limit+1)) |
| 28 | + |
0 commit comments