1283. Find the Smallest Divisor Given a Threshold
题目文本会按所选界面语言从俄语翻译;代码保持不变。
Дан 数组 целых чисел nums и 整数 threshold. Мы выберем положительный целый делитель, разделим все elementы 数组а на него и суммируем результат деления. find наименьший делитель, такой что результат, упомянутый выше, меньше или равен threshold.
Каждый результат деления округляется до ближайшего большего целого числа. (На示例: 7/3 = 3 и 10/2 = 5).
C# 解法
匹配/原始public 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++ 解法
自动草稿,提交前请检查#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 解法
匹配/原始class 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 解法
匹配/原始var 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 解法
匹配/原始import "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
活跃职位 with overlapping task tags are 已显示.
目前还没有活跃职位。