[LeetCode] Distribute Money to Maximum Children

2591. Distribute Money to Maximum Children

You are given an integer money denoting the amount of money (in dollars) that you have and another integer children denoting the number of children that you must distribute the money to.

You have to distribute the money according to the following rules:

  • All money must be distributed.
  • Everyone must receive at least 1 dollar.
  • Nobody receives 4 dollars.

Return the maximum number of children who may receive exactly 8 dollars if you distribute the money according to the aforementioned rules. If there is no way to distribute the money, return -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int distMoney(int money, int children) {
if(money < children) return -1;
money -= children;
int res = 0;
for(int i = 0; i < children; i++) {
if(money - 7 >= 0) {
money -= 7;
res += 1;
} else {
if(money == 3) {
if(i + 1 == children) res -= 1;
}
money = 0;
}
}
if(money) res -= 1;
return max(0,res);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/03/18/PS/LeetCode/distribute-money-to-maximum-children/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.