-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC_1705.kt
56 lines (49 loc) · 1.57 KB
/
LC_1705.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com
import java.util.*
class Solution {
fun eatenApples(apples: IntArray, days: IntArray): Int {
var day = 1
var ans = 0
val pq = PriorityQueue<Apple>()
pq.offer(Apple(quantity = apples[day - 1], rotDay = day + days[day - 1]))
while(true) {
if(pq.isEmpty() && day >= apples.size) break
var top = pq.poll()
while (top != null && top.canEat(day).not() && pq.isNotEmpty()) {
top = pq.poll()
}
// 현재 먹을 수 있는게 있으면 먹음
if(top!= null && top.canEat(day)) {
pq.offer(top.eat())
ans += 1
}
day += 1
if (day <= apples.size)
pq.offer(Apple(quantity = apples[day - 1], rotDay = day + days[day - 1]))
}
return ans
}
class Apple(
var quantity: Int,
private val rotDay: Int
) : Comparable<Apple> {
override fun compareTo(other: Apple): Int {
if (this.rotDay > other.rotDay) return 1
return -1
}
fun canEat(currentDay: Int): Boolean = this.quantity > 0 && currentDay < rotDay
fun eat(): Apple {
if (this.quantity > 0) {
this.quantity = this.quantity - 1
return this
}
return this
}
}
}
fun main() {
val sol = Solution()
val apples = intArrayOf(1,2,3,5,2)
val days = intArrayOf(3,2,1,4,2)
sol.eatenApples(apples, days)
}