diff --git a/solution/2000-2099/2028.Find Missing Observations/README.md b/solution/2000-2099/2028.Find Missing Observations/README.md index d7fb13431874c..8ca34e9873b1c 100644 --- a/solution/2000-2099/2028.Find Missing Observations/README.md +++ b/solution/2000-2099/2028.Find Missing Observations/README.md @@ -82,13 +82,13 @@ tags: ### 方法一:构造 -根据题目描述,所有数字之和为 $(n + m) \times mean$,已知的数字之和为 `sum(rolls)`,那么缺失的数字之和为 $s = (n + m) \times mean - sum(rolls)$。 +根据题目描述,所有数字之和为 $(n + m) \times \text{mean}$,已知的数字之和为 $\sum_{i=0}^{m-1} \text{rolls}[i]$,那么缺失的数字之和为 $s = (n + m) \times \text{mean} - \sum_{i=0}^{m-1} \text{rolls}[i]$。 如果 $s \gt n \times 6$ 或者 $s \lt n$,说明不存在满足条件的答案,返回空数组。 否则,我们可以将 $s$ 平均分配到 $n$ 个数字上,即每个数字的值为 $s / n$,其中 $s \bmod n$ 个数字的值再加上 $1$。 -时间复杂度 $O(n + m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为缺失的数字个数和已知的数字个数。 +时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别为缺失的数字个数和已知的数字个数。忽略答案的空间消耗,空间复杂度 $O(1)$。 @@ -137,11 +137,14 @@ class Solution { public: vector missingRolls(vector& 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 {}; + int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0); + if (s > n * 6 || s < n) { + return {}; + } vector ans(n, s / n); - for (int i = 0; i < s % n; ++i) ++ans[i]; + for (int i = 0; i < s % n; ++i) { + ++ans[i]; + } return ans; } }; @@ -174,33 +177,16 @@ func missingRolls(rolls []int, mean int, n int) []int { ```ts 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) { + const m = rolls.length; + const s = (n + m) * mean - rolls.reduce((a, b) => a + b, 0); + if (s > n * 6 || s < n) { 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; - } + const ans: number[] = Array(n).fill((s / n) | 0); + for (let i = 0; i < s % n; ++i) { + ans[i]++; } - return res; + return ans; } ``` @@ -209,36 +195,19 @@ function missingRolls(rolls: number[], mean: number, n: number): number[] { ```rust impl Solution { pub fn missing_rolls(rolls: Vec, mean: i32, n: i32) -> Vec { - 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 { + let m = rolls.len() as i32; + let s = (n + m) * mean - rolls.iter().sum::(); + + if s > n * 6 || s < n { 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; - } + let mut ans = vec![s / n; n as usize]; + for i in 0..(s % n) as usize { + ans[i] += 1; } - res + + ans } } ``` diff --git a/solution/2000-2099/2028.Find Missing Observations/README_EN.md b/solution/2000-2099/2028.Find Missing Observations/README_EN.md index 753c7b117082e..c069a29c629e4 100644 --- a/solution/2000-2099/2028.Find Missing Observations/README_EN.md +++ b/solution/2000-2099/2028.Find Missing Observations/README_EN.md @@ -72,13 +72,13 @@ tags: ### 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)$. +According to the problem description, the sum of all numbers is $(n + m) \times \text{mean}$, and the sum of known numbers is $\sum_{i=0}^{m-1} \text{rolls}[i]$. Therefore, the sum of the missing numbers is $s = (n + m) \times \text{mean} - \sum_{i=0}^{m-1} \text{rolls}[i]$. -If $s > n \times 6$ or $s < n$, it means there is no answer that satisfies the conditions, so return an empty array. +If $s \gt n \times 6$ or $s \lt n$, it means there is no answer that satisfies the conditions, so we 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. +The time complexity is $O(n + m)$, where $n$ and $m$ are the number of missing numbers and known numbers, respectively. Ignoring the space consumption of the answer, the space complexity is $O(1)$. @@ -127,11 +127,14 @@ class Solution { public: vector missingRolls(vector& 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 {}; + int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0); + if (s > n * 6 || s < n) { + return {}; + } vector ans(n, s / n); - for (int i = 0; i < s % n; ++i) ++ans[i]; + for (int i = 0; i < s % n; ++i) { + ++ans[i]; + } return ans; } }; @@ -164,33 +167,16 @@ func missingRolls(rolls []int, mean int, n int) []int { ```ts 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) { + const m = rolls.length; + const s = (n + m) * mean - rolls.reduce((a, b) => a + b, 0); + if (s > n * 6 || s < n) { 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; - } + const ans: number[] = Array(n).fill((s / n) | 0); + for (let i = 0; i < s % n; ++i) { + ans[i]++; } - return res; + return ans; } ``` @@ -199,36 +185,19 @@ function missingRolls(rolls: number[], mean: number, n: number): number[] { ```rust impl Solution { pub fn missing_rolls(rolls: Vec, mean: i32, n: i32) -> Vec { - 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 { + let m = rolls.len() as i32; + let s = (n + m) * mean - rolls.iter().sum::(); + + if s > n * 6 || s < n { 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; - } + let mut ans = vec![s / n; n as usize]; + for i in 0..(s % n) as usize { + ans[i] += 1; } - res + + ans } } ``` diff --git a/solution/2000-2099/2028.Find Missing Observations/Solution.cpp b/solution/2000-2099/2028.Find Missing Observations/Solution.cpp index 8f54085bfbdbd..3eba2986d209a 100644 --- a/solution/2000-2099/2028.Find Missing Observations/Solution.cpp +++ b/solution/2000-2099/2028.Find Missing Observations/Solution.cpp @@ -2,11 +2,14 @@ class Solution { public: vector missingRolls(vector& 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 {}; + int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0); + if (s > n * 6 || s < n) { + return {}; + } vector ans(n, s / n); - for (int i = 0; i < s % n; ++i) ++ans[i]; + for (int i = 0; i < s % n; ++i) { + ++ans[i]; + } return ans; } }; \ No newline at end of file diff --git a/solution/2000-2099/2028.Find Missing Observations/Solution.rs b/solution/2000-2099/2028.Find Missing Observations/Solution.rs index f2a186aa447ea..57ff7649e09ee 100644 --- a/solution/2000-2099/2028.Find Missing Observations/Solution.rs +++ b/solution/2000-2099/2028.Find Missing Observations/Solution.rs @@ -1,34 +1,17 @@ impl Solution { pub fn missing_rolls(rolls: Vec, mean: i32, n: i32) -> Vec { - 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 { + let m = rolls.len() as i32; + let s = (n + m) * mean - rolls.iter().sum::(); + + if s > n * 6 || s < n { 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; - } + let mut ans = vec![s / n; n as usize]; + for i in 0..(s % n) as usize { + ans[i] += 1; } - res + + ans } } diff --git a/solution/2000-2099/2028.Find Missing Observations/Solution.ts b/solution/2000-2099/2028.Find Missing Observations/Solution.ts index 0378556d2765b..179b4c3c6ca6d 100644 --- a/solution/2000-2099/2028.Find Missing Observations/Solution.ts +++ b/solution/2000-2099/2028.Find Missing Observations/Solution.ts @@ -1,29 +1,12 @@ 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) { + const m = rolls.length; + const s = (n + m) * mean - rolls.reduce((a, b) => a + b, 0); + if (s > n * 6 || s < n) { 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; - } + const ans: number[] = Array(n).fill((s / n) | 0); + for (let i = 0; i < s % n; ++i) { + ans[i]++; } - return res; + return ans; }