Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db5b995

Browse files
authored
Create Distribute Candies Among Children II.py
1 parent 289e161 commit db5b995

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /