From 784547b4a27f9b08133671406d7db307e0b4721b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 14 Nov 2024 22:45:40 +0100 Subject: [PATCH] =?UTF-8?q?kt:=20add=20=C2=AB2064.=20Minimized=20Maximum?= =?UTF-8?q?=20of=20Products=20Distributed=20to=20Any=20Store=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/ Signed-off-by: Matej Focko --- ...um-of-products-distributed-to-any-store.kt | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 kt/minimized-maximum-of-products-distributed-to-any-store.kt diff --git a/kt/minimized-maximum-of-products-distributed-to-any-store.kt b/kt/minimized-maximum-of-products-distributed-to-any-store.kt new file mode 100644 index 0000000..74f9a16 --- /dev/null +++ b/kt/minimized-maximum-of-products-distributed-to-any-store.kt @@ -0,0 +1,43 @@ +class Solution { + private fun canDistribute( + n: Int, + quantities: IntArray, + pivot: Int, + ): Boolean { + var j = 0 + var remaining = quantities[j] + + return (1..n).any { + if (remaining <= pivot) { + j += 1 + if (j == quantities.size) { + return@any true + } + + remaining = quantities[j] + } else { + remaining -= pivot + } + + return@any false + } + } + + fun minimizedMaximum( + n: Int, + quantities: IntArray, + ): Int { + var (left, right) = 0 to quantities.max() + + while (left < right) { + val middle = (left + right) / 2 + if (canDistribute(n, quantities, middle)) { + right = middle + } else { + left = middle + 1 + } + } + + return left + } +}