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:
Matej Focko 2024-12-07 23:56:23 +01:00
parent 90a2ad8171
commit b4a300bec2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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;
}
}