返回介绍

solution / 2900-2999 / 2979.Most Expensive Item That Can Not Be Bought / README_EN

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

2979. Most Expensive Item That Can Not Be Bought

中文文档

Description

You are given two distinct prime numbers primeOne and primeTwo.

Alice and Bob are visiting a market. The market has an infinite number of items, for any positive integer x there exists an item whose price is x. Alice wants to buy some items from the market to gift to Bob. She has an infinite number of coins in the denomination primeOne and primeTwo. She wants to know the most expensive item she can not buy to gift to Bob.

Return _the price of the most expensive item which Alice can not gift to Bob_.

 

Example 1:

Input: primeOne = 2, primeTwo = 5
Output: 3
Explanation: The prices of items which cannot be bought are [1,3]. It can be shown that all items with a price greater than 3 can be bought using a combination of coins of denominations 2 and 5.

Example 2:

Input: primeOne = 5, primeTwo = 7
Output: 23
Explanation: The prices of items which cannot be bought are [1,2,3,4,6,8,9,11,13,16,18,23]. It can be shown that all items with a price greater than 23 can be bought.

 

Constraints:

  • 1 < primeOne, primeTwo < 104
  • primeOne, primeTwo are prime numbers.
  • primeOne * primeTwo < 105

Solutions

Solution 1: Chicken McNugget Theorem

According to the Chicken McNugget Theorem, for two coprime positive integers $a$ and $b$, the largest number that cannot be expressed as a combination of $a$ and $b$ is $ab - a - b$.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def mostExpensiveItem(self, primeOne: int, primeTwo: int) -> int:
    return primeOne * primeTwo - primeOne - primeTwo
class Solution {
  public int mostExpensiveItem(int primeOne, int primeTwo) {
    return primeOne * primeTwo - primeOne - primeTwo;
  }
}
class Solution {
public:
  int mostExpensiveItem(int primeOne, int primeTwo) {
    return primeOne * primeTwo - primeOne - primeTwo;
  }
};
func mostExpensiveItem(primeOne int, primeTwo int) int {
  return primeOne*primeTwo - primeOne - primeTwo
}
function mostExpensiveItem(primeOne: number, primeTwo: number): number {
  return primeOne * primeTwo - primeOne - primeTwo;
}
impl Solution {
  pub fn most_expensive_item(prime_one: i32, prime_two: i32) -> i32 {
    prime_one * prime_two - prime_one - prime_two
  }
}

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

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

发布评论

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