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:
parent
234fce32c9
commit
f173ae8c3c
1 changed files with 26 additions and 0 deletions
26
kt/maximum-candies-allocated-to-k-children.kt
Normal file
26
kt/maximum-candies-allocated-to-k-children.kt
Normal 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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue