1491. 去掉最低工资和最高工资后的工资平均值
给你一个整数数组 salary
,数组里每个数都是 唯一 的,其中 salary[i]
是第 i
个员工的工资。
请你返回去掉最低工资和最高工资以后,剩下员工工资的平均值。
示例 1:
输入:salary = [4000,3000,1000,2000] 输出:2500.00000 解释:最低工资和最高工资分别是 1000 和 4000 。 去掉最低工资和最高工资以后的平均工资是 (2000+3000)/2= 2500
示例 2:
输入:salary = [1000,2000,3000] 输出:2000.00000 解释:最低工资和最高工资分别是 1000 和 3000 。 去掉最低工资和最高工资以后的平均工资是 (2000)/1= 2000
示例 3:
输入:salary = [6000,5000,4000,3000,2000,1000] 输出:3500.00000
示例 4:
输入:salary = [8000,9000,2000,3000,6000,1000] 输出:4750.00000
提示:
3 <= salary.length <= 100
10^3 <= salary[i] <= 10^6
salary[i]
是唯一的。10^-5
以内的结果都将视为正确答案。原站题解
rust 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2024-05-03 12:15:00
impl Solution { pub fn average(salary: Vec<i32>) -> f64 { let s = salary.iter().sum::<i32>(); let m = *salary.iter().min().unwrap(); let M = *salary.iter().max().unwrap(); let n = salary.len(); (s - m - M) as f64 / (n - 2) as f64 } }
javascript 解法, 执行用时: 58 ms, 内存消耗: 49.1 MB, 提交时间: 2024-05-03 12:14:43
/** * @param {number[]} salary * @return {number} */ var average = function(salary) { let s = _.sum(salary); let m = _.min(salary); let M = _.max(salary); let n = salary.length; return (s - m - M) / (n - 2); }
golang 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2024-05-03 12:14:29
func average(salary []int) float64 { s := 0 for _, x := range salary { s += x } m := slices.Min(salary) M := slices.Max(salary) n := len(salary) return float64(s-m-M) / float64(n-2) }
cpp 解法, 执行用时: 4 ms, 内存消耗: 8.5 MB, 提交时间: 2024-05-03 12:14:15
class Solution { public: double average(vector<int>& salary) { int s = reduce(salary.begin(), salary.end()); auto [m, M] = ranges::minmax(salary); return (double) (s - m - M) / (salary.size() - 2); } };
java 解法, 执行用时: 2 ms, 内存消耗: 40.3 MB, 提交时间: 2024-05-03 12:13:54
class Solution { public double average(int[] salary) { int s = Arrays.stream(salary).sum(); int m = Arrays.stream(salary).min().getAsInt(); int M = Arrays.stream(salary).max().getAsInt(); int n = salary.length; return (double) (s - m - M) / (n - 2); } }
java 解法, 执行用时: 0 ms, 内存消耗: 40.2 MB, 提交时间: 2024-05-03 12:13:40
class Solution { public double average(int[] salary) { int s = 0; int m = Integer.MAX_VALUE; int M = Integer.MIN_VALUE; for (int x : salary) { s += x; m = Math.min(m, x); M = Math.max(M, x); } int n = salary.length; return (double) (s - m - M) / (n - 2); } }
python3 解法, 执行用时: 39 ms, 内存消耗: 16.4 MB, 提交时间: 2024-05-03 12:13:25
class Solution: def average(self, salary: List[int]) -> float: s = sum(salary) m = min(salary) M = max(salary) n = len(salary) return (s - m - M) / (n - 2)
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-06-15 14:31:50
func average(salary []int) float64 { n, max, min, total := len(salary), salary[0], salary[0], 0 for _, s := range salary { total += s if s > max { max = s } if s < min { min = s } } return float64( total - max - min ) / float64(n - 2 ) }