-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_2028_FindMissingObservations.cpp
70 lines (55 loc) · 1.69 KB
/
LC_2028_FindMissingObservations.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
https://leetcode.com/problems/find-missing-observations/
2028. Find Missing Observations
*/
class Solution {
public:
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
int m = rolls.size();
int sum_m = accumulate(rolls.begin(), rolls.end(), 0);
vector<int> ans;
int sum_n = mean*(n+m) - sum_m;
if(sum_n > (n*6) || sum_n < n)
return ans;
// if( q <= 0 || q+bool(rem)>6)
// return ans; // not possible to make n array
int q = sum_n / n;
int rem = sum_n % n;
ans.resize(n, q);
// for(int i=0; i<n; i++)
// ans.push_back(q);
for(int i=0; i<rem; i++)
ans[i] += 1;
// cout<<sum_m<<" "<<sum_n<<" "<<q<<" "<<rem<<endl;
return ans;
}
vector<int> missingRolls_(vector<int>& rolls, int mean, int n) {
int m = rolls.size();
int sum_m = accumulate(rolls.begin(), rolls.end(), 0);
vector<int> ans;
int sum_n = mean*(n+m) - sum_m;
if(sum_n > (n*6) || sum_n < n)
return ans;
// if( q <= 0 || q+bool(rem)>6)
// return ans; // not possible to make n array
ans.resize(n, 1);
int i=0;
sum_n -= n;
while(sum_n)
{
if(sum_n >= 5)
{
ans[i] += 5;
sum_n -= 5;
}
else
{
ans[i] += sum_n;
sum_n=0;
}
i++;
}
// cout<<sum_m<<" "<<sum_n<<" "<<q<<" "<<rem<<endl;
return ans;
}
};