Given an array of integers nums
and an integer threshold
, we will choose a positive integer divisor
, divide all the array by it, and sum the division's result. Find the smallest divisor
such that the result mentioned above is less than or equal to threshold
.
Each result of the division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3
and 10/2 = 5
).
Example 1:
Input: nums = [1,2,5,9], threshold = 6
Output: 5
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
If the divisor is 4 we can get a sum of 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).
Example 2:
Input: nums = [44,22,33,11,1], threshold = 5
Output: 44
Constraints:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
This problem requires us to find the smallest divisor such that the sum of the divisions (ceiling value) of the input array elements is less than or equal to the given threshold. We can solve this using binary search.
A naive solution would be to iterate through all possible divisors from 1 to the maximum element in the nums
array and check if the sum of the divisions is less than or equal to the threshold
. This solution has a time complexity of O(N*M), where N is the length of the nums
array and M is the maximum value in the nums
array.
We can use binary search to find the smallest divisor efficiently. The search space is from 1 to the maximum value in the nums
array. For each divisor, we calculate the sum of the ceiling of the divisions and check if it is less than or equal to the threshold. If it is, we try a smaller divisor; otherwise, we try a larger divisor.
def smallestDivisor(nums, threshold):
left, right = 1, max(nums)
ans = right
while left <= right:
mid = (left + right) // 2
total = sum((num + mid - 1) // mid for num in nums)
if total <= threshold:
ans = mid
right = mid - 1
else:
left = mid + 1
return ans
nums = [1, 2, 5, 9]
threshold = 6
print(smallestDivisor(nums, threshold)) # Output: 5
The binary search takes O(log M) time, where M is the maximum element in the nums
array. For each divisor, we iterate through the nums
array once to calculate the sum of the divisions, which takes O(N) time. Therefore, the overall time complexity is O(N log M).
The space complexity is O(1) because we are only using a few variables to store the left, right, mid, and answer values. We are not using any additional data structures that scale with the input size.
nums
is empty, the problem statement doesn't explicitly define the behavior. We can assume that the problem statement asserts that the input array will not be empty, based on the problem constraints.nums.length <= threshold
. This guarantees that a valid divisor exists. If this were not true, it might be impossible to find a divisor that satisfies the condition.nums
or the threshold are very large, integer overflow might occur. Using Python avoids such issues automatically due to dynamic typing and arbitrary-precision integers. In other languages like Java or C++, using long
is necessary.nums
.The binary search approach provides an efficient way to find the smallest divisor. The key idea is to leverage the monotonically decreasing property of the sum of divisions as the divisor increases. This property allows us to effectively narrow down the search space and find the optimal divisor in logarithmic time concerning the range of possible divisors.