kt: add «2064. Minimized Maximum of Products Distributed to Any Store»
URL: https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
cd0ad4d7d5
commit
784547b4a2
1 changed files with 43 additions and 0 deletions
43
kt/minimized-maximum-of-products-distributed-to-any-store.kt
Normal file
43
kt/minimized-maximum-of-products-distributed-to-any-store.kt
Normal file
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue