LeetCode/kt/maximum-candies-allocated-to-k-children.kt

26 lines
599 B
Kotlin

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