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/

上一篇:Python:不带“


下一篇:python-给定两个元组列表,找到它们之间最接近的元组(dist