当前位置:网站首页>LeetCode 5259. 计算应缴税款总额
LeetCode 5259. 计算应缴税款总额
2022-06-13 09:06:00 【Michael阿明】
文章目录
1. 题目
给你一个下标从 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来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/calculate-amount-paid-in-taxes 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- 按题意模拟
class Solution:
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
ans = 0
prev = 0
for i in range(len(brackets)):
if income > 0:
val = brackets[i][0] - prev # 区间税额
money = min(val, income) # 要交税的金额
ans += money*brackets[i][1]/100 # 交税
income -= money # 更新剩余要交税的金额
prev = brackets[i][0] # 税的断点
else:
break
return ans44 ms 15.1 MB Python3
边栏推荐
- Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
- Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
- Neo4j Environment Building
- JUC Unsafe
- 20211115 任意n阶方阵均与三角矩阵(上三角或者下三角)相似
- Batch read all voice files under the folder
- Solov2 source code analysis
- 共享模型之不可变
- Lecture par lots de tous les fichiers vocaux sous le dossier
- C/s model and P2P model
猜你喜欢

攻防世界PWN play 条件竞争漏洞的利用

20211020 academician all drive system

C language: minesweeping

How excel adds hyperlinks to some text in a cell

Redis fuzzy query batch deletion

What exactly is Huawei cloud desktop saying when it says "smooth"?

IP address introduction

C language: deep understanding of character functions and string functions (2)

教程篇(5.0) 02. 管理 * FortiEDR * Fortinet 网络安全专家 NSE 5

Top+jstack to analyze the causes of excessive CPU
随机推荐
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
Summary of the first retrospective meeting
20211108 A转置乘A是正定矩阵吗?A转置乘A是正定矩阵的充分必要条件是什么?
线上调试工具Arthas高级
[network security penetration] if you don't understand CSRF? This article gives you a thorough grasp
Redirect vulnerability analysis of network security vulnerability analysis
简单实现数据库链接池
Neo4j环境搭建
20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
20211018 一些特殊矩阵
LeetCode 201. 数字范围按位与
Drill down to protobuf - Introduction
How excel adds hyperlinks to some text in a cell
20211006 线性变换
消息中间件
20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
A very detailed blog about the implementation of bilinear interpolation by opencv
Neo4j environment construction
202012 CCF test questions
LeetCode 1143. 最长公共子序列