首页 > 其他分享 >leetcode-10460-easy

leetcode-10460-easy

时间:2023-02-15 21:33:36浏览次数:41  
标签:stones stone easy int queue abs 10460 poll leetcode

Last Stone Weight

You are given an array of integers stones where stones[i] is the weight of the ith stone.

We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. 

Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is:

If x == y, both stones are destroyed, and
If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
At the end of the game, there is at most one stone left.

Return the weight of the last remaining stone. If there are no stones left, return 0.

Example 1:

Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation: 
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
Example 2:

Input: stones = [1]
Output: 1
Constraints:

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

思路一:有优先队列存储,模拟操作

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

        for (int stone : stones) {
            queue.offer(stone);
        }

        while (queue.size() > 1) {
            Integer x = queue.poll();
            Integer y = queue.poll();

            int abs = Math.abs(x - y);
            if (abs > 0) {
                queue.offer(abs);
            }
        }

        return queue.isEmpty() ? 0 : queue.poll();
    }

标签:stones,stone,easy,int,queue,abs,10460,poll,leetcode
From: https://www.cnblogs.com/iyiluo/p/17124773.html

相关文章

  • leetcode-783-easy
    MinimumDistancdBetweenNodesGiventherootofaBinarySearchTree(BST),returntheminimumdifferencebetweenthevaluesofanytwodifferentnodesinthe......
  • leetcode-824-easy
    GoatLatinYouaregivenastringsentencethatconsistofwordsseparatedbyspaces.Eachwordconsistsoflowercaseanduppercaselettersonly.Wewouldlik......
  • 【LeetCode栈与队列#05】滑动窗口最大值
    滑动窗口最大值力扣题目链接(opensnewwindow)给定一个数组nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k个数字。......
  • [Leetcode]435. 无重叠区间
    435.无重叠区间给定一个区间的集合 intervals ,其中intervals[i]=[starti,endi] 。返回需要移除区间的最小数量,使剩余区间互不重叠 。示例1:输入:intervals......
  • 【算法训练营day44】完全背包基础 LeetCode518. 零钱兑换II LeetCode377. 组合总和IV
    LeetCode518.零钱兑换II题目链接:518.零钱兑换II独上高楼,望尽天涯路组合问题和完全背包的混合应用,感觉脑中模拟的不是很清晰,但是靠着背包问题的代码技巧和模板就能比较......
  • 【算法训练营day43】LeetCode1049. 最后一块石头的重量II LeetCode494. 目标和 LeetCo
    LeetCode1049.最后一块石头的重量II题目链接:1049.最后一块石头的重量II独上高楼,望尽天涯路一开始还是没有想到怎么转化成01背包问题,所以直接看题解找思路慕然回首,灯......
  • vue3之异步组件defineAsyncComponent 使用无效?
    原文地址:我的稀土掘金介绍:defineAsyncComponent用于拆分应用为更小的块,并仅在需要时再从服务器加载相关组件官网案例<scriptsetup>import{defineAsyncComponent......
  • 【LeetCode队列#04】逆波兰表达式求值(仍然是经典的栈操作)
    逆波兰表达式求值力扣题目链接(opensnewwindow)根据逆波兰表示法,求表达式的值。有效的运算符包括+,-,*,/。每个运算对象可以是整数,也可以是另一个逆波兰表达......
  • leetcode-数据结构与算法
    第0001题:求两数之和leetcode对应题号:1力扣-原题链接:[请点击此处](https://leetcode.cn/problems/two-sum/"请点击此处")方法一思路暴力破解法,时间复杂度是\(O(n......
  • leetcode:题目1-
    第1题,对应leetcode题目编号:一、题目:xxx1、原题-力扣链接:请点击此处二、思路+代码1、方法一:一、思路二、代码statussList_merge3(mySList*pa,mySList*pb){ if......