EasyNo. 2303
Calculate Amount Paid in Taxes — LeetCode 2303 Python Solution
- Topics
- Array, Simulation
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] means that the ith tax bracket has an upper bound of upperi and is taxed at a rate of percenti. The brackets are sorted by upper bound (i.e.
Example
- Input
- brackets = [[3,50],[7,10],[12,25]], income = 10
- Output
- 2.65000
- Explanation
- Based on your income, you have 3 dollars in the 1st tax bracket, 4 dollars in the 2nd tax bracket, and 3 dollars in the 3rd tax bracket.
Python solution
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 / 100Complexity
- Time
- O(n)where n is the length of `brackets`
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 2293 | Min Max Game | Easy |
| 0495 | Teemo Attacking | Easy |
| 0985 | Sum of Even Numbers After Queries | Medium |
| 1389 | Create Target Array in the Given Order | Easy |
| 1409 | Queries on a Permutation With Key | Medium |
| 1503 | Last Moment Before All Ants Fall Out of a Plank | Medium |
Frequently asked questions
- How hard is LeetCode 2303. Calculate Amount Paid in Taxes?
LeetCode 2303. Calculate Amount Paid in Taxes is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2303. Calculate Amount Paid in Taxes?
The Python solution on this page runs in O(n), where n is the length of `brackets`.
- What is the space complexity of LeetCode 2303. Calculate Amount Paid in Taxes?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2303. Calculate Amount Paid in Taxes cover?
LeetCode 2303. Calculate Amount Paid in Taxes is tagged Array and Simulation on LeetCode.