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 96489b1

Browse files
Create Distribute Candies Among Children II.java
1 parent 52620f4 commit 96489b1

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public long distributeCandies(int n, int limit) {
3+
long result = 0;
4+
for (int i = 0; i <= Math.min(limit, n); i++) {
5+
// distribution not possible as we will have to allocate at least one child with
6+
// more than limit candies
7+
if (n - i > 2 * limit) {
8+
continue;
9+
}
10+
result += Math.min(n - i, limit) - Math.max(0, n - i - limit) + 1;
11+
}
12+
return result;
13+
}
14+
}

0 commit comments

Comments
(0)

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