LeetCode 题解工作台

计算应缴税款总额

给你一个下标从 0 开始的二维整数数组 brackets ,其中 brackets[i] = [upper i , percent i ] ,表示第 i 个税级的上限是 upper i ,征收的税率为 percent i 。税级按上限 从低到高排序 (在满足 0 的前提下, upper i-1 i …

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·模拟

bolt

答案摘要

我们遍历 `brackets`,对于每个税级,计算该税级的税额,然后累加即可。 时间复杂度 ,其中 为 `brackets` 的长度。空间复杂度 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·模拟 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的二维整数数组 brackets ,其中 brackets[i] = [upperi, percenti] ,表示第 i 个税级的上限是 upperi ,征收的税率为 percenti 。税级按上限 从低到高排序(在满足 0 < i < brackets.length 的前提下,upperi-1 < upperi)。

税款计算方式如下:

  • 不超过 upper0 的收入按税率 percent0 缴纳
  • 接着 upper1 - upper0 的部分按税率 percent1 缴纳
  • 然后 upper2 - upper1 的部分按税率 percent2 缴纳
  • 以此类推

给你一个整数 income 表示你的总收入。返回你需要缴纳的税款总额。与标准答案误差不超 10-5 的结果将被视作正确答案。

 

示例 1:

输入:brackets = [[3,50],[7,10],[12,25]], income = 10
输出:2.65000
解释:
前 $3 的税率为 50% 。需要支付税款 $3 * 50% = $1.50 。
接下来 $7 - $3 = $4 的税率为 10% 。需要支付税款 $4 * 10% = $0.40 。
最后 $10 - $7 = $3 的税率为 25% 。需要支付税款 $3 * 25% = $0.75 。
需要支付的税款总计 $1.50 + $0.40 + $0.75 = $2.65 。

示例 2:

输入:brackets = [[1,0],[4,25],[5,50]], income = 2
输出:0.25000
解释:
前 $1 的税率为 0% 。需要支付税款 $1 * 0% = $0 。
剩下 $1 的税率为 25% 。需要支付税款 $1 * 25% = $0.25 。
需要支付的税款总计 $0 + $0.25 = $0.25 。

示例 3:

输入:brackets = [[2,50]], income = 0
输出:0.00000
解释:
没有收入,无需纳税,需要支付的税款总计 $0 。

 

提示:

  • 1 <= brackets.length <= 100
  • 1 <= upperi <= 1000
  • 0 <= percenti <= 100
  • 0 <= income <= 1000
  • upperi 按递增顺序排列
  • upperi 中的所有值 互不相同
  • 最后一个税级的上限大于等于 income
lightbulb

解题思路

方法一:模拟

我们遍历 brackets,对于每个税级,计算该税级的税额,然后累加即可。

时间复杂度 O(n)O(n),其中 nnbrackets 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
class Solution:
    def calculateTax(self, brackets: List[List[int]], income: int) -> float:
        ans = prev = 0
        for upper, percent in brackets:
            ans += max(0, min(income, upper) - prev) * percent
            prev = upper
        return ans / 100
speed

复杂度分析

指标
时间complexity is O(n) where n is the number of tax brackets, since each bracket is processed once. Space complexity is O(1) because only a few variables like prev and total tax are needed.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    They may ask you to explain why you track the previous bracket's upper bound.

  • question_mark

    They might probe your handling of income that exactly matches a bracket's upper bound.

  • question_mark

    They could test understanding of precision by providing rates or income that produce fractional taxes.

warning

常见陷阱

外企场景
  • error

    Forgetting to subtract the previous upper bound when calculating the taxable portion in a bracket.

  • error

    Continuing to compute tax for brackets when income is already fully taxed.

  • error

    Rounding intermediate calculations instead of maintaining precision until the final total.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Compute taxes with brackets given in descending order, requiring sorting first.

  • arrow_right_alt

    Calculate tax when brackets may overlap or have missing ranges, requiring conditional checks.

  • arrow_right_alt

    Determine total after applying deductions before applying the bracketed tax simulation.

help

常见问题

外企场景

计算应缴税款总额题解:数组·模拟 | LeetCode #2303 简单