Skip to content

feat: update solutions to lc problem: No.2028 #2917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 25 additions & 56 deletions solution/2000-2099/2028.Find Missing Observations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$

<!-- tabs:start -->

Expand Down Expand Up @@ -137,11 +137,14 @@ 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 {};
int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0);
if (s > n * 6 || s < n) {
return {};
}
vector<int> 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;
}
};
Expand Down Expand Up @@ -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;
}
```

Expand All @@ -209,36 +195,19 @@ function missingRolls(rolls: number[], mean: number, n: number): number[] {
```rust
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 {
let m = rolls.len() as i32;
let s = (n + m) * mean - rolls.iter().sum::<i32>();

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
}
}
```
Expand Down
83 changes: 26 additions & 57 deletions solution/2000-2099/2028.Find Missing Observations/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -127,11 +127,14 @@ 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 {};
int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0);
if (s > n * 6 || s < n) {
return {};
}
vector<int> 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;
}
};
Expand Down Expand Up @@ -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;
}
```

Expand All @@ -199,36 +185,19 @@ function missingRolls(rolls: number[], mean: number, n: number): number[] {
```rust
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 {
let m = rolls.len() as i32;
let s = (n + m) * mean - rolls.iter().sum::<i32>();

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
}
}
```
Expand Down
11 changes: 7 additions & 4 deletions solution/2000-2099/2028.Find Missing Observations/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ 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 {};
int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0);
if (s > n * 6 || s < n) {
return {};
}
vector<int> 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;
}
};
35 changes: 9 additions & 26 deletions solution/2000-2099/2028.Find Missing Observations/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
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 {
let m = rolls.len() as i32;
let s = (n + m) * mean - rolls.iter().sum::<i32>();

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
}
}
31 changes: 7 additions & 24 deletions solution/2000-2099/2028.Find Missing Observations/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading