Leetcode(easy heap)
Leetcode (easy heap)
703 資料流中的第K大元素
class KthLargest{
int size;
private PriorityQueue<Integer> q;
public KthLargest(int k,int[] nums){
size = k;
q = new PriorityQueue(k);
for(int num:nums) add(num);
}
public int add(int val){
if(q.size()<size) q.add(val);
else if(q.peek()<val){
q.poll();
q.add(val);
}
return q.peek();
}
}
1046 最後一塊石頭的重量
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> (o2 - o1));
for (int i = 0; i < stones.length; i++) {
queue.offer(stones[i]);
}
while( queue.size() > 1 ) {
int y = queue.poll();
int x = queue.poll();
int diff = y - x;
if ( diff != 0 ) queue.offer(diff);
}
if ( queue.size() == 0 ) return 0;
return queue.peek();
}
}
相關文章
- LeetCode-EasyLeetCode
- LeetCode 198. 打家劫舍(Easy)LeetCode
- Leetcode - Tree - Easy(100-110)LeetCode
- LeetCode - Easy - 66. Plus OneLeetCode
- Leetcode-Easy 70. Climbing StairsLeetCodeAI
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- C# 寫 LeetCode easy #14 Longest Common PrefixC#LeetCode
- [LeetCode-SQL-Easy]176. 第二高薪水LeetCodeSQL高薪
- LeetCode C++ 703. Kth Largest Element in a Stream【Heap/Design】簡單LeetCodeC++
- 【LeetCode】290. Word Pattern 單詞規律(Easy)(JAVA)每日一題LeetCodeJava每日一題
- 堆排序 Heap Sort排序
- 淺談堆-Heap(一)
- ●Joyoi Easy
- Java堆記憶體Heap與非堆記憶體Non-HeapJava記憶體
- The Stack and the Heap棧與堆__RustRust
- Exploit開發系列教程-Heap
- 資料結構 - 堆(Heap)資料結構
- As Easy As A+B
- Easy-Admin
- 【BUUCTF】Easy JavaJava
- 【BUUCTF】easy calc
- 字串魔法(easy)字串
- 資料結構之堆(Heap)資料結構
- vue-easy-rendererVue
- type challenge(easy 部分)
- Prefix Flip (Easy Version)
- [RoarCTF 2019]Easy Calc
- Fractal pg walkthrough Easy
- Hub PG walkthrough Easy
- Catch the Mole(Easy Version)
- j-easy/easy-rules: Java簡單的規則引擎Java
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- Golang標準庫學習—container/heapGolangAI
- 【轉】堆排序Heap Sort——Java實現排序Java
- 聊聊flink JobManager的heap大小設定
- Kernel pwn 基礎教程之 Heap Overflow
- 解決yarn打包時出現“FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory”的問題YarnErrorMITAIJavaScript