-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-missing-observations.cpp
47 lines (42 loc) · 1.38 KB
/
find-missing-observations.cpp
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
40
41
42
43
44
45
46
47
// Time: O(n)
// Space: O(1)
class Solution {
public:
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
static const int MAX_V = 6;
static const int MIN_V = 1;
const int total = accumulate(cbegin(rolls), cend(rolls), 0);
const int missing = mean * (n + size(rolls)) - total;
if (missing < MIN_V * n || missing > MAX_V * n) {
return {};
}
const int q = missing / n;
const int r = missing % n;
vector<int> result(n);
for (int i = 0; i < size(result); ++i) {
result[i] = q + int(i < r);
}
return result;
}
};
// Time: O(n)
// Space: O(1)
class Solution2 {
public:
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
static const int MAX_V = 6;
static const int MIN_V = 1;
const int total = accumulate(cbegin(rolls), cend(rolls), 0);
const int missing = mean * (n + size(rolls)) - total;
if (missing < MIN_V * n || missing > MAX_V * n) {
return {};
}
const int q = (missing - MIN_V * n) / (MAX_V - MIN_V);
const int r = (missing - MIN_V * n) % (MAX_V - MIN_V);
vector<int> result(n);
for (int i = 0; i < size(result); ++i) {
result[i] = (i < q) ? MAX_V : ((i == q) ? MIN_V + r : MIN_V);
}
return result;
}
};