LeetCode 2394: Employees With Deductions

Database

Problem Description

Explanation:

To solve this problem, we can iterate through the list of employees and their deductions. For each employee, we calculate the total deduction amount and then store the result in a HashMap where the key is the employee ID and the value is the total deduction amount. Then, we iterate through the list of employees again and subtract their individual deduction amount from the total deduction amount for the employee. If the remaining deduction amount after subtraction is greater than 0, we include this employee in the final result.

Algorithm:

  1. Create a HashMap to store the total deduction amount for each employee.
  2. Iterate through the list of employees and their deductions and calculate the total deduction amount for each employee.
  3. Iterate through the list of employees again and check if the remaining deduction amount is greater than 0 after subtracting the individual deduction amount.
  4. If the remaining deduction amount is greater than 0, include the employee in the final result.
  5. Return the list of employees with deductions greater than 0.

Time Complexity:

  • The time complexity of this algorithm is O(N), where N is the number of employees.

Space Complexity:

  • The space complexity of this algorithm is O(N), where N is the number of employees.

:

Solutions

import java.util.*;

class Solution {
    public List<Integer> employeesWithDeductions(List<Integer> employeeIds, List<Integer> deductions) {
        Map<Integer, Integer> totalDeductions = new HashMap<>();
        List<Integer> result = new ArrayList<>();

        for (int i = 0; i < employeeIds.size(); i++) {
            totalDeductions.put(employeeIds.get(i), totalDeductions.getOrDefault(employeeIds.get(i), 0) + deductions.get(i));
        }

        for (int i = 0; i < employeeIds.size(); i++) {
            int remainingDeduction = totalDeductions.get(employeeIds.get(i)) - deductions.get(i);
            if (remainingDeduction > 0) {
                result.add(employeeIds.get(i));
            }
        }

        return result;
    }
}

Loading editor...