算法题目---优先级队列

(一).基础概念

优先级队列,也称为堆。在前面介绍数据结构章节介绍过了,所以这里就不多介绍了,直接上题目。

(二).具体题目

1.最后一块石头的重量

1046. 最后一块石头的重量 - 力扣(LeetCode)

解法:使用优先级队列

java 复制代码
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });

        for(int x:stones){
            priorityQueue.add(x);
        }
        while (priorityQueue.size()>1){
            int v1=priorityQueue.poll();
            int v2=priorityQueue.poll();
            if (v1-v2!=0){
                priorityQueue.add(v1-v2);
            }
        }
        return priorityQueue.size()>=1?priorityQueue.peek():0;
    }
}

2.数据流中的第K大元素

703. 数据流中的第 K 大元素 - 力扣(LeetCode)

解法:使用优先级队列

java 复制代码
class KthLargest {

    PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1-o2;
        }
    });
    int size;
    public KthLargest(int k, int[] nums) {
        size=k;
        for (int i = 0; i < nums.length; i++) {
            priorityQueue.add(nums[i]);
            if (priorityQueue.size()>k){
                priorityQueue.poll();
            }
        }
    }

    public int add(int val) {
        priorityQueue.add(val);
        if (priorityQueue.size()>size){
            priorityQueue.poll();
        }
        return priorityQueue.peek();
    }
}

3.前K个高频单词

692. 前K个高频单词 - 力扣(LeetCode)

解法:使用优先级队列

java 复制代码
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String,Integer> hashMap=new HashMap<>();
        for(String word:words){
            hashMap.put(word,hashMap.getOrDefault(word,0)+1);
        }

        PriorityQueue<Map.Entry<String,Integer>> priorityQueue=new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (o1.getValue().compareTo(o2.getValue())==0){
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        for(Map.Entry<String,Integer> entry:hashMap.entrySet()){
            priorityQueue.add(entry);
            if (priorityQueue.size()>k){
                priorityQueue.poll();
            }
        }
        List<String> list=new ArrayList<>();
        for (int i = 0; i < k; i++) {
            list.add(priorityQueue.poll().getKey());
        }
        Collections.reverse(list);
        return list;
    }

4.数据流的中位数

295. 数据流的中位数 - 力扣(LeetCode)

解法:使用优先级队列

java 复制代码
class MedianFinder {


    PriorityQueue<Integer> leftQueue;
    PriorityQueue<Integer> rightQueue;

    public MedianFinder() {
        leftQueue=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });

        rightQueue=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
    }

    public void addNum(int num) {
        if (leftQueue.size()==rightQueue.size()){
            if (leftQueue.isEmpty() || num<=leftQueue.peek()){
                leftQueue.add(num);
            }else{
                rightQueue.add(num);
                leftQueue.add(rightQueue.poll());
            }
        }else{
            if (num<=leftQueue.peek()){
                leftQueue.add(num);
                rightQueue.add(leftQueue.poll());
            }else {
                rightQueue.add(num);
            }
        }
    }

    public double findMedian() {
        if (leftQueue.size()>rightQueue.size()){
            return leftQueue.peek();
        }else{
            return (leftQueue.peek()+rightQueue.peek())/2.0;
        }
    }
}
相关推荐
想做小南娘,发现自己是女生喵1 小时前
【无标题】
数据结构·算法
Kx_Triumphs3 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
旖-旎3 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
轻颂呀4 小时前
约瑟夫环问题
算法
科技大视界6 小时前
投资AI项目,传统尽调不够用了——李章虎律师拆解算法、数据、算力三大雷区
人工智能·算法·数据挖掘
郝学胜-神的一滴6 小时前
算法实战:最小k个数——大顶堆的优雅解法
开发语言·数据结构·c++·python·程序人生·算法·排序算法
Irissgwe6 小时前
算法滑动窗口
数据结构·算法
怪兽学LLM6 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:分治递归思路详解
算法·leetcode·职场和发展
可编程芯片开发7 小时前
基于PI控制的三相整流器控制系统的simulink建模与仿真,包含超级电容充电和电机
算法