5891. 找出缺失的观测数据 - 力扣(LeetCode) (leetcode-cn.com)
import java.util.ArrayList;
import java.util.List;
class Solution {
public int[] missingRolls(int[] rolls, int mean, int n) {
int m = rolls.length;
int rolls_sum = 0;
for (int i = 0; i < m; i++) {
rolls_sum += rolls[i];
}
int defect_sum = mean * (m + n) - rolls_sum;//算出defect_sum
int[] ans = new int[n];
//defect_sum不能太小,小到比n还小
//defect_sum不能太大,大到比n*6还大
if (defect_sum < n || defect_sum > 6 * n) {
return new int[]{};
}
while (defect_sum > 0) {
for (int i = 0; i < n && defect_sum > 0; i++) {
ans[i] += 1;
defect_sum -= 1;
}
}
return ans;
}
}