Skip to content

Commit c9dcbb2

Browse files
committed
2028 找出缺失的观测数据
1 parent ce67961 commit c9dcbb2

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.leetcode.editor.cn;
2+
3+
//现有一份 n + m 次投掷单个 六面 骰子的观测数据,骰子的每个面从 1 到 6 编号。观测数据中缺失了 n 份,你手上只拿到剩余 m 次投掷的数据。幸好
4+
//你有之前计算过的这 n + m 次投掷数据的 平均值 。
5+
//
6+
// 给你一个长度为 m 的整数数组 rolls ,其中 rolls[i] 是第 i 次观测的值。同时给你两个整数 mean 和 n 。
7+
//
8+
// 返回一个长度为 n 的数组,包含所有缺失的观测数据,且满足这 n + m 次投掷的 平均值 是 mean 。如果存在多组符合要求的答案,只需要返回其中任意
9+
//一组即可。如果不存在答案,返回一个空数组。
10+
//
11+
// k 个数字的 平均值 为这些数字求和后再除以 k 。
12+
//
13+
// 注意 mean 是一个整数,所以 n + m 次投掷的总和需要被 n + m 整除。
14+
//
15+
//
16+
//
17+
// 示例 1:
18+
//
19+
//
20+
//输入:rolls = [3,2,4,3], mean = 4, n = 2
21+
//输出:[6,6]
22+
//解释:所有 n + m 次投掷的平均值是 (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4 。
23+
//
24+
//
25+
// 示例 2:
26+
//
27+
//
28+
//输入:rolls = [1,5,6], mean = 3, n = 4
29+
//输出:[2,3,2,2]
30+
//解释:所有 n + m 次投掷的平均值是 (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3 。
31+
//
32+
//
33+
// 示例 3:
34+
//
35+
//
36+
//输入:rolls = [1,2,3,4], mean = 6, n = 4
37+
//输出:[]
38+
//解释:无论丢失的 4 次数据是什么,平均值都不可能是 6 。
39+
//
40+
//
41+
// 示例 4:
42+
//
43+
//
44+
//输入:rolls = [1], mean = 3, n = 1
45+
//输出:[5]
46+
//解释:所有 n + m 次投掷的平均值是 (1 + 5) / 2 = 3 。
47+
//
48+
//
49+
//
50+
//
51+
// 提示:
52+
//
53+
//
54+
// m == rolls.length
55+
// 1 <= n, m <= 10⁵
56+
// 1 <= rolls[i], mean <= 6
57+
//
58+
// Related Topics 数组 数学 模拟 👍 47 👎 0
59+
60+
61+
import java.util.Arrays;
62+
63+
/**
64+
* 2028 找出缺失的观测数据
65+
*
66+
* @author shang.liang
67+
* @date 2022-03-27 19:59:17
68+
*/
69+
public class FindMissingObservations {
70+
public static void main(String[] args) {
71+
Solution soution = new FindMissingObservations().new Solution();
72+
73+
}
74+
75+
//leetcode submit region begin(Prohibit modification and deletion)
76+
class Solution {
77+
public int[] missingRolls(int[] rolls, int mean, int n) {
78+
79+
int[] res = new int[n];
80+
81+
int sum = Arrays.stream(rolls).sum();
82+
int nTotal = mean * rolls.length - sum + n * mean;
83+
84+
if (nTotal < n || nTotal > 6 * n) {
85+
return new int[0];
86+
}
87+
88+
int index = 0;
89+
for (int i = 0; i < nTotal; i++) {
90+
if (index == n) {
91+
index = 0;
92+
}
93+
res[index++]++;
94+
95+
}
96+
97+
return res;
98+
}
99+
}
100+
//leetcode submit region end(Prohibit modification and deletion)
101+
102+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<p>现有一份 <code>n + m</code>&nbsp;次投掷单个<strong> 六面</strong> 骰子的观测数据,骰子的每个面从 <code>1</code> 到 <code>6</code> 编号。观测数据中缺失了 <code>n</code> 份,你手上只拿到剩余&nbsp;<code>m</code> 次投掷的数据。幸好你有之前计算过的这 <code>n + m</code> 次投掷数据的 <strong>平均值</strong> 。</p>
2+
3+
<p>给你一个长度为 <code>m</code> 的整数数组 <code>rolls</code> ,其中&nbsp;<code>rolls[i]</code> 是第 <code>i</code> 次观测的值。同时给你两个整数 <code>mean</code> 和 <code>n</code> 。</p>
4+
5+
<p>返回一个长度为<em> </em><code>n</code><em> </em>的数组,包含所有缺失的观测数据,且满足这<em> </em><code>n + m</code><em> </em>次投掷的 <strong>平均值</strong> 是<em> </em><code>mean</code> 。如果存在多组符合要求的答案,只需要返回其中任意一组即可。如果不存在答案,返回一个空数组。</p>
6+
7+
<p><code>k</code>&nbsp;个数字的 <strong>平均值</strong> 为这些数字求和后再除以&nbsp;<code>k</code> 。</p>
8+
9+
<p>注意 <code>mean</code> 是一个整数,所以 <code>n + m</code> 次投掷的总和需要被&nbsp;<code>n + m</code>&nbsp;整除。</p>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre>
16+
<strong>输入:</strong>rolls = [3,2,4,3], mean = 4, n = 2
17+
<strong>输出:</strong>[6,6]
18+
<strong>解释:</strong>所有 n + m 次投掷的平均值是 (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4 。
19+
</pre>
20+
21+
<p><strong>示例 2:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>rolls = [1,5,6], mean = 3, n = 4
25+
<strong>输出:</strong>[2,3,2,2]
26+
<strong>解释:</strong>所有 n + m 次投掷的平均值是 (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3 。
27+
</pre>
28+
29+
<p><strong>示例 3:</strong></p>
30+
31+
<pre>
32+
<strong>输入:</strong>rolls = [1,2,3,4], mean = 6, n = 4
33+
<strong>输出:</strong>[]
34+
<strong>解释:</strong>无论丢失的 4 次数据是什么,平均值都不可能是 6 。
35+
</pre>
36+
37+
<p><strong>示例 4:</strong></p>
38+
39+
<pre>
40+
<strong>输入:</strong>rolls = [1], mean = 3, n = 1
41+
<strong>输出:</strong>[5]
42+
<strong>解释:</strong>所有 n + m 次投掷的平均值是 (1 + 5) / 2 = 3 。
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>m == rolls.length</code></li>
51+
<li><code>1 &lt;= n, m &lt;= 10<sup>5</sup></code></li>
52+
<li><code>1 &lt;= rolls[i], mean &lt;= 6</code></li>
53+
</ul>
54+
<div><div>Related Topics</div><div><li>数组</li><li>数学</li><li>模拟</li></div></div><br><div><li>👍 47</li><li>👎 0</li></div>

0 commit comments

Comments
 (0)