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 are totally destroyed;
  • If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.

At the end, there is at most 1 stone left.  Return the weight of this stone (or 0 if there are no stones left.)

Example 1:

Input: [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 last stone.

Note:

  1. 1 <= stones.length <= 30
  2. 1 <= stones[i] <= 1000
class Solution {
    public int lastStoneWeight(int[] A) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b)-> b - a);
        for (int a : A)
            pq.offer(a);
        while (pq.size() > 1)
            pq.offer(pq.poll() - pq.poll());
        return pq.poll();
    }

}

https://leetcode.com/problems/last-stone-weight/discuss/294956/JavaC%2B%2BPython-Priority-Queue

大神总是能一次次的使我下巴脱臼

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12245054.html

时间: 2024-11-07 05:20:41

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 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)

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. 

(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

动态规划-Last Stone Weight II

2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个题目被证明和背包问题是同一个问题,只是需要进行一下转化. 本题其实也是一个套壳题目,只是这次的壳套的更加隐蔽,对于本题来说,其实核心就是将其划分成两个堆,我们需要的是两堆的diff最小. 那么就需要另一个技巧了,我们不会直接使用dp去求这个最小值,而是使用dp去判断对于小堆中的和的可能性,最后在遍历

stone jaw crusher

Topics CoveredIntroductionDiscussionCalibrationSensitivityPrecisionConclusionAcknowledgmentReferencesAbout PANalytical IntroductionAirborne silica particles pose a carcinogenic threat at concentrations as low as 50 μg/m3. mineral material grinding mi

HDU 4048 Zhuge Liang&#39;s Stone Sentinel Maze

Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 385    Accepted Submission(s): 106 Problem Description Zhuge Liang was a chancellor of the state of Shu Han dur

POJ 1740 A New Stone Game

A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5453   Accepted: 2989 Description Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob

LinearLayout设置 weight 无法绘制的问题

项目地址:ChildLayout 一. 问题起因: 新项目中首页有这么一个需求:看布局: 主要就是"大牌专场"这个栏目的布局问题,呈现一个,左--右上--右下 的形式,因为不能像 iOS 那样直接根据 UED 给的标注来写死布局尺寸,Android 混乱的分辨率决定了这个布局需要采用 android:layout_height="wrap_content" 的形式来决定它的高. 二. 解决方法1:使用 LinearLayout--失败 要画这个布局很简单的,比例都是