Skip to content

Commit abb4736

Browse files
add 2335
1 parent 2565cc4 commit abb4736

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

contest/src/main/java/com/github/contest/heap/HeapLeetcode.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,27 @@ fun numberGame(nums: IntArray): IntArray {
217217

218218
return nums
219219
}
220+
221+
/**
222+
* 2335. Minimum Amount of Time to Fill Cups
223+
*/
224+
225+
fun fillCups(amount: IntArray): Int {
226+
val heap = PriorityQueue<Int> { a, b -> b - a }
227+
var counter = 0
228+
for (water in amount) if (water != 0) heap.offer(water)
229+
while (heap.isNotEmpty()) {
230+
if (heap.size >= 2) {
231+
val one = heap.poll()
232+
val two = heap.poll()
233+
if (one - 1 != 0) heap.offer(one - 1)
234+
if (two - 1 != 0) heap.offer(two - 1)
235+
} else {
236+
val single = heap.poll()
237+
if (single - 1 != 0) heap.offer(single - 1)
238+
}
239+
counter++
240+
}
241+
242+
return counter
243+
}

0 commit comments

Comments
 (0)