kt: add «2070. Most Beautiful Item for Each Query»
URL: https://leetcode.com/problems/most-beautiful-item-for-each-query/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d2aca8d88f
commit
b0a4c140be
1 changed files with 33 additions and 0 deletions
33
kt/most-beautiful-item-for-each-query.kt
Normal file
33
kt/most-beautiful-item-for-each-query.kt
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
class Solution {
|
||||||
|
private data class Item(val price: Int, val beauty: Int)
|
||||||
|
|
||||||
|
fun maximumBeauty(
|
||||||
|
items: Array<IntArray>,
|
||||||
|
queries: IntArray,
|
||||||
|
): IntArray {
|
||||||
|
// Sort items by the price
|
||||||
|
val items = items.map { Item(it[0], it[1]) }.toMutableList()
|
||||||
|
items.sortBy { it.price }
|
||||||
|
val maxBeautyTillNow =
|
||||||
|
items.scan(0) { runningMax, it ->
|
||||||
|
listOf(runningMax, it.beauty).max()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort queries by the price
|
||||||
|
val sortedQueries = queries.withIndex().toMutableList()
|
||||||
|
sortedQueries.sortBy { it.value }
|
||||||
|
|
||||||
|
var i = 0
|
||||||
|
return sortedQueries.map { query ->
|
||||||
|
while (i < items.size && items[i].price <= query.value) {
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
IndexedValue(query.index, maxBeautyTillNow[i])
|
||||||
|
}.sortedBy { query ->
|
||||||
|
query.index
|
||||||
|
}.map { query ->
|
||||||
|
query.value
|
||||||
|
}.toIntArray()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue