1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| import java.util.*
class Solution {
fun solution(food_times: IntArray, k : Long) : Int {
val foodQueue = LinkedList(food_times.indices
.groupBy { food_times[it] }
.toSortedMap()
.toList())
var takingTime = 0L
var remainFoodCount = food_times.size
var lastEatenFoodTakingTime = 0
while(foodQueue.isNotEmpty()) {
val currentFood = foodQueue.peekFirst()
val nextTime : Long = takingTime + (currentFood.first - lastEatenFoodTakingTime) * remainFoodCount
if(nextTime > k) break
takingTime = nextTime
remainFoodCount -= currentFood.second.size
lastEatenFoodTakingTime = currentFood.first
foodQueue.removeFirst()
}
return if(foodQueue.isEmpty()) -1
else ArrayList<Int>().apply { foodQueue.forEach { it.second.forEach { add(it + 1) } } }.sorted()[((k - takingTime) % remainFoodCount).toInt()]
}
}
|