|
| 1 | +/** |
| 2 | + * 2591 |
| 3 | + * Distribute Money to Maximum Children |
| 4 | + ** |
| 5 | + * You are given an integer money denoting the amount of money (in dollars) |
| 6 | + * that you have and another integer children |
| 7 | + * denoting the number of children that you must distribute the money to. |
| 8 | + * |
| 9 | + * You have to distribute the money according to the following rules: |
| 10 | + * • All money must be distributed. |
| 11 | + * • Everyone must receive at least 1 dollar. |
| 12 | + * • Nobody receives 4 dollars. |
| 13 | + * |
| 14 | + * Return the maximum number of children |
| 15 | + * who may receive exactly 8 dollars |
| 16 | + * if you distribute the money according to the aforementioned rules. |
| 17 | + * If there is no way to distribute the money, return -1. |
| 18 | + * |
| 19 | + * Example 1: |
| 20 | + * Input: money = 20, children = 3 |
| 21 | + * Output: 1 |
| 22 | + * Explanation: |
| 23 | + * The maximum number of children with 8 dollars will be 1. |
| 24 | + * One of the ways to distribute the money is: |
| 25 | + * - 8 dollars to the first child. |
| 26 | + * - 9 dollars to the second child. |
| 27 | + * - 3 dollars to the third child. |
| 28 | + * It can be proven that no distribution exists |
| 29 | + * such that number of children getting 8 dollars is greater than 1. |
| 30 | + * |
| 31 | + * Example 2: |
| 32 | + * Input: money = 16, children = 2 |
| 33 | + * Output: 2 |
| 34 | + * Explanation: Each child can be given 8 dollars. |
| 35 | + * |
| 36 | + * Constraints: |
| 37 | + * • 1 <= money <= 200 |
| 38 | + * • 2 <= children <= 30 |
| 39 | + * |
| 40 | + * Hint 1: |
| 41 | + * Can we distribute the money according to the rules |
| 42 | + * if we give 'k' children exactly 8 dollars? |
| 43 | + * |
| 44 | + * Hint 2: |
| 45 | + * Brute force to find the largest possible value of k, |
| 46 | + * or return -1 if there doesn’t exist any such k. |
| 47 | + ** |
| 48 | + * https://leetcode.com/problems/distribute-money-to-maximum-children/ |
| 49 | +***/ |
| 50 | + |
| 51 | +using System; |
| 52 | + |
| 53 | +namespace Problems; |
| 54 | + |
| 55 | +public class DistributeMoneyToMaximumChildren |
| 56 | +{ |
| 57 | + public int DistMoney( int money, int children ) |
| 58 | + { |
| 59 | + money -= children; |
| 60 | + |
| 61 | + if ( money < 0 ) |
| 62 | + { |
| 63 | + return -1; |
| 64 | + } |
| 65 | + |
| 66 | + if ( children * 7 == money ) |
| 67 | + { |
| 68 | + return children; |
| 69 | + } |
| 70 | + |
| 71 | + if ( money / 7 == children - 1 && money % 7 == 3 ) |
| 72 | + { |
| 73 | + return children - 2; |
| 74 | + } |
| 75 | + |
| 76 | + return Math.Min( children - 1, money / 7 ); |
| 77 | + } |
| 78 | +} |
0 commit comments