1046. 最后一块石头的重量
作者:互联网
import java.util.Collections;
import java.util.PriorityQueue;
public class Algorithm {
public static void main(String[] args) {
int[] arr = {2,7,4,1,8,1};
System.out.println(new Solution().lastStoneWeight(arr));
}
}
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < stones.length; i++) {
pq.add(stones[i]);
}
while (pq.size() > 1){
int y = pq.poll();
int x = pq.poll();
if (y > x) {
pq.add(y - x);
}
}
if (pq.size() == 1){
return pq.poll();
}
else {
return 0;
}
}
}
https://leetcode-cn.com/problems/last-stone-weight/
标签:stones,pq,1046,int,重量,石头,PriorityQueue,poll,public 来源: https://www.cnblogs.com/taoyuann/p/15494504.html