Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bef415f

Browse files
Merge pull request #330 from cheehwatang/add-2028-FindMissingObservations
Add 2028. Find Missing Observations
2 parents 3fbfd5f + 17831d7 commit bef415f

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

‎README.md‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">September 9th</td>
32+
<td>2028. <a href="https://leetcode.com/problems/find-missing-observations/">Find Missing Observations</a></td>
33+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
34+
<td align="center">
35+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/2028.%20Find%20Missing%20Observations/FindMissingObservations.java">Math</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>,
39+
<a href="#math">Math</a>,
40+
<a href="#simulation">Simulation</a>
41+
</td>
42+
</tr>
3043
<tr>
3144
<td align="center">September 8th</td>
3245
<td>1155. <a href="https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/">Number of Dice Rolls With Target Sum</a></td>
@@ -74,19 +87,6 @@
7487
<a href="#math">Math</a>
7588
</td>
7689
</tr>
77-
<tr>
78-
<td align="center">September 4th</td>
79-
<td>923. <a href="https://leetcode.com/problems/3sum-with-multiplicity/">3Sum With Multiplicity</a></td>
80-
<td align="center">$\text{\color{Dandelion}Medium}$</td>
81-
<td align="center">
82-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/923.%203Sum%20With%20Multiplicity/ThreeSumWithMultiplicity_Sorting_TwoPointers.java">Sorting & Two Pointers</a>
83-
</td>
84-
<td align="center">
85-
<a href="#array">Array</a>,
86-
<a href="#sorting">Sorting</a>,
87-
<a href="#two-pointers">Two Pointers</a>
88-
</td>
89-
</tr>
9090
</table>
9191
</br>
9292
<hr>
@@ -1593,6 +1593,20 @@
15931593
</td>
15941594
<td></td>
15951595
</tr>
1596+
<tr>
1597+
<td align="center">2028</td>
1598+
<td><a href="https://leetcode.com/problems/find-missing-observations/">Find Missing Observations</a></td>
1599+
<td align="center">
1600+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/2028.%20Find%20Missing%20Observations/FindMissingObservations.java">Java</a>
1601+
</td>
1602+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
1603+
<td align="center">
1604+
<a href="#array">Array</a>,
1605+
<a href="#math">Math</a>,
1606+
<a href="#simulation">Simulation</a>
1607+
</td>
1608+
<td></td>
1609+
</tr>
15961610
<tr>
15971611
<td align="center">2131</td>
15981612
<td><a href="https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/">Longest Palindrome by Concatenating Two Letter Words</a></td>
@@ -6410,6 +6424,20 @@
64106424
</td>
64116425
<td></td>
64126426
</tr>
6427+
<tr>
6428+
<td align="center">2028</td>
6429+
<td><a href="https://leetcode.com/problems/find-missing-observations/">Find Missing Observations</a></td>
6430+
<td align="center">
6431+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/2028.%20Find%20Missing%20Observations/FindMissingObservations.java">Java</a>
6432+
</td>
6433+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
6434+
<td align="center">
6435+
<a href="#array">Array</a>,
6436+
<a href="#math">Math</a>,
6437+
<a href="#simulation">Simulation</a>
6438+
</td>
6439+
<td></td>
6440+
</tr>
64136441
<tr>
64146442
<td align="center">2169</td>
64156443
<td><a href="https://leetcode.com/problems/count-operations-to-obtain-zero/">Count Operations to Obtain Zero</a></td>
@@ -7236,6 +7264,20 @@
72367264
</td>
72377265
<td></td>
72387266
</tr>
7267+
<tr>
7268+
<td align="center">2028</td>
7269+
<td><a href="https://leetcode.com/problems/find-missing-observations/">Find Missing Observations</a></td>
7270+
<td align="center">
7271+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/2028.%20Find%20Missing%20Observations/FindMissingObservations.java">Java</a>
7272+
</td>
7273+
<td align="center">$\text{\color{Dandelion}Medium}$</td>
7274+
<td align="center">
7275+
<a href="#array">Array</a>,
7276+
<a href="#math">Math</a>,
7277+
<a href="#simulation">Simulation</a>
7278+
</td>
7279+
<td></td>
7280+
</tr>
72397281
<tr>
72407282
<td align="center">2169</td>
72417283
<td><a href="https://leetcode.com/problems/count-operations-to-obtain-zero/">Count Operations to Obtain Zero</a></td>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.cheehwatang.leetcode;
2+
3+
// Time Complexity : O(m + n),
4+
// where 'm' is the length of 'rolls', and 'n' is 'n'.
5+
// We traverse 'rolls' to get the sum, and traverse the result array of size 'n' to populate the missing observations.
6+
//
7+
// Space Complexity : O(n),
8+
// where 'n' is the input variable 'n'.
9+
// The result has a size of 'n'.
10+
11+
public class FindMissingObservations {
12+
13+
// Approach:
14+
// Arithmetically determine the sum of the 'n' rolls by first getting the sum of the 'm' rolls.
15+
// Then determine the average roll for the 'n' rolls and its remainders to assign the correct rolls.
16+
// If sum = 10, n = 4, the average roll is 10 / 4 == 2, with remainder of 2. Thus, we know to assign [3,3,2,2]
17+
18+
public int[] missingRolls(int[] rolls, int mean, int n) {
19+
// Determine the sum of 'n' rolls.
20+
int sumM = 0;
21+
for (int roll : rolls) sumM += roll;
22+
int sumN = mean * (rolls.length + n) - sumM;
23+
24+
// If the sum for 'n' rolls is:
25+
// - less than all 'n' rolls of 1, or
26+
// - greater than all 'n' rolls of 6,
27+
// then we know it is impossible to get the target with 'n' rolls.
28+
if (sumN < n || sumN > 6 * n) return new int[0];
29+
30+
// Determine the 'floor' number, and the 'remainder' for the number of rolls to add 1 to the 'floor'.
31+
int floor = sumN / n;
32+
int remainder = sumN % n;
33+
int[] result = new int[n];
34+
for (int i = 0; i < n; i++) {
35+
result[i] = remainder-- > 0 ? floor + 1 : floor;
36+
}
37+
return result;
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /