算法刷题打卡第74天:计算应缴税款总额

计算应缴税款总额

难度:简单

给你一个下标从 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 。

模拟

思路:
设第 i i i 个税级的上限是 upper i \textit{upper}_i upperi ,征收的税率为 percent i \textit{percent}_i percenti。税级按上限从低到高排序且满足 upper i − 1 < upper i \textit{upper}_{i-1} < \textit{upper}_i upperi1<upperi

根据题意税款计算方式如下:

  • 不超过 upper 0 \textit{upper}_0 upper0 的收入按税率 percent 0 \textit{percent}_0 percent0 缴纳;
  • 接着处于 [ upper 0 , upper 1 ] [\textit{upper}_0,\textit{upper}_1] [upper0,upper1] 的部分按税率 percent 1 \textit{percent}_1 percent1 缴纳;
  • 接着处于 [ upper 1 , upper 2 ] [\textit{upper}_1,\textit{upper}_2] [upper1,upper2] 的部分按税率 percent 2 \textit{percent}_2 percent2 缴纳;
  • 以此类推;

给定的 income \textit{income} income 表示总收入,依次计算 income \textit{income} income 处于第 i i i 个区间 [ upper i − 1 , upper i ] [\textit{upper}_{i-1},\textit{upper}_i] [upperi1,upperi] 之间的部分为 pay i \textit{pay}_i payi,此时计算公式为 pay i = min ⁡ ( income , upper i ) − upper i − 1 \textit{pay}_i = \min(\textit{income},\textit{upper}_i) - \textit{upper}_{i-1} payi=min(income,upperi)upperi1 ,则在第 i i i 个征税区间缴纳的税为 tax i = pay i × percent i \textit{tax}_i = \textit{pay}_i \times \textit{percent}_i taxi=payi×percenti ,则征税总额为 totalTax = ∑ i = 0 n − 1 tax i \textit{totalTax} = \sum\limits_{i=0}^{n-1}\textit{tax}_i totalTax=i=0n1taxi
其中第 0 0 0 个征税区间为 [ 0 , upper i ] [0,\textit{upper}_i] [0,upperi],为了计算方便可先进行整数累加,最后再进行浮点数除法计算。

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n 表示数组的长度。我们只需要遍历一遍数组即可。
  • 空间复杂度: O ( 1 ) O(1) O(1)
class Solution:
    def calculateTax(self, brackets: List[List[int]], income: int) -> float:
        prev, res = 0, 0
        while income > 0:
            upper, percent = brackets.pop(0)
            res += min(upper - prev, income) * percent
            income -= upper - prev
            prev = upper
        return res/100

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/calculate-amount-paid-in-taxes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏秃然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值