[Solved] Calculate Amount Paid in Taxes LeetCode Contest Problem

Calculate Amount Paid in Taxes: 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. upperi-1 < upperi for 0 < i < brackets.length).

Tax is calculated as follows:

  • The first upper0 dollars earned are taxed at a rate of percent0.
  • The next upper1 - upper0 dollars earned are taxed at a rate of percent1.
  • The next upper2 - upper1 dollars earned are taxed at a rate of percent2.
  • And so on.

You are given an integer income representing the amount of money you earned. Return the amount of money that you have to pay in taxes. Answers within 10-5 of the actual answer will be accepted.

Example 1:

Input: brackets = [[3,50],[7,10],[12,25]], income = 10
Output: 2.65000
Explanation:
The first 3 dollars you earn are taxed at 50%. You have to pay $3 * 50% = $1.50 dollars in taxes.
The next 7 - 3 = 4 dollars you earn are taxed at 10%. You have to pay $4 * 10% = $0.40 dollars in taxes.
The final 10 - 7 = 3 dollars you earn are taxed at 25%. You have to pay $3 * 25% = $0.75 dollars in taxes.
You have to pay a total of $1.50 + $0.40 + $0.75 = $2.65 dollars in taxes.

Example 2:

Input: brackets = [[1,0],[4,25],[5,50]], income = 2
Output: 0.25000
Explanation:
The first dollar you earn is taxed at 0%. You have to pay $1 * 0% = $0 dollars in taxes.
The second dollar you earn is taxed at 25%. You have to pay $1 * 25% = $0.25 dollars in taxes.
You have to pay a total of $0 + $0.25 = $0.25 dollars in taxes.

Example 3:

Input: brackets = [[2,50]], income = 0
Output: 0.00000
Explanation:
You have no income to tax, so you have to pay a total of $0 dollars in taxes.

Constraints:

  • 1 <= brackets.length <= 100
  • 1 <= upperi <= 1000
  • 0 <= percenti <= 100
  • 0 <= income <= 1000
  • upperi is sorted in ascending order.
  • All the values of upperi are unique.
  • The upper bound of the last tax bracket is greater than or equal to income.

Solution:

Time Complexity: O(n)

class Solution {
    public double calculateTax(int[][] brackets, int income) {
        if(income==0)
            return 0.00000;
        if(brackets[0][0]>income)
            return (double)income*(brackets[0][1]/100.00);
        int prev=0;
        for(int i=0;i<brackets.length;i++){
            int x=brackets[i][0];
            brackets[i][0]=brackets[i][0]-prev;
            prev=x;
            
        }
        double res=0.0000;
        double calc=(double)brackets[0][0]*(brackets[0][1]/100.00);
        res=calc;
        income-=brackets[0][0];
        for(int i=1;i<brackets.length;i++){
            if(income>=brackets[i][0]){
                calc=(double)brackets[i][0]*(brackets[i][1]/100.00);
                res+=calc;
                income-=brackets[i][0];
            }
            else if(income>0){
                calc=(double)income*(brackets[i][1]/100.00);
                res+=calc;
                income-=income;
            }
            else if(income==0)
                break;
        }
        return res;
    }
}
class Solution {
public:
    double calculateTax(vector<vector<int>>& brackets, int income) {
        double ans=0;  
        if(income==0){
            return ans;
        }
        int prev=0; 
        for(int i=0;i<brackets.size();i++){
            int actual=min(brackets[i][0],income);
            ans+=((actual-prev)*brackets[i][1]*1.0)/100.0;
            if(brackets[i][0]>=income){
                break;
            }
            prev=brackets[i][0];
        }
        return ans;
        
    }
};
    def calculateTax(self, brackets: List[List[int]], income: int) -> float:
        tax = prev = 0
        for upper, p in brackets:
            if income >= upper:
                tax += (upper - prev) * p / 100
                prev = upper
            else:
                tax += (income - prev) * p / 100
                return tax
        return tax    

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *