leetcode-pp / 91alg-9-daily-check

5 stars 0 forks source link

【Day 86 】2023-01-25 - 1046.最后一块石头的重量 #93

Open azl397985856 opened 1 year ago

azl397985856 commented 1 year ago

1046.最后一块石头的重量

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/last-stone-weight/

前置知识

每一回合,从中选出两块 最重的 石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下:

如果 x == y,那么两块石头都会被完全粉碎; 如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x。 最后,最多只会剩下一块石头。返回此石头的重量。如果没有石头剩下,就返回 0。

 

示例:

输入:[2,7,4,1,8,1] 输出:1 解释: 先选出 7 和 8,得到 1,所以数组转换为 [2,4,1,1,1], 再选出 2 和 4,得到 2,所以数组转换为 [2,1,1,1], 接着是 2 和 1,得到 1,所以数组转换为 [1,1,1], 最后选出 1 和 1,得到 0,最终数组转换为 [1],这就是最后剩下那块石头的重量。  

提示:

1 <= stones.length <= 30 1 <= stones[i] <= 1000

paopaohua commented 1 year ago
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> b - a);
        for (int stone : stones) {
            pq.offer(stone);
        }

        while (pq.size() > 1) {
            int a = pq.poll();
            int b = pq.poll();
            if (a > b) {
                pq.offer(a - b);
            }
        }
        return pq.isEmpty() ? 0 : pq.poll();
    }
}
tiandao043 commented 1 year ago
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        sort(stones.begin(),stones.end());
        int n=stones.size();
        priority_queue<int,vector<int>,less<int>> pq;
        for(int s:stones){
            pq.push(s);
        }
        while(pq.size()!=1){
            // cout<<pq.size()<<endl;
            int a=pq.top();
            pq.pop();
            int b=pq.top();
            pq.pop();
            pq.push(a-b);
            // cout<<a<<" "<<b<<endl;
            if(pq.size()==1)break;
        }
        return pq.top();
    }
};
Ryanbaiyansong commented 1 year ago
class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        # choose 最重的两个
        stones = [-x for x in stones]
        heapq.heapify(stones)
        while len(stones) >= 2:
            x = heappop(stones)
            y = heappop(stones)
            if x == y:
                continue 
            else:
                heappush(stones, -abs(x - y))
        if len(stones) >= 1:
            return -stones[0]
        return 0
FlipN9 commented 1 year ago
/**    
    TC: O(NlogN)    SC: O(N)
*/
class Solution1 {
    public int lastStoneWeight(int[] stones) {
        // MaxHeap
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
        for (int x : stones)
            pq.offer(x);
        while (pq.size() > 1) {
            int a = pq.poll();
            int b = pq.poll();
            if (a > b)
                pq.offer(a - b);
        }
        return pq.isEmpty() ? 0: pq.poll();
    }
}
mayloveless commented 1 year ago

思路

大顶堆

代码

/**
 * @param {number[]} stones
 * @return {number}
 */
var lastStoneWeight = function(stones) {
    const maxQ = new MaxPriorityQueue();
    for (let i = 0; i < stones.length; ++i) {
        maxQ.enqueue(stones[i])
    }

    while(maxQ.size()>1) {
        const one = maxQ.dequeue().element;
        const two = maxQ.dequeue().element;
        if (one !== two) {
            maxQ.enqueue(one-two);
        }
    }

    return maxQ.dequeue()?.element ?? 0;
};

复杂度

时间O(nlogn) 空间O(n)

snmyj commented 1 year ago
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        int n=stones.size();
        if(n==1) return stones[0];

        for(int i=n-1;i>0;i--){
            sort(stones.begin(),stones.end());
            if(stones[i-1]==0) continue;
            if(stones[i]==stones[i-1]) {
                stones[i]=stones[i-1]=0;

            }
            else{
                stones[i-1]=stones[i]-stones[i-1];
                stones[i]=0;
            }
            i=n;
        }
        return stones[n-1];
    }
};
klspta commented 1 year ago
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
        for(int i = 0; i < stones.length; i++){
            queue.add(stones[i]);
        }
        while(queue.size() > 1){
            int y = queue.poll();
            int x = queue.poll();
            if(x == y){
                continue;
            }else{
                queue.add(y - x);
            }
        }
        return queue.size() == 1 ? queue.poll() : 0;
    }
}
bookyue commented 1 year ago

code

    public int lastStoneWeight(int[] stones) {
        var pq = new PriorityQueue<Integer>(Comparator.reverseOrder());

        for (int stone : stones) pq.add(stone);

        while (pq.size() > 1) {
            int y = pq.poll();
            int x = pq.poll();

            if (y > x) pq.offer(y - x);
        }

        return pq.isEmpty() ? 0 : pq.poll();
    }
Abby-xu commented 1 year ago

class Solution: def lastStoneWeight(self, stones: List[int]) -> int: h = [-x for x in stones] heapq.heapify(h) while len(h) > 1 and h[0] != 0: heapq.heappush(h, heapq.heappop(h) - heapq.heappop(h)) return -h[0]

Jetery commented 1 year ago
class Solution {
    public int lastStoneWeight(int[] stones) {

        PriorityQueue<Integer> heap = new PriorityQueue<>(stones.length, new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for(int num : stones) {
            heap.offer(num);
        }

        while (heap.size() > 1) {
            int largest = heap.poll();
            int large = heap.poll();
            if (largest > large) {
                heap.offer(largest - large);
            }
        }
        return heap.isEmpty() ? 0 : heap.poll();
    }
}
whoam-challenge commented 1 year ago

class Solution: def lastStoneWeight(self, stones: List[int]) -> int: while len(stones)>=2: stones.sort() stones.append(stones.pop()-stones.pop()) return stones[0]

RestlessBreeze commented 1 year ago

code

class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int> q;
        for (int s: stones) {
            q.push(s);
        }

        while (q.size() > 1) {
            int a = q.top();
            q.pop();
            int b = q.top();
            q.pop();
            if (a > b) {
                q.push(a - b);
            }
        }
        return q.empty() ? 0 : q.top();
    }
};