func smallestDivisor(nums []int, threshold int) int {
left := 1
right := 0
for i := 0; i < len(nums); i++ {
if nums[i] > right {
right = nums[i]
}
}
for left < right {
mid := left + (right - left) / 2
sum := 0
for i := 0; i < len(nums); i++ {
if nums[i] % mid == 0 {
sum += nums[i] / mid
} else {
sum += nums[i] / mid + 1
}
}
if sum > threshold {
left = mid + 1
} else {
right = mid
}
}
return left
}
[LeetCode][Go] Daily Challenge 2020-11-06 Find the Smallest Divisor Given a Threshold
最新推荐文章于 2024-08-14 17:00:26 发布