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 _themaximum number of children who may receive exactly _8dollars if you distribute the money according to the aforementioned rules.
If there is no way to distribute the money, return -1.
Input: money =20, children =3Output: 1Explanation:
The maximum number of children with8 dollars will be 1. One of the ways to distribute the money is:-8 dollars to the first child.-9 dollars to the second child.-3 dollars to the third child.It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
To maximize the number of children who get exactly 8 dollars, we should give as many children as possible exactly 8 dollars, while ensuring that:
Each child gets at least 1 dollar.
No child gets exactly 4 dollars.
All money is distributed.
We need to be careful with the remaining money after giving 8 dollars to as many children as possible, to avoid the forbidden 4-dollar case.
classSolution {
public:int distMoney(int money, int children) {
if (money < children) return-1;
int max8 = min((money - children) /7, children);
for (int k = max8; k >=0; --k) {
int left = money - k *8;
int rest = children - k;
if (rest ==0&& left ==0) return k;
if (rest ==0) continue;
if (left >= rest && left !=4* rest) return k;
}
return-1;
}
};
funcdistMoney(moneyint, childrenint) int {
ifmoney < children {
return-1 }
max8:= (money-children) /7ifmax8 > children {
max8 = children }
fork:=max8; k>=0; k-- {
left:=money-k*8rest:=children-kifrest==0&&left==0 {
returnk }
ifrest==0 {
continue }
ifleft>=rest&&left!=4*rest {
returnk }
}
return-1}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintdistMoney(int money, int children) {
if (money < children) return-1;
int max8 = Math.min((money - children) / 7, children);
for (int k = max8; k >= 0; --k) {
int left = money - k * 8;
int rest = children - k;
if (rest == 0 && left == 0) return k;
if (rest == 0) continue;
if (left >= rest && left != 4 * rest) return k;
}
return-1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
fundistMoney(money: Int, children: Int): Int {
if (money < children) return -1var max8 = (money - children) / 7if (max8 > children) max8 = children
for (k in max8 downTo 0) {
val left = money - k * 8val rest = children - k
if (rest ==0&& left ==0) return k
if (rest ==0) continueif (left >= rest && left !=4 * rest) return k
}
return -1 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defdistMoney(self, money: int, children: int) -> int:
if money < children:
return-1 max8 = min((money - children) //7, children)
for k in range(max8, -1, -1):
left = money - k *8 rest = children - k
if rest ==0and left ==0:
return k
if rest ==0:
continueif left >= rest and left !=4* rest:
return k
return-1
impl Solution {
pubfndist_money(money: i32, children: i32) -> i32 {
if money < children {
return-1;
}
letmut max8 = (money - children) /7;
if max8 > children {
max8 = children;
}
for k in (0..=max8).rev() {
let left = money - k *8;
let rest = children - k;
if rest ==0&& left ==0 {
return k;
}
if rest ==0 {
continue;
}
if left >= rest && left !=4* rest {
return k;
}
}
-1 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
distMoney(money: number, children: number):number {
if (money<children) return-1;
letmax8= Math.min(Math.floor((money-children) /7), children);
for (letk=max8; k>=0; --k) {
constleft=money-k*8;
constrest=children-k;
if (rest===0&&left===0) returnk;
if (rest===0) continue;
if (left>=rest&&left!==4*rest) returnk;
}
return-1;
}
}