Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 46e8823

Browse files
committed
Add solution #2028
1 parent ec82cc1 commit 46e8823

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,695 LeetCode solutions in JavaScript
1+
# 1,696 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1555,6 +1555,7 @@
15551555
2024|[Maximize the Confusion of an Exam](./solutions/2024-maximize-the-confusion-of-an-exam.js)|Medium|
15561556
2025|[Maximum Number of Ways to Partition an Array](./solutions/2025-maximum-number-of-ways-to-partition-an-array.js)|Hard|
15571557
2027|[Minimum Moves to Convert String](./solutions/2027-minimum-moves-to-convert-string.js)|Easy|
1558+
2028|[Find Missing Observations](./solutions/2028-find-missing-observations.js)|Medium|
15581559
2033|[Minimum Operations to Make a Uni-Value Grid](./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js)|Medium|
15591560
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
15601561
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 2028. Find Missing Observations
3+
* https://leetcode.com/problems/find-missing-observations/
4+
* Difficulty: Medium
5+
*
6+
* You have observations of n + m 6-sided dice rolls with each face numbered from 1 to 6. n
7+
* of the observations went missing, and you only have the observations of m rolls.
8+
* Fortunately, you have also calculated the average value of the n + m rolls.
9+
*
10+
* You are given an integer array rolls of length m where rolls[i] is the value of the ith
11+
* observation. You are also given the two integers mean and n.
12+
*
13+
* Return an array of length n containing the missing observations such that the average
14+
* value of the n + m rolls is exactly mean. If there are multiple valid answers, return
15+
* any of them. If no such array exists, return an empty array.
16+
*
17+
* The average value of a set of k numbers is the sum of the numbers divided by k.
18+
*
19+
* Note that mean is an integer, so the sum of the n + m rolls should be divisible by n + m.
20+
*/
21+
22+
/**
23+
* @param {number[]} rolls
24+
* @param {number} mean
25+
* @param {number} n
26+
* @return {number[]}
27+
*/
28+
var missingRolls = function(rolls, mean, n) {
29+
const totalRolls = rolls.length + n;
30+
const targetSum = mean * totalRolls;
31+
const currentSum = rolls.reduce((sum, roll) => sum + roll, 0);
32+
const missingSum = targetSum - currentSum;
33+
34+
if (missingSum < n || missingSum > 6 * n) return [];
35+
36+
const baseValue = Math.floor(missingSum / n);
37+
const remainder = missingSum % n;
38+
const result = [];
39+
40+
for (let i = 0; i < n; i++) {
41+
if (i < remainder) {
42+
result.push(baseValue + 1);
43+
} else {
44+
result.push(baseValue);
45+
}
46+
}
47+
48+
return result;
49+
};

0 commit comments

Comments
 (0)