cs: add «1760. Minimum Limit of Balls in a Bag»
URL: https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
90a2ad8171
commit
b4a300bec2
1 changed files with 39 additions and 0 deletions
39
cs/minimum-limit-of-balls-in-a-bag.cs
Normal file
39
cs/minimum-limit-of-balls-in-a-bag.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
public class Solution {
|
||||
private int CeilDiv(int left, int right) {
|
||||
if (left % right != 0) {
|
||||
return 1 + left / right;
|
||||
}
|
||||
return left / right;
|
||||
}
|
||||
|
||||
private bool IsPossible(int[] nums, int maxOperations, int maxBalls) {
|
||||
var performed = 0;
|
||||
|
||||
foreach (var bag in nums) {
|
||||
var operations = CeilDiv(bag, maxBalls) - 1;
|
||||
performed += operations;
|
||||
|
||||
if (performed > maxOperations) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int MinimumSize(int[] nums, int maxOperations) {
|
||||
int left = 1, right = nums.Max();
|
||||
|
||||
while (left < right) {
|
||||
var middle = (left + right) / 2;
|
||||
|
||||
if (IsPossible(nums, maxOperations, middle)) {
|
||||
right = middle;
|
||||
} else {
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue