当前位置:网站首页>LeetCode 5259. Calculate the total tax payable
LeetCode 5259. Calculate the total tax payable
2022-06-13 09:18:00 【Michael Amin】
List of articles
1. subject
I'll give you a subscript from 0 Start with a two-dimensional integer array brackets , among brackets[i] = [upperi, percenti] , It means the first one i The upper limit of individual income tax is upperi , The tax rate charged is percenti . Tax level by upper limit Sort from low to high ( In the meet 0 < i < brackets.length Under the premise of ,upperi-1 < upperi).
The tax is calculated as follows :
- No more than
upper0Income is taxedpercent0Pay - next
upper1 - upper0The part of the tax rate ispercent1Pay - then
upper2 - upper1The part of the tax rate ispercent2Pay And so on
Give you an integer income Represents your total income . Return the total amount of tax you have to pay . The error with the standard answer does not exceed 10-5 The result of will be regarded as the correct answer .
Example 1:
Input :brackets = [[3,50],[7,10],[12,25]], income = 10
Output :2.65000
explain :
front $3 The tax rate is 50% . Taxes are payable $3 * 50% = $1.50 .
Next $7 - $3 = $4 The tax rate is 10% . Taxes are payable $4 * 10% = $0.40 .
Last $10 - $7 = $3 The tax rate is 25% . Taxes are payable $3 * 25% = $0.75 .
Total taxes to be paid $1.50 + $0.40 + $0.75 = $2.65 .
Example 2:
Input :brackets = [[1,0],[4,25],[5,50]], income = 2
Output :0.25000
explain :
front $1 The tax rate is 0% . Taxes are payable $1 * 0% = $0 .
be left over $1 The tax rate is 25% . Taxes are payable $1 * 25% = $0.25 .
Total taxes to be paid $0 + $0.25 = $0.25 .
Example 3:
Input :brackets = [[2,50]], income = 0
Output :0.00000
explain :
No income , No tax is required , Total taxes to be paid $0 .
Tips :
1 <= brackets.length <= 100
1 <= upperi <= 1000
0 <= percenti <= 100
0 <= income <= 1000
upperi In ascending order
upperi All the values in Different from each other
The upper limit of the last tax level is greater than or equal to incomesource : Power button (LeetCode) link :https://leetcode.cn/problems/calculate-amount-paid-in-taxes Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Problem solving
- Imitate according to the meaning of the topic
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 # Interval tax amount
money = min(val, income) # The amount to be taxed
ans += money*brackets[i][1]/100 # pay taxes
income -= money # Update the remaining amount to be taxed
prev = brackets[i][0] # Tax breakpoints
else:
break
return ans44 ms 15.1 MB Python3
边栏推荐
猜你喜欢

SQL ROW_ The number() function uses

Storage mode of drawings

BGP Federation +community

JUC atomic reference and ABA problem

Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5

BGP 联邦+Community

Simulink如何添加模块到Library Browser

Yolov5 face learning notes

C/s model and P2P model

202012 CCF test questions
随机推荐
turtle库显示系统时间
20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?
计算两个时间相差的天数(支持跨月、跨年)
Drill down to protobuf - Introduction
LeetCode 322. 零钱兑换
C#入门系列(十三) -- 初识结构体
20211005 Hermite matrix and some properties
202012 CCF test questions
JUC原子整数
Online debugging tool Arthas advanced
20211028 调节和跟踪
LeetCode 583. 两个字符串的删除操作
Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
20220524 如何把CoppeliaSim安装到D盘
20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
Yolov5 face video stream
C language: five custom types
20211006 integral, differential and projection belong to linear transformation
@Value不生效及extend/implement其他类无法注入bean的手动注入
C language: timer principle