Skip to content

Instantly share code, notes, and snippets.

@JunJaBoy
Created April 29, 2024 07:31
Show Gist options
  • Save JunJaBoy/4d908fa2de08ef9990477d983c52a17c to your computer and use it in GitHub Desktop.
Save JunJaBoy/4d908fa2de08ef9990477d983c52a17c to your computer and use it in GitHub Desktop.
HackerRank/Algorithm/JesseAndCookies kotlin2
import java.util.*
import kotlin.*
import kotlin.collections.*
private fun List<Int>.sweetenCookies(targetSweetness: Int): Int {
val heap = PriorityQueue(this)
var count = 0
while (heap.size > 1 && heap.peek() <= targetSweetness) {
val first = heap.poll()
val second = heap.poll()
heap.add(first + second * 2)
count++
}
return if (heap.peek() < targetSweetness) {
-1
} else {
count
}
}
fun main(args: Array<String>) {
val (_, sweetness) = readln().trimEnd()
.split(" ")
.map(String::toInt)
val cookies = readln().trimEnd()
.split(" ")
.map(String::toInt)
val result = cookies.sweetenCookies(targetSweetness = sweetness)
println(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment