LeetCode 569: Median Employee Salary

Database

Problem Description

Explanation:

To find the median employee salary, we need to first sort the salaries array and then find the median. If the number of salaries is odd, the median is the middle element. If the number of salaries is even, the median is the average of the middle two elements.

Algorithm:

  1. Sort the salaries array.
  2. Find the number of employees, n.
  3. If n is odd, return the middle element from the sorted array.
  4. If n is even, return the average of the two middle elements.

Time Complexity:

The time complexity of this approach is O(nlogn) where n is the number of employees.

Space Complexity:

The space complexity is O(n) to store the sorted array of salaries.

: :

Solutions

class Solution {
    public double findMedianEmployeeSalary(int[] salaries) {
        Arrays.sort(salaries);
        int n = salaries.length;
        if (n % 2 == 1) {
            return (double) salaries[n / 2];
        } else {
            return (double) (salaries[n / 2 - 1] + salaries[n / 2]) / 2;
        }
    }
}

Loading editor...