kt: add «2226. Maximum Candies Allocated to K Children»

URL:	https://leetcode.com/problems/maximum-candies-allocated-to-k-children/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-03-14 18:26:26 +01:00
parent 234fce32c9
commit f173ae8c3c
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,26 @@
class Solution {
private fun canAllocate(
candies: IntArray,
k: Long,
count: Int,
): Boolean =
candies.asSequence().sumOf { pile ->
pile / count.toLong()
} >= k
fun maximumCandies(
candies: IntArray,
k: Long,
): Int {
var (left, right) = 0 to candies.max()!!
while (left < right) {
val mid = (left + right + 1) / 2
when {
canAllocate(candies, k, mid) -> left = mid
else -> right = mid - 1
}
}
return left
}
}