1283. Find the Smallest Divisor Given a Threshold
Task text is translated from Russian for the selected interface language. Code is left unchanged.
Дан array целых чисел nums и integer threshold. Мы выберем положительный целый делитель, разделим все elementы arrayа на него и суммируем результат деления. find наименьший делитель, такой что результат, упомянутый выше, меньше или равен threshold.
Каждый результат деления округляется до ближайшего большего целого числа. (НаExample: 7/3 = 3 и 10/2 = 5).
C# solution
matched/originalpublic class Solution {
public int SmallestDivisor(int[] nums, int threshold) {
int maxElement = nums.Max();
for (int divisor = 1; divisor <= maxElement; divisor++) {
int sumOfDivisionResults = 0;
bool thresholdExceeded = true;
foreach (int num in nums) {
sumOfDivisionResults += (num + divisor - 1) / divisor;
if (sumOfDivisionResults > threshold) {
thresholdExceeded = false;
break;
}
}
if (thresholdExceeded) {
return divisor;
}
}
return -1;
}
}
C++ solution
auto-draft, review before submit#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int SmallestDivisor(vector<int>& nums, int threshold) {
int maxElement = nums.Max();
for (int divisor = 1; divisor <= maxElement; divisor++) {
int sumOfDivisionResults = 0;
bool thresholdExceeded = true;
foreach (int num in nums) {
sumOfDivisionResults += (num + divisor - 1) / divisor;
if (sumOfDivisionResults > threshold) {
thresholdExceeded = false;
break;
}
}
if (thresholdExceeded) {
return divisor;
}
}
return -1;
}
}
Java solution
matched/originalclass Solution {
public int smallestDivisor(int[] nums, int threshold) {
int maxElement = Arrays.stream(nums).max().getAsInt();
for (int divisor = 1; divisor <= maxElement; divisor++) {
int sumOfDivisionResults = 0;
boolean thresholdExceeded = true;
for (int num : nums) {
sumOfDivisionResults += (int) Math.ceil((double) num / divisor);
if (sumOfDivisionResults > threshold) {
thresholdExceeded = false;
break;
}
}
if (thresholdExceeded) {
return divisor;
}
}
return -1;
}
}
JavaScript solution
matched/originalvar smallestDivisor = function(nums, threshold) {
let maxElement = Math.max(...nums);
for (let divisor = 1; divisor <= maxElement; divisor++) {
let sumOfDivisionResults = 0;
let thresholdExceeded = true;
for (let num of nums) {
sumOfDivisionResults += Math.ceil(num / divisor);
if (sumOfDivisionResults > threshold) {
thresholdExceeded = false;
break;
}
}
if (thresholdExceeded) {
return divisor;
}
}
return -1;
};
Go solution
matched/originalimport "math"
func smallestDivisor(nums []int, threshold int) int {
maxElement := max(nums)
for divisor := 1; divisor <= maxElement; divisor++ {
sumOfDivisionResults := 0
thresholdExceeded := true
for _, num := range nums {
sumOfDivisionResults += int(math.Ceil(float64(num) / float64(divisor)))
if sumOfDivisionResults > threshold {
thresholdExceeded = false
break
}
}
if thresholdExceeded {
return divisor
}
}
return -1
}
func max(nums []int) int {
maxNum := nums[0]
for _, num := range nums {
if num > maxNum {
maxNum = num
}
}
return maxNum
}
Vacancies for this task
Active vacancies with overlapping task tags are shown.
There are no active vacancies yet.