Skip to content

Commit 2679598

Browse files
committed
Time: 31 ms (26.47%), Space: 9.1 MB (18.82%) - LeetHub
1 parent 1db1c98 commit 2679598

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
long long distributeCandies(int n, int limit) {
4+
int a = max(0, n - 2 * limit);
5+
int b = min(n, limit);
6+
long long ans = 0;
7+
while (a <= b) {
8+
int rem = n - a;
9+
int mn = max(0, rem - limit);
10+
int mx = min(rem, limit);
11+
ans += mx - mn + 1;
12+
++a;
13+
}
14+
return ans;
15+
}
16+
};

0 commit comments

Comments
 (0)