-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
45 lines (39 loc) · 1.03 KB
/
Solution.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
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> missingRolls(vector<int>& rolls, int mean, int n) {
int m = rolls.size();
int expectedSum = (n + m) * mean;
for (auto const& num : rolls) {
expectedSum -= num;
}
if (expectedSum > n * 6 || expectedSum < n * 1) {
return {};
}
vector<int> result(n, 0);
// Pigeonhole principle
// Place at least this many pigeons into each hole first
// Remainder can be distributed to the holes
int pigeonRoll = expectedSum / n;
expectedSum -= n * pigeonRoll;
for (int i = 0; i < n; ++i) {
int additionalPigeons = 0;
if (expectedSum) {
additionalPigeons = min(6 - pigeonRoll, expectedSum);
expectedSum -= additionalPigeons;
}
result[i] = pigeonRoll + additionalPigeons;
}
return result;
}
};