2028. 找出缺失的观测数据

找出缺失的观测数据

解法一: 数学

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func missingRolls(rolls []int, mean int, n int) []int {
m := len(rolls)
sum := mean * (m + n)
curSum := 0
for _, roll := range rolls {
curSum += roll
}
subSum := sum - curSum
if subSum < n || subSum > 6*n {
return []int{}
}
ans := make([]int, n)
per := subSum / n
div := subSum - per*n
for i := 0; i < n; i++ {
ans[i] = per + min(1, div)
div = max(0, div-1)
}
return ans
}
作者

wuhunyu

发布于

2024-05-27

更新于

2025-01-15

许可协议