2591. Distribute Money to Maximum Children

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public int distMoney(int money, int children) {
// 先尝试每人分一元
money -= children;

if (money < 0) {
return -1;
}

// 给尽可能多的人每人分 7 元,注意最多分给 children 个人
int count = Math.min(money / 7, children);
money -= count * 7;

children -= count; // 可能还剩下部分人不足 8 元

if (children == 0 && money > 0) {
// 每个人都分了 8 元但是还有剩余的钱,此时把这部分钱都给其中一个人,但是要损失一个 8 元的人数
count--;
} else if (children == 1 && money == 3) {
// 剩余一个人不足 8 元且剩余 3 元,此时不能将这 3 元都分给这个人
count--;
}

return count;
}
}

根据 第 100 场双周赛 全国排名 的结果来看,许多人都会在此题上出错。

References

2591. Distribute Money to Maximum Children