classSolution { publicint[] missingRolls(int[] rolls, int mean, int n) { intm= rolls.length; intsum= mean * (n + m); intmissingSum= sum; for (int roll : rolls) { missingSum -= roll; } if (missingSum < n || missingSum > 6 * n) { returnnewint[0]; } intquotient= missingSum / n, remainder = missingSum % n; int[] missing = newint[n]; for (inti=0; i < n; i++) { missing[i] = quotient + (i < remainder ? 1 : 0); } return missing; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicint[] MissingRolls(int[] rolls, int mean, int n) { int m = rolls.Length; int sum = mean * (n + m); int missingSum = sum; foreach (int roll in rolls) { missingSum -= roll; } if (missingSum < n || missingSum > 6 * n) { returnnewint[0]; } int quotient = missingSum / n, remainder = missingSum % n; int[] missing = newint[n]; for (int i = 0; i < n; i++) { missing[i] = quotient + (i < remainder ? 1 : 0); } return missing; } }
classSolution { public: vector<int> missingRolls(vector<int>& rolls, int mean, int n){ int m = rolls.size(); int sum = mean * (n + m); int missingSum = sum; for (int & roll : rolls) { missingSum -= roll; } if (missingSum < n || missingSum > 6 * n) { return {}; } int quotient = missingSum / n, remainder = missingSum % n; vector<int> missing(n); for (int i = 0; i < n; i++) { missing[i] = quotient + (i < remainder ? 1 : 0); } return missing; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int* missingRolls(int* rolls, int rollsSize, int mean, int n, int* returnSize){ int m = rollsSize; int sum = mean * (n + m); int missingSum = sum; for (int i = 0; i < m; i++) { missingSum -= rolls[i]; } if (missingSum < n || missingSum > 6 * n) { *returnSize = 0; returnNULL; } int quotient = missingSum / n, remainder = missingSum % n; int * missing = (int *)malloc(sizeof(int) * n); for (int i = 0; i < n; i++) { missing[i] = quotient + (i < remainder ? 1 : 0); } *returnSize = n; return missing; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var missingRolls = function(rolls, mean, n) { const m = rolls.length; const sum = mean * (n + m); let missingSum = sum; for (const roll of rolls) { missingSum -= roll; } if (missingSum < n || missingSum > 6 * n) { return []; } const quotient = Math.floor(missingSum / n), remainder = missingSum % n; const missing = newArray(n).fill(0); for (let i = 0; i < n; i++) { missing[i] = quotient + (i < remainder ? 1 : 0); } return missing; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcmissingRolls(rolls []int, mean, n int) []int { missingSum := mean * (n + len(rolls)) for _, roll := range rolls { missingSum -= roll } if missingSum < n || missingSum > n*6 { returnnil }
quotient, remainder := missingSum/n, missingSum%n ans := make([]int, n) for i := range ans { ans[i] = quotient if i < remainder { ans[i]++ } } return ans }