返回介绍

solution / 2000-2099 / 2028.Find Missing Observations / README_EN

发布于 2024-06-17 01:03:11 字数 6510 浏览 0 评论 0 收藏 0

2028. Find Missing Observations

中文文档

Description

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.

 

Constraints:

  • m == rolls.length
  • 1 <= n, m <= 105
  • 1 <= rolls[i], mean <= 6

Solutions

Solution 1: Construction

According to the problem description, the sum of all numbers is $(n + m) \times mean$, and the sum of known numbers is sum(rolls). Therefore, the sum of the missing numbers is $s = (n + m) \times mean - sum(rolls)$.

If $s > n \times 6$ or $s < n$, it means there is no answer that satisfies the conditions, so return an empty array.

Otherwise, we can evenly distribute $s$ to $n$ numbers, that is, the value of each number is $s / n$, and the value of $s \bmod n$ numbers is increased by $1$.

The time complexity is $O(n + m)$, and the space complexity is $O(1)$. Here, $n$ and $m$ are the number of missing numbers and known numbers, respectively.

class Solution:
  def missingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
    m = len(rolls)
    s = (n + m) * mean - sum(rolls)
    if s > n * 6 or s < n:
      return []
    ans = [s // n] * n
    for i in range(s % n):
      ans[i] += 1
    return ans
class Solution {
  public int[] missingRolls(int[] rolls, int mean, int n) {
    int m = rolls.length;
    int s = (n + m) * mean;
    for (int v : rolls) {
      s -= v;
    }
    if (s > n * 6 || s < n) {
      return new int[0];
    }
    int[] ans = new int[n];
    Arrays.fill(ans, s / n);
    for (int i = 0; i < s % n; ++i) {
      ++ans[i];
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
    int m = rolls.size();
    int s = (n + m) * mean;
    for (int& v : rolls) s -= v;
    if (s > n * 6 || s < n) return {};
    vector<int> ans(n, s / n);
    for (int i = 0; i < s % n; ++i) ++ans[i];
    return ans;
  }
};
func missingRolls(rolls []int, mean int, n int) []int {
  m := len(rolls)
  s := (n + m) * mean
  for _, v := range rolls {
    s -= v
  }
  if s > n*6 || s < n {
    return []int{}
  }
  ans := make([]int, n)
  for i, j := 0, 0; i < n; i, j = i+1, j+1 {
    ans[i] = s / n
    if j < s%n {
      ans[i]++
    }
  }
  return ans
}
function missingRolls(rolls: number[], mean: number, n: number): number[] {
  const len = rolls.length + n;
  const sum = rolls.reduce((p, v) => p + v);
  const max = n * 6;
  const min = n;
  if ((sum + max) / len < mean || (sum + min) / len > mean) {
    return [];
  }

  const res = new Array(n);
  for (let i = min; i <= max; i++) {
    if ((sum + i) / len === mean) {
      const num = Math.floor(i / n);
      res.fill(num);
      let count = i - n * num;
      let j = 0;
      while (count != 0) {
        if (res[j] === 6) {
          j++;
        } else {
          res[j]++;
          count--;
        }
      }
      break;
    }
  }
  return res;
}
impl Solution {
  pub fn missing_rolls(rolls: Vec<i32>, mean: i32, n: i32) -> Vec<i32> {
    let n = n as usize;
    let mean = mean as usize;
    let len = rolls.len() + n;
    let sum: i32 = rolls.iter().sum();
    let sum = sum as usize;
    let max = n * 6;
    let min = n;
    if sum + max < mean * len || sum + min > mean * len {
      return vec![];
    }

    let mut res = vec![0; n];
    for i in min..=max {
      if (sum + i) / len == mean {
        let num = i / n;
        res.fill(num as i32);
        let mut count = i - n * num;
        let mut j = 0;
        while count != 0 {
          if res[j] == 6 {
            j += 1;
          } else {
            res[j] += 1;
            count -= 1;
          }
        }
        break;
      }
    }
    res
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文