-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2028-find-missing-observations.js
More file actions
49 lines (44 loc) · 1.59 KB
/
2028-find-missing-observations.js
File metadata and controls
49 lines (44 loc) · 1.59 KB
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
/**
* 2028. Find Missing Observations
* https://leetcode.com/problems/find-missing-observations/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} rolls
* @param {number} mean
* @param {number} n
* @return {number[]}
*/
var missingRolls = function(rolls, mean, n) {
const totalRolls = rolls.length + n;
const targetSum = mean * totalRolls;
const currentSum = rolls.reduce((sum, roll) => sum + roll, 0);
const missingSum = targetSum - currentSum;
if (missingSum < n || missingSum > 6 * n) return [];
const baseValue = Math.floor(missingSum / n);
const remainder = missingSum % n;
const result = [];
for (let i = 0; i < n; i++) {
if (i < remainder) {
result.push(baseValue + 1);
} else {
result.push(baseValue);
}
}
return result;
};