Find Missing Observations — LeetCode 2028 Python Solution
- Problem
- #2028
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You have observations of n + m 6-sided dice rolls with each face numbered from 1 to 6. n of the observations went missing, and you only have the observations of m rolls.
Example
- Input
- rolls = [3,2,4,3], mean = 4, n = 2
- Output
- [6,6]
- Explanation
- The mean of all n + m rolls is (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4.
Python solution
class Solution:
def missingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
m = len(rolls)
s = (n + m) * mean - sum(rolls)
if s > n * 6 or s < n:
return []
ans = [s // n] * n
for i in range(s % n):
ans[i] += 1
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n + m), where n and m are the number of missing numbers and known numbers, respectively |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2028. Find Missing Observations is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2028. Find Missing Observations?
- LeetCode 2028. Find Missing Observations is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2028. Find Missing Observations?
- The Python solution on this page runs in O(n + m), where n and m are the number of missing numbers and known numbers, respectively.
- What is the space complexity of LeetCode 2028. Find Missing Observations?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2028. Find Missing Observations cover?
- LeetCode 2028. Find Missing Observations is tagged Array, Math and Simulation on LeetCode.