原文链接: https://leetcode-cn.com/problems/find-missing-observations
英文原文
You have observations of n + m
6-sided dice rolls with each face numbered from 1
to 6
. n
of the observations went missing, and you only have the observations of m
rolls. Fortunately, you have also calculated the average value of the n + m
rolls.
You are given an integer array rolls
of length m
where rolls[i]
is the value of the ith
observation. You are also given the two integers mean
and n
.
Return an array of length n
containing the missing observations such that the average value of the n + m
rolls is exactly mean
. If there are multiple valid answers, return any of them. If no such array exists, return an empty array.
The average value of a set of k
numbers is the sum of the numbers divided by k
.
Note that mean
is an integer, so the sum of the n + m
rolls should be divisible by n + m
.
Example 1:
Input: rolls = [3,2,4,3], mean = 4, n = 2 Output: [6,6] Explanation: The mean of all n + m rolls is (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4.
Example 2:
Input: rolls = [1,5,6], mean = 3, n = 4 Output: [2,3,2,2] Explanation: The mean of all n + m rolls is (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3.
Example 3:
Input: rolls = [1,2,3,4], mean = 6, n = 4 Output: [] Explanation: It is impossible for the mean to be 6 no matter what the 4 missing rolls are.
Example 4:
Input: rolls = [1], mean = 3, n = 1 Output: [5] Explanation: The mean of all n + m rolls is (1 + 5) / 2 = 3.
Constraints:
m == rolls.length
1 <= n, m <= 105
1 <= rolls[i], mean <= 6
中文题目
现有一份 n + m
次投掷单个 六面 骰子的观测数据,骰子的每个面从 1
到 6
编号。观测数据中缺失了 n
份,你手上只拿到剩余 m
次投掷的数据。幸好你有之前计算过的这 n + m
次投掷数据的 平均值 。
给你一个长度为 m
的整数数组 rolls
,其中 rolls[i]
是第 i
次观测的值。同时给你两个整数 mean
和 n
。
返回一个长度为 n
的数组,包含所有缺失的观测数据,且满足这 n + m
次投掷的 平均值 是 mean
。如果存在多组符合要求的答案,只需要返回其中任意一组即可。如果不存在答案,返回一个空数组。
k
个数字的 平均值 为这些数字求和后再除以 k
。
注意 mean
是一个整数,所以 n + m
次投掷的总和需要被 n + m
整除。
示例 1:
输入:rolls = [3,2,4,3], mean = 4, n = 2 输出:[6,6] 解释:所有 n + m 次投掷的平均值是 (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4 。
示例 2:
输入:rolls = [1,5,6], mean = 3, n = 4 输出:[2,3,2,2] 解释:所有 n + m 次投掷的平均值是 (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3 。
示例 3:
输入:rolls = [1,2,3,4], mean = 6, n = 4 输出:[] 解释:无论丢失的 4 次数据是什么,平均值都不可能是 6 。
示例 4:
输入:rolls = [1], mean = 3, n = 1 输出:[5] 解释:所有 n + m 次投掷的平均值是 (1 + 5) / 2 = 3 。
提示:
m == rolls.length
1 <= n, m <= 105
1 <= rolls[i], mean <= 6
通过代码
高赞题解
解题思路
mean * (m + n) 求总数
sum(rolls) 求已经有的数量
sum = mean * (m+n) - sum(rolls) 剩下需要补充的平均分配就行
< N * 1 或者 > N * 6 则不可能有合法分配
代码
class Solution {
public:
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
int m = rolls.size();
int total = m + n;
int sum = total * mean;
int sum1 = 0;
for (auto r: rolls) {
sum1 += r;
}
int sum2 = sum - sum1;
vector<int> ans;
if (sum2 < n || sum2 > 6 * n) return ans;
int r = sum2 % n;
int v = sum2 / n;
for (int i = 0; i < n; i++) {
if (i < r) {
ans.push_back(v+1);
} else {
ans.push_back(v);
}
}
return ans;
}
};
关于我
大家好,我是微扰君。18年毕业于上海交通大学,一个在阿里、字节、腾讯都工作过的工程师,有丰富的面试经验。从 2021.4 开始在emqx从事存储研发,希望在今年多多输出。
现在是北京【悖论13】剧本杀的老板,欢迎大家来探店。
想了解我的店或者一起刷题的可以 +v : constant_variation
最后,如果对你有帮助,可以点个赞支持一下我哦 也欢迎在leetcode上关注我。
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
4165 | 9737 | 42.8% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|