Find the Smallest Divisor Given a Threshold
给一个数组和一个数n, 求一个最小数, 作为除数时让数组每个数初它的和小于n. 这个题是二分搜索. 一个个找.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Solution { public int smallestDivisor(int[] nums, int threshold) { int l = 1; int h = 1000000; while(l < h) { int m = (h - l) / 2 + l; if(find(nums, m) > threshold) l = m + 1; else h = m; } return l; } private int find(int[] nums, int d) { int res = 0; for(int n : nums) { res += (n + d - 1) / d; } return res; } } |