Last active
April 12, 2020 20:21
-
-
Save munguial/6aca47f704165d0507139ce73bbd8797 to your computer and use it in GitHub Desktop.
Day 12 - Last Stone Weight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3297/ | |
import java.util.Collections | |
import java.util.PriorityQueue | |
class Solution { | |
fun lastStoneWeight(stones: IntArray): Int { | |
val pq = PriorityQueue<Int>(Collections.reverseOrder()) | |
for (s in stones) { | |
pq.add(s) | |
} | |
while (pq.size > 1) { | |
val res = pq.remove() - pq.remove() | |
if (res > 0) { | |
pq.add(res) | |
} | |
} | |
return if (pq.isEmpty()) 0 else pq.remove() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment