Skip to content

Commit 6d9ebd6

Browse files
authored
Create find-the-smallest-divisor-given-a-threshold.py
1 parent c5add92 commit 6d9ebd6

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import math
2+
3+
class Solution:
4+
"""
5+
@param nums: an array of integers
6+
@param threshold: an integer
7+
@return: return the smallest divisor
8+
"""
9+
def smallestDivisor(self, nums, threshold):
10+
# write your code here
11+
ret = 0
12+
min_value = sum(nums)
13+
l, r = 1, max(nums)
14+
while l <= r:
15+
mid = int((l + r) / 2)
16+
if self.check(nums, mid, threshold):
17+
r = mid - 1
18+
ret = mid
19+
else:
20+
l = mid + 1
21+
return ret
22+
23+
def check(self, nums, div, threshold):
24+
s = 0
25+
for i in nums:
26+
s += math.ceil(i / div)
27+
if s > threshold:
28+
return False
29+
return True
30+
31+
# medium: https://www.lintcode.com/problem/1816/

0 commit comments

Comments
 (0)