leetcode
leetcode 2401 ~ 2450
取整购买后的账户余额

取整购买后的账户余额

难度:

标签:

题目描述

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

代码结果

运行时间: 24 ms, 内存: 16.5 MB


/*
 * 思路:
 * 1. 使用Java Stream计算向下和向上取整到10的倍数的两个值。
 * 2. 比较这两个值哪个更接近purchaseAmount,如果差值相等,则选择较大的那个值。
 * 3. 用100减去最终选择的值,得到剩余余额。
 */

import java.util.stream.IntStream;

public class Solution {
    public int remainingBalance(int purchaseAmount) {
        int lowerMultiple = (purchaseAmount / 10) * 10;
        int upperMultiple = lowerMultiple + 10;
        int roundedAmount = IntStream.of(lowerMultiple, upperMultiple)
            .reduce((a, b) -> Math.abs(a - purchaseAmount) <= Math.abs(b - purchaseAmount) ? a : b)
            .getAsInt();
        if (upperMultiple - purchaseAmount == purchaseAmount - lowerMultiple) {
            roundedAmount = upperMultiple;
        }
        return 100 - roundedAmount;
    }
}

解释

方法:

题解的核心思路是计算购买金额purchaseAmount向最近的10的倍数取整的结果roundedAmount,并从初始余额100中减去这个值来得到购买后的余额。首先,判断购买金额对10取余的结果(purchaseAmount % 10),这个余数用于决定金额向上舍入还是向下舍入。如果余数大于等于5,则意味着应当向上取整到下一个10的倍数;如果余数小于5,则向下取整到最近的较小的10的倍数。

时间复杂度:

O(1)

空间复杂度:

O(1)

代码细节讲解

🦆
为什么在`purchaseAmount % 10`等于5时选择向上取整而不是向下取整?
根据常用的四舍五入规则,当一个数字在被除数的中点时,通常会向上舍入到更大的数。这样做可以减少总体的舍入误差,并且在许多应用中,向上取整被视为更加正面或保守的估计。在此算法中,采用向上取整可以确保在边界条件下,处理方式与常见的四舍五入方法一致。
🦆
算法中是否考虑了`purchaseAmount`为负数的情况,如果没有,如何修改算法来处理负数输入?
原始算法没有直接考虑负数输入的情况。为了处理负数输入,我们可以在取整操作之前,首先对`purchaseAmount`取绝对值,然后再应用取整逻辑。处理完毕后,再将结果转换回相应的负数,这样可以确保算法在负数输入下也能正确运行。修改后的代码可以是: python class Solution: def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int: if purchaseAmount < 0: purchaseAmount = -((-purchaseAmount + 5) // 10 * 10) else: if purchaseAmount % 10 >= 5: purchaseAmount = ((purchaseAmount // 10) + 1) * 10 else: purchaseAmount = (purchaseAmount // 10) * 10 return 100 - purchaseAmount
🦆
在计算向上取整的情况时,直接使用`(purchaseAmount // 10) + 1`是否会在某些情况下导致错误的取整结果?例如`purchaseAmount`为19时。
使用`(purchaseAmount // 10) + 1`在`purchaseAmount`为19时会正确工作,因为这将计算为`(19 // 10) + 1 = 2`,然后乘以10得到20,这是向上取整到最近的10的倍数的正确结果。这种方法在所有情况下都能正确地将金额向上舍入到下一个10的倍数。
🦆
题解中提到如果有多个最接近的10的倍数,应该选择较大的倍数。这个逻辑是否在所有情况下都正确反映了题目的指示?
题解中的逻辑是基于常规的四舍五入规则,即在正好处于两个10的倍数之间时(例如50%的情况),选择较大的倍数。这与许多实际应用中的舍入逻辑一致,例如在财务计算中常用的方法。因此,这种逻辑在大多数情况下都能正确反映题目的指示,除非题目明确指定了使用不同的舍入规则。

相关问题