Skip to content

Commit 1ac1592

Browse files
solves #2591: Distribute Money to Maximum Children in java
1 parent 72d8cf6 commit 1ac1592

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@
807807
| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum) | [![Java](assets/java.png)](src/SplitWithMinimumSum.java) | |
808808
| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow) | [![Java](assets/java.png)](src/PassThePillow.java) | |
809809
| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range) | [![Java](assets/java.png)](src/CountTheNumberOfVowelStringsInRange.java) | |
810-
| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children) | | |
810+
| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children) | [![Java](assets/java.png)](src/DistributeMoneyToMaximumChildren.java) | |
811811
| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits) | | |
812812
| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum) | | |
813813
| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays) | | |
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/distribute-money-to-maximum-children
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class DistributeMoneyToMaximumChildren {
6+
public int distMoney(int money, int children) {
7+
if (children > money) return -1;
8+
9+
int childrenReceiving8 = Math.min((money - children) / 7, children);
10+
final int childrenNotReceiving8 = children - childrenReceiving8;
11+
final int moneyLeft = money - 8 * childrenReceiving8;
12+
13+
if (childrenNotReceiving8 == 1 && moneyLeft == 4) {
14+
childrenReceiving8--;
15+
}
16+
17+
if (children == childrenReceiving8 && (money / 8 != children || money % 8 != 0)) {
18+
childrenReceiving8--;
19+
}
20+
21+
return childrenReceiving8;
22+
}
23+
}

0 commit comments

Comments
 (0)