![JAR search and dependency download from the Maven repository](/logo.png)
g2501_2600.s2517_maximum_tastiness_of_candy_basket.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2501_2600.s2517_maximum_tastiness_of_candy_basket
// #Medium #Array #Sorting #Binary_Search #2023_07_04_Time_547_ms_(33.33%)_Space_52.1_MB_(100.00%)
class Solution {
fun maximumTastiness(price: IntArray, k: Int): Int {
price.sort()
val n = price.size
var left = 1
var right = (price[n - 1] - price[0]) / (k - 1) + 1
while (left < right) {
val mid = left + (right - left) / 2
if (check(mid, price, k)) {
left = mid + 1
} else {
right = mid
}
}
return left - 1
}
private fun check(target: Int, price: IntArray, k: Int): Boolean {
var count = 1
var x0 = price[0]
for (x in price) {
if (x >= x0 + target) {
count++
if (count >= k) {
return true
}
x0 = x
}
}
return false
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy