返回介绍

solution / 2500-2599 / 2591.Distribute Money to Maximum Children / README_EN

发布于 2024-06-17 01:03:02 字数 5258 浏览 0 评论 0 收藏 0

2591. Distribute Money to Maximum Children

中文文档

Description

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.

 

Example 1:

Input: money = 20, children = 3
Output: 1
Explanation: 
The maximum number of children with 8 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.

Example 2:

Input: money = 16, children = 2
Output: 2
Explanation: Each child can be given 8 dollars.

 

Constraints:

  • 1 <= money <= 200
  • 2 <= children <= 30

Solutions

Solution 1: Case analysis

If $money \lt children$, then there must be a child who did not receive money, return $-1$.

If $money \gt 8 \times children$, then there are $children-1$ children who received $8$ dollars, and the remaining child received $money - 8 \times (children-1)$ dollars, return $children-1$.

If $money = 8 \times children - 4$, then there are $children-2$ children who received $8$ dollars, and the remaining two children shared the remaining $12$ dollars (as long as it is not $4$, $8$ dollars is fine), return $children-2$.

If we assume that there are $x$ children who received $8$ dollars, then the remaining money is $money- 8 \times x$, as long as it is greater than or equal to the number of remaining children $children-x$, it can meet the requirements. Therefore, we only need to find the maximum value of $x$, which is the answer.

Time complexity $O(1)$, space complexity $O(1)$.

class Solution:
  def distMoney(self, money: int, children: int) -> int:
    if money < children:
      return -1
    if money > 8 * children:
      return children - 1
    if money == 8 * children - 4:
      return children - 2
    # money-8x >= children-x, x <= (money-children)/7
    return (money - children) // 7
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;
    }
    // money-8x >= children-x, x <= (money-children)/7
    return (money - children) / 7;
  }
}
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;
    }
    // money-8x >= children-x, x <= (money-children)/7
    return (money - children) / 7;
  }
};
func distMoney(money int, children int) int {
  if money < children {
    return -1
  }
  if money > 8*children {
    return children - 1
  }
  if money == 8*children-4 {
    return children - 2
  }
  // money-8x >= children-x, x <= (money-children)/7
  return (money - children) / 7
}
function distMoney(money: number, children: number): number {
  if (money < children) {
    return -1;
  }
  if (money > 8 * children) {
    return children - 1;
  }
  if (money === 8 * children - 4) {
    return children - 2;
  }
  return Math.floor((money - children) / 7);
}
impl Solution {
  pub fn dist_money(money: i32, children: i32) -> i32 {
    if money < children {
      return -1;
    }

    if money > children * 8 {
      return children - 1;
    }

    if money == children * 8 - 4 {
      return children - 2;
    }

    (money - children) / 7
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文