LeetCode 1046. Last Stone Weight (最后一块石头的重量 )

题目标签:Greedy

  利用priority queue, 把石头重量都存入 pq, 每次取最大两个比较,存入差值,直到pq 只剩最后一个。

Java Solution:

Runtime:  1 ms, faster than 92.5%

Memory Usage: 37.1 MB, less than 100%

完成日期:02/14/2020

关键点:priority queue

class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue <Integer> pq = new PriorityQueue<>((a, b) -> b - a);

        // add each stone weight into pq
        for(int s : stones) {
            pq.offer(s);
        }

        // each time get two max stones and add the difference back into pq
        while(pq.size() > 1) {
            pq.offer(pq.poll() - pq.poll());
        }

        return pq.poll();
    }
}

参考资料:LeetCode Discuss

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/12310516.html

时间: 2024-10-08 19:21:14

LeetCode 1046. Last Stone Weight (最后一块石头的重量 )的相关文章

LeetCode 1046. Last Stone Weight

原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose the two heaviest rocks and smash them together.  Suppose the stones have weights x and y with x

LeetCode 1049. Last Stone Weight II

原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y. 

LeetCode1046 最后一块石头的重量(贪心—Java优先队列简单应用)

题目: 有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块最重的石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么两块石头都会被完全粉碎:如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x.最后,最多只会剩下一块石头.返回此石头的重量.如果没有石头剩下,就返回 0. 提示: 1 <= stones.length <= 301 <= stones[i]

1046. Last Stone Weight

We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose the two heaviest rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones ar

一块石头

作者: 刘亮程 突然想起我送你的那块石头了.它还好吗?你是否带着它回了家.你已经没有家了,四处漂泊的你,又能将一块石头带到了哪里.我说过,它很沉的.那是块孤独透顶的石头,你别企图感化它,我相信你的爱和热情,但它不会比一块石头坚持的更久. 之所以送给你,是因为你的生日,也因为那天我喝多了,想到自己,最终无力让我喜欢的石头,真正出土. 我发现了它,就像它发现了我. 仅仅是一个人和一块石头的默默相遇.在那荒凉的乱石滩上,那样巨大而空虚的一个黄昏,头顶滚滚沙尘的我,用什么擦拭了一块石头的百年蒙尘. 把它

(Easy) Last Stone Weight LeetCode

class Solution { public int lastStoneWeight(int[] stones) { int len = stones.length; int i = len -1; int minus = 0; int remain = len; if(stones.length ==1){ return stones[0]; } else { Arrays.sort(stones); do{ if(stones[i]==stones[i-1]){ stones[i] = 0

[leetcode] 339. Nested List Weight Sum

题目链接: https://leetcode.com/problems/nested-list-weight-sum/ Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or ot

连载《一个程序员的成长历程》-11.80后结婚,房子是悬在每个人心中的一块石头

小的时候,每到寒暑假就去三姨家去“度假”,有一次父母骑着自行车送我,正好一位老先生在那串门,老眼昏花.两鬓斑白,据说有90多岁了,他们聊的很热闹,我记得印象最深的一句话是(老先生说):他们(指我,代指80后)赶上好时代了,生在红旗下长在春风里,不愁吃不悉穿.后半句说的到是真的,但是以我活到现在却深深沉思着:我们真的赶上好时代了?生在红旗下,真的是长在春风里? 改革开放初期的“改革”如同当年的“革命”一样,给亿万中国人注入了新鲜的血液,让所有人看到了希望,感觉取得的成绩会人民共享.随着改革的不断深

LeetCode 339. Nested List Weight Sum (嵌套列表重和)

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Example 1:Given the list [[1,1],2,[1,1]], return