Skip to content

Commit d36bce4

Browse files
authored
feat: update solutions to lc problem: No.2028 (#2917)
No.2028.Find Missing Observations
1 parent a8def68 commit d36bce4

File tree

5 files changed

+74
-167
lines changed

5 files changed

+74
-167
lines changed

solution/2000-2099/2028.Find Missing Observations/README.md

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ tags:
8282

8383
### 方法一:构造
8484

85-
根据题目描述,所有数字之和为 $(n + m) \times mean$,已知的数字之和为 `sum(rolls)`,那么缺失的数字之和为 $s = (n + m) \times mean - sum(rolls)$。
85+
根据题目描述,所有数字之和为 $(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]$。
8686

8787
如果 $s \gt n \times 6$ 或者 $s \lt n$,说明不存在满足条件的答案,返回空数组。
8888

8989
否则,我们可以将 $s$ 平均分配到 $n$ 个数字上,即每个数字的值为 $s / n$,其中 $s \bmod n$ 个数字的值再加上 $1$。
9090

91-
时间复杂度 $O(n + m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为缺失的数字个数和已知的数字个数。
91+
时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别为缺失的数字个数和已知的数字个数。忽略答案的空间消耗,空间复杂度 $O(1)$
9292

9393
<!-- tabs:start -->
9494

@@ -137,11 +137,14 @@ class Solution {
137137
public:
138138
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
139139
int m = rolls.size();
140-
int s = (n + m) * mean;
141-
for (int& v : rolls) s -= v;
142-
if (s > n * 6 || s < n) return {};
140+
int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0);
141+
if (s > n * 6 || s < n) {
142+
return {};
143+
}
143144
vector<int> ans(n, s / n);
144-
for (int i = 0; i < s % n; ++i) ++ans[i];
145+
for (int i = 0; i < s % n; ++i) {
146+
++ans[i];
147+
}
145148
return ans;
146149
}
147150
};
@@ -174,33 +177,16 @@ func missingRolls(rolls []int, mean int, n int) []int {
174177

175178
```ts
176179
function missingRolls(rolls: number[], mean: number, n: number): number[] {
177-
const len = rolls.length + n;
178-
const sum = rolls.reduce((p, v) => p + v);
179-
const max = n * 6;
180-
const min = n;
181-
if ((sum + max) / len < mean || (sum + min) / len > mean) {
180+
const m = rolls.length;
181+
const s = (n + m) * mean - rolls.reduce((a, b) => a + b, 0);
182+
if (s > n * 6 || s < n) {
182183
return [];
183184
}
184-
185-
const res = new Array(n);
186-
for (let i = min; i <= max; i++) {
187-
if ((sum + i) / len === mean) {
188-
const num = Math.floor(i / n);
189-
res.fill(num);
190-
let count = i - n * num;
191-
let j = 0;
192-
while (count != 0) {
193-
if (res[j] === 6) {
194-
j++;
195-
} else {
196-
res[j]++;
197-
count--;
198-
}
199-
}
200-
break;
201-
}
185+
const ans: number[] = Array(n).fill((s / n) | 0);
186+
for (let i = 0; i < s % n; ++i) {
187+
ans[i]++;
202188
}
203-
return res;
189+
return ans;
204190
}
205191
```
206192

@@ -209,36 +195,19 @@ function missingRolls(rolls: number[], mean: number, n: number): number[] {
209195
```rust
210196
impl Solution {
211197
pub fn missing_rolls(rolls: Vec<i32>, mean: i32, n: i32) -> Vec<i32> {
212-
let n = n as usize;
213-
let mean = mean as usize;
214-
let len = rolls.len() + n;
215-
let sum: i32 = rolls.iter().sum();
216-
let sum = sum as usize;
217-
let max = n * 6;
218-
let min = n;
219-
if sum + max < mean * len || sum + min > mean * len {
198+
let m = rolls.len() as i32;
199+
let s = (n + m) * mean - rolls.iter().sum::<i32>();
200+
201+
if s > n * 6 || s < n {
220202
return vec![];
221203
}
222204

223-
let mut res = vec![0; n];
224-
for i in min..=max {
225-
if (sum + i) / len == mean {
226-
let num = i / n;
227-
res.fill(num as i32);
228-
let mut count = i - n * num;
229-
let mut j = 0;
230-
while count != 0 {
231-
if res[j] == 6 {
232-
j += 1;
233-
} else {
234-
res[j] += 1;
235-
count -= 1;
236-
}
237-
}
238-
break;
239-
}
205+
let mut ans = vec![s / n; n as usize];
206+
for i in 0..(s % n) as usize {
207+
ans[i] += 1;
240208
}
241-
res
209+
210+
ans
242211
}
243212
}
244213
```

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

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ tags:
7272

7373
### Solution 1: Construction
7474

75-
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)$.
75+
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]$.
7676

77-
If $s > n \times 6$ or $s < n$, it means there is no answer that satisfies the conditions, so return an empty array.
77+
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.
7878

7979
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$.
8080

81-
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.
81+
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)$.
8282

8383
<!-- tabs:start -->
8484

@@ -127,11 +127,14 @@ class Solution {
127127
public:
128128
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
129129
int m = rolls.size();
130-
int s = (n + m) * mean;
131-
for (int& v : rolls) s -= v;
132-
if (s > n * 6 || s < n) return {};
130+
int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0);
131+
if (s > n * 6 || s < n) {
132+
return {};
133+
}
133134
vector<int> ans(n, s / n);
134-
for (int i = 0; i < s % n; ++i) ++ans[i];
135+
for (int i = 0; i < s % n; ++i) {
136+
++ans[i];
137+
}
135138
return ans;
136139
}
137140
};
@@ -164,33 +167,16 @@ func missingRolls(rolls []int, mean int, n int) []int {
164167

165168
```ts
166169
function missingRolls(rolls: number[], mean: number, n: number): number[] {
167-
const len = rolls.length + n;
168-
const sum = rolls.reduce((p, v) => p + v);
169-
const max = n * 6;
170-
const min = n;
171-
if ((sum + max) / len < mean || (sum + min) / len > mean) {
170+
const m = rolls.length;
171+
const s = (n + m) * mean - rolls.reduce((a, b) => a + b, 0);
172+
if (s > n * 6 || s < n) {
172173
return [];
173174
}
174-
175-
const res = new Array(n);
176-
for (let i = min; i <= max; i++) {
177-
if ((sum + i) / len === mean) {
178-
const num = Math.floor(i / n);
179-
res.fill(num);
180-
let count = i - n * num;
181-
let j = 0;
182-
while (count != 0) {
183-
if (res[j] === 6) {
184-
j++;
185-
} else {
186-
res[j]++;
187-
count--;
188-
}
189-
}
190-
break;
191-
}
175+
const ans: number[] = Array(n).fill((s / n) | 0);
176+
for (let i = 0; i < s % n; ++i) {
177+
ans[i]++;
192178
}
193-
return res;
179+
return ans;
194180
}
195181
```
196182

@@ -199,36 +185,19 @@ function missingRolls(rolls: number[], mean: number, n: number): number[] {
199185
```rust
200186
impl Solution {
201187
pub fn missing_rolls(rolls: Vec<i32>, mean: i32, n: i32) -> Vec<i32> {
202-
let n = n as usize;
203-
let mean = mean as usize;
204-
let len = rolls.len() + n;
205-
let sum: i32 = rolls.iter().sum();
206-
let sum = sum as usize;
207-
let max = n * 6;
208-
let min = n;
209-
if sum + max < mean * len || sum + min > mean * len {
188+
let m = rolls.len() as i32;
189+
let s = (n + m) * mean - rolls.iter().sum::<i32>();
190+
191+
if s > n * 6 || s < n {
210192
return vec![];
211193
}
212194

213-
let mut res = vec![0; n];
214-
for i in min..=max {
215-
if (sum + i) / len == mean {
216-
let num = i / n;
217-
res.fill(num as i32);
218-
let mut count = i - n * num;
219-
let mut j = 0;
220-
while count != 0 {
221-
if res[j] == 6 {
222-
j += 1;
223-
} else {
224-
res[j] += 1;
225-
count -= 1;
226-
}
227-
}
228-
break;
229-
}
195+
let mut ans = vec![s / n; n as usize];
196+
for i in 0..(s % n) as usize {
197+
ans[i] += 1;
230198
}
231-
res
199+
200+
ans
232201
}
233202
}
234203
```

solution/2000-2099/2028.Find Missing Observations/Solution.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ class Solution {
22
public:
33
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
44
int m = rolls.size();
5-
int s = (n + m) * mean;
6-
for (int& v : rolls) s -= v;
7-
if (s > n * 6 || s < n) return {};
5+
int s = (n + m) * mean - accumulate(rolls.begin(), rolls.end(), 0);
6+
if (s > n * 6 || s < n) {
7+
return {};
8+
}
89
vector<int> ans(n, s / n);
9-
for (int i = 0; i < s % n; ++i) ++ans[i];
10+
for (int i = 0; i < s % n; ++i) {
11+
++ans[i];
12+
}
1013
return ans;
1114
}
1215
};
Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
impl Solution {
22
pub fn missing_rolls(rolls: Vec<i32>, mean: i32, n: i32) -> Vec<i32> {
3-
let n = n as usize;
4-
let mean = mean as usize;
5-
let len = rolls.len() + n;
6-
let sum: i32 = rolls.iter().sum();
7-
let sum = sum as usize;
8-
let max = n * 6;
9-
let min = n;
10-
if sum + max < mean * len || sum + min > mean * len {
3+
let m = rolls.len() as i32;
4+
let s = (n + m) * mean - rolls.iter().sum::<i32>();
5+
6+
if s > n * 6 || s < n {
117
return vec![];
128
}
139

14-
let mut res = vec![0; n];
15-
for i in min..=max {
16-
if (sum + i) / len == mean {
17-
let num = i / n;
18-
res.fill(num as i32);
19-
let mut count = i - n * num;
20-
let mut j = 0;
21-
while count != 0 {
22-
if res[j] == 6 {
23-
j += 1;
24-
} else {
25-
res[j] += 1;
26-
count -= 1;
27-
}
28-
}
29-
break;
30-
}
10+
let mut ans = vec![s / n; n as usize];
11+
for i in 0..(s % n) as usize {
12+
ans[i] += 1;
3113
}
32-
res
14+
15+
ans
3316
}
3417
}
Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
function missingRolls(rolls: number[], mean: number, n: number): number[] {
2-
const len = rolls.length + n;
3-
const sum = rolls.reduce((p, v) => p + v);
4-
const max = n * 6;
5-
const min = n;
6-
if ((sum + max) / len < mean || (sum + min) / len > mean) {
2+
const m = rolls.length;
3+
const s = (n + m) * mean - rolls.reduce((a, b) => a + b, 0);
4+
if (s > n * 6 || s < n) {
75
return [];
86
}
9-
10-
const res = new Array(n);
11-
for (let i = min; i <= max; i++) {
12-
if ((sum + i) / len === mean) {
13-
const num = Math.floor(i / n);
14-
res.fill(num);
15-
let count = i - n * num;
16-
let j = 0;
17-
while (count != 0) {
18-
if (res[j] === 6) {
19-
j++;
20-
} else {
21-
res[j]++;
22-
count--;
23-
}
24-
}
25-
break;
26-
}
7+
const ans: number[] = Array(n).fill((s / n) | 0);
8+
for (let i = 0; i < s % n; ++i) {
9+
ans[i]++;
2710
}
28-
return res;
11+
return ans;
2912
}

0 commit comments

Comments
 (0)