URL: https://leetcode.com/problems/maximum-candies-allocated-to-k-children/ Signed-off-by: Matej Focko <me@mfocko.xyz>
26 lines
599 B
Kotlin
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
|
|
}
|
|
}
|