返回介绍

solution / 2800-2899 / 2806.Account Balance After Rounded Purchase / README_EN

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

2806. Account Balance After Rounded Purchase

中文文档

Description

Initially, you have a bank account balance of 100 dollars.

You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars.

At the store where you will make the purchase, the purchase amount is rounded to the nearest multiple of 10. In other words, you pay a non-negative amount, roundedAmount, such that roundedAmount is a multiple of 10 and abs(roundedAmount - purchaseAmount) is minimized.

If there is more than one nearest multiple of 10, the largest multiple is chosen.

Return _an integer denoting your account balance after making a purchase worth _purchaseAmount_ dollars from the store._

Note: 0 is considered to be a multiple of 10 in this problem.

 

Example 1:

Input: purchaseAmount = 9
Output: 90
Explanation: In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90.

Example 2:

Input: purchaseAmount = 15
Output: 80
Explanation: In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen.
Hence, your account balance becomes 100 - 20 = 80.

 

Constraints:

  • 0 <= purchaseAmount <= 100

Solutions

Solution 1: Enumeration + Simulation

We enumerate all multiples of 10 within the range $[0, 100]$, and find the one that is closest to purchaseAmount, denoted as $x$. The answer is $100 - x$.

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

class Solution:
  def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
    diff, x = 100, 0
    for y in range(100, -1, -10):
      if (t := abs(y - purchaseAmount)) < diff:
        diff = t
        x = y
    return 100 - x
class Solution {
  public int accountBalanceAfterPurchase(int purchaseAmount) {
    int diff = 100, x = 0;
    for (int y = 100; y >= 0; y -= 10) {
      int t = Math.abs(y - purchaseAmount);
      if (t < diff) {
        diff = t;
        x = y;
      }
    }
    return 100 - x;
  }
}
class Solution {
public:
  int accountBalanceAfterPurchase(int purchaseAmount) {
    int diff = 100, x = 0;
    for (int y = 100; y >= 0; y -= 10) {
      int t = abs(y - purchaseAmount);
      if (t < diff) {
        diff = t;
        x = y;
      }
    }
    return 100 - x;
  }
};
func accountBalanceAfterPurchase(purchaseAmount int) int {
  diff, x := 100, 0
  for y := 100; y >= 0; y -= 10 {
    t := abs(y - purchaseAmount)
    if t < diff {
      diff = t
      x = y
    }
  }
  return 100 - x
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
function accountBalanceAfterPurchase(purchaseAmount: number): number {
  let [diff, x] = [100, 0];
  for (let y = 100; y >= 0; y -= 10) {
    const t = Math.abs(y - purchaseAmount);
    if (t < diff) {
      diff = t;
      x = y;
    }
  }
  return 100 - x;
}

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

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

发布评论

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