Calculate Amount Paid in Taxes LeetCode solution in Python

1
# Runtime: 75 ms (Top 95.72%) | Memory: 13.9 MB (Top 34.37%)
2

3

4
class Solution:
5
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
6
taxtot = 0
7
if brackets[0][0] < income:
8
taxtot += brackets[0][0] * (brackets[0][1])
9
income -= brackets[0][0]
10
else:
11
taxtot += income * (brackets[0][1])
12
return taxtot / 100
13
i = 1
14
while income > 0 and i < len(brackets):
15
if income > (brackets[i][0] - brackets[i - 1][0]):
16
taxtot += (brackets[i][0] - brackets[i - 1][0]) * brackets[i][1]
17
income -= brackets[i][0] - brackets[i - 1][0]
18
else:
19
taxtot += income * brackets[i][1]
20
income = 0
21
i += 1
22
return taxtot / 100

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0