Calculate Amount Paid in Taxes - Complete Solution Guide
Calculate Amount Paid in Taxes is LeetCode problem 2303, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed 2D integer array brackets where brackets[i] = [upper i , percent i ] means that the i th tax bracket has an upper bound of upper i and is taxed at a rate of percent i . The brackets are sorted by upper bound (i.e. upper i-1 < upper i for 0 < i < brackets.length ). Tax is calculated as follows: The first upper 0 dollars earned are taxed at a rate of percent 0 . The next upper 1 - upper 0 dollars earned are taxed at a rate of percent 1 . The next upper 2 - upper 1 dollars
Detailed Explanation
The problem asks you to calculate the total tax owed on a given income based on a tiered tax bracket system. The input is a 2D array `brackets` where each inner array represents a tax bracket: `[upper_bound, tax_rate]`. `upper_bound` is the maximum income subject to that tax rate, and `tax_rate` is the percentage to apply within that bracket. The brackets are sorted by their upper bounds. The input also includes `income`, the total earnings. The output is the total tax payable, a floating-point number.
Solution Approach
The provided code uses an iterative approach. It starts from the lowest tax bracket and iterates through the `brackets` array. For each bracket, it calculates the taxable income within that bracket by taking the minimum of the remaining `income` and the difference between the current bracket's `upper_bound` and the previous bracket's `upper_bound` (or 0 for the first bracket). The tax for that bracket is calculated and added to the running total. This process repeats until all the income is taxed or all brackets are processed. The function efficiently avoids redundant calculations.
Step-by-Step Algorithm
- Step 1: Initialize `tax` to 0.0 and `prev_upper` (or `prev`) to 0. `prev_upper` keeps track of the upper bound of the previously processed bracket.
- Step 2: Iterate through the `brackets` array. For each bracket `[upper, percent]`, calculate the `taxable` income within that bracket. This is the minimum of the remaining `income` and (`upper - prev_upper`).
- Step 3: Calculate the tax for the current bracket: `taxable * percent / 100.0`. Add this to the running `tax` total.
- Step 4: Update the remaining `income`: Subtract the `taxable` amount from the `income`.
- Step 5: Update `prev_upper`: Set `prev_upper` to the current bracket's `upper`.
- Step 6: If the remaining `income` is 0, exit the loop early.
- Step 7: Return the final `tax` value.
Key Insights
- Insight 1: Iterating through the brackets: The solution needs to process each tax bracket sequentially, calculating the taxable amount within each bracket and adding the corresponding tax to the total.
- Insight 2: Handling partial bracket income: The income might not fully utilize the highest tax bracket; it might fall somewhere within a bracket. The solution must handle this partial income correctly.
- Insight 3: Efficient calculation: To minimize calculation, it's crucial to use a running variable to track previously processed income and calculate only the taxable amount within each current bracket
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Simulation.
Companies
Asked at: Snowflake.