当前位置:网站首页>1046. Weight of the last stone
1046. Weight of the last stone
2022-07-21 22:40:00 【Anji_ lh1029】
1、 Title Description
2、 title
Because you have to choose in every round 【 The two heaviest stones 】, So you need to sort every time , You can think of using ready-made data structures that can automatically sort , therefore The thought of 【 Pile up 】 This data structure that can automatically sort .
Put all the stones into the big top pile , Then take out the two heaviest stones in turn x and y, There must be x ≥ y. If x > y, Then the new stone x - y Put it back in the largest heap ; If x = y, The two stones were completely crushed , Therefore, no new stones will be produced .
- use 【JAVA】 Realization , Statement 【 Descending 】 The big root pile is the difficulty :
- PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> b - a);
The implementation code is as follows :
class Solution {
public int lastStoneWeight(int[] stones) {
// Declare big root heap , Avoid sorting every time
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a,b)->b-a);
for(int stone : stones){
pq.offer(stone);
}
while(pq.size() > 1){
// because pq The array has been sorted before , Take the maximum two numbers each time , There is only b<=a The possibility of
int a = pq.poll();
int b = pq.poll();
//b=a when , Two direct poll that will do , When b<a when , You need to write to the heap again (b-a) This number
if(b < a){
pq.offer(a-b);
}
}
return pq.isEmpty() ? 0:pq.poll();
}
}
Complexity analysis
Time complexity :O(nlogn), among n Is the number of stones . Each time an element is taken out of the queue, it takes O(logn) Time for , At most, it needs to be crushed n−1 Secondary stone .
Spatial complexity :O(n).
边栏推荐
- Which of these five special Bluetooth cores suits your application best
- 第八周ACM训练报告
- What is a direct drinking machine? What is its working principle and advantages?
- 用两个模板(递归法和迭代法)完成二叉树的四种遍历方式
- MySQL安装
- What is PCBA? What is the importance of PCBA testing?
- LinkedList源码深度剖析
- 数据可视化图表插件开发作品大赏(一)
- 深入剖析多重背包问题(上篇)
- 数据可视化应用安装部署 | 使用 datart 安装包和源码部署的常见问题教程
猜你喜欢
YOLOv4(darknet版)后处理:显示置信度、保存检测框的内容到本地
Further learning of 02 selenium (control browser window +)
VS2019+OpenCV安装与配置教程
What is the difference between embedded hardware and software in embedded development?
What impact does the Internet of things have on the development of enterprises?
Heap - principle to application - heap sorting, priority queue
Paoding solves the fiboracci sequence and knapsack problem - analyze the optimization process of the two problems in detail, and take you to understand dynamic programming from the most basic problem!
437. 路径总和 III
MySQL安装
What is a direct drinking machine? What is its working principle and advantages?
随机推荐
Vs2019+opencv installation and configuration tutorial
你真的懂01背包问题吗?01背包的这几问你能答出来吗?
2022ACM夏季集训周报(一)
2022ACM夏季集训周报(三)
取石子
第十一周ACM训练报告
卡牌
03-selenium的自动化测试模型
Programmation créative / groupe primaire (4e - 6e année) - graphisme créatif
n factorial
n的阶乘
第七周ACM训练报告
What is the difference between embedded hardware and software in embedded development?
Let you know the current situation and future development trend of wireless charging technology
Open source data visualization tool datart new user experience tutorial for popular communities
2020普及组总结
Teach you how to use yolov4 training and testing data set on the server (nanny level)
手把手教你在服务器上用YOLOv4训练和测试数据集(保姆级)
04 cadre d'essai de l'unit é unitest
創意編程/小學組(4-6年級)-圖形化創意