classSolution { publicintminimumSize(int[] nums, int max) { intl=1, r = 0x3f3f3f3f; while (l < r) { intmid= l + r >> 1; if (check(nums, mid, max)) r = mid; else l = mid + 1; } return r; } booleancheck(int[] nums, int limit, int max) { intcnt=0; for (int x : nums) cnt += Math.ceil(x * 1.0 / limit) - 1; return cnt <= max; } }
C++ 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intminimumSize(vector<int>& nums, int maxv){ int l = 1, r = 0x3f3f3f3f; while (l < r) { int mid = l + r >> 1; if (check(nums, mid, maxv)) r = mid; else l = mid + 1; } return r; } boolcheck(vector<int>& nums, int limit, int maxv){ int cnt = 0; for (int x : nums) cnt += ceil(x * 1.0 / limit) - 1; return cnt <= maxv; } };
Python 代码:
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defminimumSize(self, nums: List[int], maxv: int) -> int: defcheck(nums, limit, maxv): returnsum([(x + limit - 1) // limit - 1for x in nums]) <= maxv l, r = 1, 0x3f3f3f3f while l < r: mid = l + r >> 1 if check(nums, mid, maxv): r = mid else: l = mid + 1 return r
TypeScript 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functionminimumSize(nums: number[], max: number): number { functioncheck(nums: number[], limit: number, max: number): boolean { let cnt = 0 for (const x of nums) cnt += Math.ceil(x / limit) - 1 return cnt <= max } let l = 1, r = 0x3f3f3f3f while (l < r) { const mid = l + r >> 1 if (check(nums, mid, max)) r = mid else l = mid + 1 } return r }