LeetCode 1468: Calculate Salaries Solution
Master LeetCode problem 1468 (Calculate Salaries), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1468. Calculate Salaries
Problem Explanation
Explanation
Given a list of salaries, you are tasked with calculating the sum of salaries after applying a special tax deduction rule. The rule states that if an employee's salary is higher than the average salary of all employees, their salary will be reduced by a certain percentage.
To solve this problem, we can follow these steps:
- Calculate the average salary of all employees.
- Iterate through the list of salaries and for each salary:
- If the salary is higher than the average salary, reduce it by the given percentage.
- Add the adjusted salary to the total sum.
- Return the total sum as the final result.
Time Complexity:
- Calculating the average salary: O(n)
- Iterating through the list of salaries: O(n) Overall time complexity: O(n)
Space Complexity:
- We only use a constant amount of extra space for variables. Overall space complexity: O(1)
Solution Code
class Solution {
public double calculateSalaries(int[] salaries, double percentage) {
double sum = 0;
double avgSalary = 0;
// Calculate average salary
for (int salary : salaries) {
avgSalary += salary;
}
avgSalary /= salaries.length;
// Calculate sum after deduction
for (int salary : salaries) {
if (salary > avgSalary) {
sum += salary - (salary * percentage);
} else {
sum += salary;
}
}
return sum;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1468 (Calculate Salaries)?
This page provides optimized solutions for LeetCode problem 1468 (Calculate Salaries) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1468 (Calculate Salaries)?
The time complexity for LeetCode 1468 (Calculate Salaries) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1468 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1468 in Java, C++, or Python.