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

题解:

Put all stones into max heap.

While heap size >= 2, poll top 2 elements and get diff. If diff is larger than 0, add it back to heap.

Time Complexity: O(nlogn). n = stones.length. while loop could run for maximumn n-1 times.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int lastStoneWeight(int[] stones) {
 3         if(stones == null || stones.length == 0){
 4             return 0;
 5         }
 6
 7         PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
 8         for(int stone : stones){
 9             maxHeap.add(stone);
10         }
11
12         while(maxHeap.size() > 1){
13             int x = maxHeap.poll();
14             int y = maxHeap.poll();
15             int diff = x-y;
16             if(diff > 0){
17                 maxHeap.add(diff);
18             }
19         }
20
21         return maxHeap.isEmpty() ? 0 : maxHeap.peek();
22     }
23 }

跟上Last Stone Weight II.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11785085.html

时间: 2024-08-30 14:02:43

LeetCode 1046. Last Stone Weight的相关文章

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. 

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

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 

Leetcode 364: Nested List Weight Sum II

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. Different from the previous question where weight

[LeetCode] Random Pick with Weight 根据权重随机取点

Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight. Note: 1 <= w.length <= 10000 1 <= w[i] <= 10^5 pickIndex will be called at

动态规划-Last Stone Weight II

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