From b4a300bec274fab62596cbcb946f991e0aa7b48b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 7 Dec 2024 23:56:23 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB1760.=20Minimum=20Limit=20of?= =?UTF-8?q?=20Balls=20in=20a=20Bag=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/ Signed-off-by: Matej Focko --- cs/minimum-limit-of-balls-in-a-bag.cs | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 cs/minimum-limit-of-balls-in-a-bag.cs diff --git a/cs/minimum-limit-of-balls-in-a-bag.cs b/cs/minimum-limit-of-balls-in-a-bag.cs new file mode 100644 index 0000000..00fd33e --- /dev/null +++ b/cs/minimum-limit-of-balls-in-a-bag.cs @@ -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; + } +}