LC.P2591[将钱分给最多的儿童]

方法一:分类讨论

1
2
3
4
5
6
7
8
class Solution {
public int distMoney(int money, int children) {
if (money < children) return -1;
if (money > 8 * children) return children - 1;
if (money == 8 * children - 4) return children - 2;
return (money - children) / 7;
}
}
  • 时间复杂度:$O(1)$
  • 空间复杂度:$O(1)$