Skip to content

2028. Find Missing Observations 👍

  • Time: $O(n + m)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
    const int targetSum = (rolls.size() + n) * mean;
    int missingSum = targetSum - accumulate(rolls.begin(), rolls.end(), 0);
    if (missingSum > n * 6 || missingSum < n)
      return {};

    vector<int> ans(n, missingSum / n);
    missingSum %= n;
    for (int i = 0; i < missingSum; ++i)
      ++ans[i];

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
  public int[] missingRolls(int[] rolls, int mean, int n) {
    final int targetSum = (rolls.length + n) * mean;
    int missingSum = targetSum - Arrays.stream(rolls).sum();
    if (missingSum > n * 6 || missingSum < n)
      return new int[] {};

    int[] ans = new int[n];
    Arrays.fill(ans, missingSum / n);
    missingSum %= n;
    for (int i = 0; i < missingSum; ++i)
      ++ans[i];

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
  def missingRolls(self, rolls: list[int], mean: int, n: int) -> list[int]:
    targetSum = (len(rolls) + n) * mean
    missingSum = targetSum - sum(rolls)
    if missingSum > n * 6 or missingSum < n:
      return []

    ans = [missingSum // n] * n
    for i in range(missingSum % n):
      ans[i] += 1

    return ans