Employee Count Median, Mean and Mode
Problem statement
You downloaded a remote text file in which each line is one company's employee count. The file's lines are given as the string array lines.
Return [median, mean, mode] of the employee counts:
median: sort the counts by value. With an odd number of counts it is the middle one; with an even number it is the average of the two middle ones.mean: the sum of the counts divided by how many there are.mode: the count that appears most often. If several counts tie, return the smallest of them.
Each value within 10^-9 of the expected one (relative for values above 1) is accepted.
Function
employeeCountStats(lines: String[]) → double[]Examples
Example 1
lines = ["120","45","300","45","80"]return = [80.0,118.0,45.0]Sorted, the counts are 45, 45, 80, 120, 300. The median is the middle value 80, the mean is 590 / 5 = 118, and 45 appears twice, more than any other count. Sorting the lines as text instead would give "120", "300", "45", "45", "80" and a wrong middle value of 45.
Example 2
lines = ["10","400","25","400","10","7"]return = [17.5,142.0,10.0]Sorted: 7, 10, 10, 25, 400, 400. With an even count the median is the average of the two middle values, (10 + 25) / 2 = 17.5. The mean is 852 / 6 = 142. 10 and 400 both appear twice, so the mode is the smaller one, 10.
Example 3
lines = ["9","1000","250"]return = [250.0,419.6666666666667,9.0]Every count appears once, so the mode is the smallest count, 9. Sorted, the counts are 9, 250, 1000, so the median is 250. The mean is 1259 / 3 = 419.666....
Constraints
1 <= lines.length <= 2000- Each line is a decimal integer from
0to10^7with no sign, spaces or leading zeros.