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

(一).基础概念

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

(二).具体题目

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;
        }
    }
}
相关推荐
程序喵大人6 分钟前
【C++进阶】STL算法与函数对象 - 01 让算法去处理一段迭代器范围
开发语言·c++·算法·函数对象
Keven_1110 分钟前
算法札记:二叉树前序、中序、后序遍历及对应序列重要性质
数据结构·算法·深度优先
Delite80214 分钟前
微库仑氧化法硫氯检测:油品分析中测量误差的控制要点
算法
宵时待雨20 分钟前
优选算法专题12:队列
算法·leetcode·职场和发展
tianyu23429 分钟前
双指针循环匹配金额——打款记录与账单的自动对账算法
java·算法·双指针循环·匹配金额
旖旎夜光1 小时前
LeetCode 738:单调递增的数字(贪心算法) —— 题解
数据结构·c++·算法·leetcode·贪心算法
青山木1 小时前
Hot 100 --- 搜索二维矩阵
java·算法·leetcode·矩阵
旖旎夜光1 小时前
LeetCode 1262:可被三整除的最大和 (贪心算法)—— 题解
数据结构·c++·算法·leetcode·贪心算法
心运软件1 小时前
基于机器学习的智能邮件分类系统:从数据采集到模型部署全流程实战
人工智能·python·算法·随机森林·机器学习·分类·scikit-learn
晚风叙码2 小时前
C++ 模板深度解析:类型模板、非类型模板、特化与分离编译
开发语言·c++·算法