Calculate Amount Paid in Taxes — LeetCode 2303 Python Solution

EasyArraySimulation
Problem
#2303
Reading time
2 min

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

Python
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 / 100

Complexity

MeasureComplexity
TimeO(n), where n is the length of `brackets`
SpaceO(1) auxiliary

Related problems

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview