Skip to content

Commit d33f3fb

Browse files
committed
Sync LeetCode submission Runtime - 4656 ms (9.59%), Memory - 18 MB (15.75%)
1 parent ad1b3c6 commit d33f3fb

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p>
2+
3+
<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> n = 5, limit = 2
10+
<strong>Output:</strong> 3
11+
<strong>Explanation:</strong> 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+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> n = 3, limit = 3
18+
<strong>Output:</strong> 10
19+
<strong>Explanation:</strong> 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).
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>1 &lt;= n &lt;= 10<sup>6</sup></code></li>
27+
<li><code>1 &lt;= limit &lt;= 10<sup>6</sup></code></li>
28+
</ul>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Approach 1: Enumeration
2+
3+
# Time: O(min(limit, n))
4+
# Space: O(1)
5+
6+
class Solution:
7+
def distributeCandies(self, n: int, limit: int) -> int:
8+
ans = 0
9+
for i in range(min(limit, n) + 1):
10+
if n - i > 2 * limit:
11+
continue
12+
ans += min(n - i, limit) - max(0, n - i - limit) + 1
13+
return ans
14+

0 commit comments

Comments
 (0)