|
| 1 | +# [2591. Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children) |
| 2 | + |
| 3 | +[中文文档](/solution/2500-2599/2591.Distribute%20Money%20to%20Maximum%20Children/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p> |
| 8 | + |
| 9 | +<p>You have to distribute the money according to the following rules:</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li>All money must be distributed.</li> |
| 13 | + <li>Everyone must receive at least <code>1</code> dollar.</li> |
| 14 | + <li>Nobody receives <code>4</code> dollars.</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p>Return <em>the <strong>maximum</strong> number of children who may receive <strong>exactly</strong> </em><code>8</code> <em>dollars if you distribute the money according to the aforementioned rules</em>. If there is no way to distribute the money, return <code>-1</code>.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> money = 20, children = 3 |
| 24 | +<strong>Output:</strong> 1 |
| 25 | +<strong>Explanation:</strong> |
| 26 | +The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is: |
| 27 | +- 8 dollars to the first child. |
| 28 | +- 9 dollars to the second child. |
| 29 | +- 3 dollars to the third child. |
| 30 | +It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> money = 16, children = 2 |
| 37 | +<strong>Output:</strong> 2 |
| 38 | +<strong>Explanation:</strong> Each child can be given 8 dollars. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li><code>1 <= money <= 200</code></li> |
| 46 | + <li><code>2 <= children <= 30</code></li> |
| 47 | +</ul> |
| 48 | + |
| 49 | +## Solutions |
| 50 | + |
| 51 | +<!-- tabs:start --> |
| 52 | + |
| 53 | +### **Python3** |
| 54 | + |
| 55 | +```python |
| 56 | +class Solution: |
| 57 | + def distMoney(self, money: int, children: int) -> int: |
| 58 | + if money < children: |
| 59 | + return -1 |
| 60 | + if money > 8 * children: |
| 61 | + return children - 1 |
| 62 | + if money == 8 * children - 4: |
| 63 | + return children - 2 |
| 64 | + # money-8x >= children-x, x <= (money-children)/7 |
| 65 | + return (money - children) // 7 |
| 66 | +``` |
| 67 | + |
| 68 | +### **Java** |
| 69 | + |
| 70 | +```java |
| 71 | +class Solution { |
| 72 | + public int distMoney(int money, int children) { |
| 73 | + if (money < children) { |
| 74 | + return -1; |
| 75 | + } |
| 76 | + if (money > 8 * children) { |
| 77 | + return children - 1; |
| 78 | + } |
| 79 | + if (money == 8 * children - 4) { |
| 80 | + return children - 2; |
| 81 | + } |
| 82 | + // money-8x >= children-x, x <= (money-children)/7 |
| 83 | + return (money - children) / 7; |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### **C++** |
| 89 | + |
| 90 | +```cpp |
| 91 | +class Solution { |
| 92 | +public: |
| 93 | + int distMoney(int money, int children) { |
| 94 | + if (money < children) { |
| 95 | + return -1; |
| 96 | + } |
| 97 | + if (money > 8 * children) { |
| 98 | + return children - 1; |
| 99 | + } |
| 100 | + if (money == 8 * children - 4) { |
| 101 | + return children - 2; |
| 102 | + } |
| 103 | + // money-8x >= children-x, x <= (money-children)/7 |
| 104 | + return (money - children) / 7; |
| 105 | + } |
| 106 | +}; |
| 107 | +``` |
| 108 | +
|
| 109 | +### **Go** |
| 110 | +
|
| 111 | +```go |
| 112 | +func distMoney(money int, children int) int { |
| 113 | + if money < children { |
| 114 | + return -1 |
| 115 | + } |
| 116 | + if money > 8*children { |
| 117 | + return children - 1 |
| 118 | + } |
| 119 | + if money == 8*children-4 { |
| 120 | + return children - 2 |
| 121 | + } |
| 122 | + // money-8x >= children-x, x <= (money-children)/7 |
| 123 | + return (money - children) / 7 |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### **...** |
| 128 | + |
| 129 | +``` |
| 130 | +
|
| 131 | +``` |
| 132 | + |
| 133 | +<!-- tabs:end --> |
0 commit comments