Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e61d254

Browse files
authoredDec 8, 2019
Create find-the-smallest-divisor-given-a-threshold.py
1 parent eff1ad1 commit e61d254

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def smallestDivisor(self, nums, threshold):
6+
"""
7+
:type nums: List[int]
8+
:type threshold: int
9+
:rtype: int
10+
"""
11+
def check(A, d, threshold):
12+
return sum((i-1)//d+1 for i in nums) <= threshold
13+
14+
left, right = 1, max(nums)
15+
while left <= right:
16+
mid = left + (right-left)//2
17+
if check(nums, mid, threshold):
18+
right = mid-1
19+
else:
20+
left = mid+1
21+
return left

0 commit comments

Comments
 (0)
Please sign in to comment.