-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1491.去掉最低工资和最高工资后的工资平均值.go
91 lines (86 loc) · 1.85 KB
/
1491.去掉最低工资和最高工资后的工资平均值.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* @lc app=leetcode.cn id=1491 lang=golang
*
* [1491] 去掉最低工资和最高工资后的工资平均值
*
* https://leetcode-cn.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/description/
*
* algorithms
* Easy (64.58%)
* Likes: 34
* Dislikes: 0
* Total Accepted: 31.9K
* Total Submissions: 49.4K
* Testcase Example: '[4000,3000,1000,2000]'
*
* 给你一个整数数组 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 以内的结果都将视为正确答案。
*
*
*/
// @lc code=start
func average(salary []int) float64 {
minS := math.MaxInt32
maxS := math.MinInt32
sum := 0
for _, s := range salary {
minS = min(s, minS)
maxS = max(s, maxS)
sum += s
}
return float64(sum-minS-maxS) / float64(len(salary)-2)
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
// @lc code=end