forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-missing-observations.py
39 lines (36 loc) · 1.01 KB
/
find-missing-observations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Time: O(n)
# Space: O(1)
class Solution(object):
def missingRolls(self, rolls, mean, n):
"""
:type rolls: List[int]
:type mean: int
:type n: int
:rtype: List[int]
"""
MAX_V = 6
MIN_V = 1
total = sum(rolls)
missing = mean*(n+len(rolls))-total
if missing < MIN_V*n or missing > MAX_V*n:
return []
q, r = divmod(missing, n)
return [q+int(i < r) for i in xrange(n)]
# Time: O(n)
# Space: O(1)
class Solution2(object):
def missingRolls(self, rolls, mean, n):
"""
:type rolls: List[int]
:type mean: int
:type n: int
:rtype: List[int]
"""
MAX_V = 6
MIN_V = 1
total = sum(rolls)
missing = mean*(n+len(rolls))-total
if missing < MIN_V*n or missing > MAX_V*n:
return []
q, r = divmod(missing-MIN_V*n, (MAX_V-MIN_V))
return [MAX_V if i < q else MIN_V+r if i == q else MIN_V for i in xrange(n)]