码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【英雄哥六月集训】第 21天: 堆(优先队列)


    系列文章

    【英雄哥六月集训】第 01天:数组

    【英雄哥六月集训】第 02天:字符串

    【英雄哥六月集训】第 03天:排序

    【英雄哥六月集训】第 04天:贪心

    【英雄哥六月集训】第 05天: 双指针

    【英雄哥六月集训】第 06天:滑动窗口

    【英雄哥六月集训】第 07天:哈希

    【英雄哥六月集训】第 08天:前缀和

    【英雄哥六月集训】第 09天:二分查找

    【英雄哥六月集训】第 10天: 位运算

    【英雄哥六月集训】第 11天: 矩阵

    【英雄哥六月集训】第 12天: 链表

    【英雄哥六月集训】第 13天: 双向链表

    【英雄哥六月集训】第 14天: 栈

    【英雄哥六月集训】第 15天: 二叉树

    【英雄哥六月集训】第 16天: 队列

    【英雄哥六月集训】第 17天: 广度优先搜索

    【英雄哥六月集训】第 18天: 树

    【英雄哥六月集训】第 19天: 二叉树

    【英雄哥六月集训】第 20天: 二叉搜索树

    【英雄哥六月集训】第 21天: 堆(优先队列)

    文章目录

    • 系列文章
    • 堆(优先队列)
    • 一、 道路的最大总重要性
    • 二、 最长快乐字符串
    • 三、 为高尔夫比赛砍树
    • 四、 分割数组为连续子序列
    • 总结


    堆(优先队列)

    堆(heap)是计算机科学中一类特殊的数据结构的统称。堆通常是一个可以被看做一棵树的数组对象。堆总是满足下列性质:
    堆中某个结点的值总是不大于或不小于其父结点的值;
    堆总是一棵完全二叉树。
    将根结点最大的堆叫做最大堆或大根堆,根结点最小的堆叫做最小堆或小根堆。常见的堆有二叉堆、斐波那契堆等。
    堆是非线性数据结构,相当于一维数组,有两个直接后继。

    一、 道路的最大总重要性

    2285. 道路的最大总重要性

    class Solution {
        class Node{
            int deg;
            int v;
            Node(){}
    
            Node(int v,int d){
                this.v=v;
                this.deg=d;
            }
    
            public boolean compareTo(Node o){
                return this.deg < o.deg;
            }
    
            public String toString(){
                return "Node[val="+v+",deg="+deg+"]";
            }
        }
       
        public long maximumImportance(int n, int[][] roads) {
            int[] deg = new int[50010];
            int[] val = new int[50010];
            int id = n;
            for(int i=0;i<roads.length; ++i){
                ++deg[roads[i][0]];
                ++deg[roads[i][1]];
            }
            PriorityQueue<Node> q = new PriorityQueue<Node>((na,nb)->nb.deg-na.deg);
            for(int i=0;i<n;++i){
                q.add(new Node(i,deg[i]));
            }
            System.out.println(q);
            while(!q.isEmpty()){
                Node node =q.remove();
                val[node.v]=id;
                --id;
            }
            long ans=0;
            for(int i=0;i<roads.length;++i){
                ans += val[roads[i][0]]+val[roads[i][1]];
            }
            return ans;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    二、 最长快乐字符串

    1405. 最长快乐字符串

    class Solution {
        class Node{
            char ch;
            int cnt;
            Node(){}
            Node(char c,int cn){
                this.ch=c;
                this.cnt=cn;
            }
    
            public String toString(){
                return "["+this.ch+","+this.cnt+"]";
            }
        }
        public String longestDiverseString(int a, int b, int c) {
            PriorityQueue<Node> q=new PriorityQueue<Node>((na,nb)->nb.cnt-na.cnt);
            if(a!=0){
                q.add(new Node('a',a));
            }
            if(b!=0){
                q.add(new Node('b',b));
            }
            if(c!=0){
                q.add(new Node('c',c));
            }
            StringBuilder ret = new StringBuilder();
            StringBuilder pre = new StringBuilder();
            while(!q.isEmpty()){
                Node n = q.remove();
                System.out.println("before q="+q);
                if(pre.toString().equals("aa") && n.ch == 'a' || pre.toString().equals("bb") && n.ch == 'b' || pre.toString().equals("cc") && n.ch == 'c'){
                    System.out.println("pre="+pre+",n.ch="+n.ch);
                    if(q.isEmpty()){
                        return ret.toString();
                    }
                    Node next = q.remove();
                    ret.append(next.ch);
                    if(--next.cnt!=0){
                        q.offer(next);
                    }
                    q.offer(n);
                    pre = new StringBuilder();
                    pre.append(next.ch);
                }
                else
                {
                    ret.append(n.ch);
                    pre.append(n.ch);
                    if(pre.length()>2){
                        char pre2 = pre.charAt(pre.length()-2);
                        char pre1 = pre.charAt(pre.length()-1);
    
                        pre = new StringBuilder();
                        pre.append(pre2);
                        pre.append(pre1);
                    }
    
                    if(--n.cnt!=0){
                        q.offer(n);
                    }
    
                }
                System.out.println("after q="+q);
            }
            return ret.toString();
    
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    三、 为高尔夫比赛砍树

    675. 为高尔夫比赛砍树

    class Solution {
        class Node{
            int x,y,h;
            Node(){}
            Node(int x,int y,int h){
                this.x=x;
                this.y=y;
                this.h=h;
            }
            public String toString(){
                return "["+this.x+","+this.y+","+this.h+"]";
            }
        }
        int[][] dir = new int[][]{
            {0,1},{0,-1},{1,0},{-1,0}
        };
        public int cutOffTree(List<List<Integer>> forest) {
            int m = forest.size();
            int n = forest.get(0).size();
            int i,j;
            PriorityQueue<Node> q = new PriorityQueue<Node>((na,nb)->na.h-nb.h);
            if(forest.get(0).get(0)==0){
                return -1;
            }
            for(i = 0; i<m; ++i){
                for(j=0; j<n; ++j){
                    if(forest.get(i).get(j)>1){
                        q.add(new Node(i,j,forest.get(i).get(j)));
                    }
    
                }
            }
            System.out.println(q);
            Node pre = new Node(0,0,forest.get(0).get(0));
            int ans = 0;
            while(!q.isEmpty()){
                
                Node now = q.remove();
                //System.out.println(now);
                int dis=bfs(m,n,forest,pre,now);
                //System.out.println(dis);
                if(dis == -1){
                    return -1;
                }
                ans += dis;
                pre = now;
            }
            return ans;
    
        }
        public int bfs(int m,int n, List<List<Integer>> forest, Node start, Node end){
            int[][] hash = new int[51][51];
            for(int i=0;i<51;i++){
                for(int j=0;j<51;j++){
                    hash[i][j]=-1;
                }
            }
            Queue<Node> d=new LinkedList<Node>();
            hash[start.x][start.y]=0;
            d.add(start);
            while(!d.isEmpty()){
                //System.out.println(q);
                Node now=d.poll();
                
                if(now.x == end.x && now.y == end.y){
                    return hash[end.x][end.y];
                }
                System.out.println(d);
                Node next = new Node();
                
                for(int i=0;i<4;++i){
                    next.x = dir[i][0] + now.x;
                    next.y = dir[i][1] + now.y;
                    if(next.x<0 || next.y<0 || next.x == m || next.y == n){
                        continue;
                    }
                    if(forest.get(next.x).get(next.y)==0){
                        continue;
                    }
                    if(hash[next.x][next.y]!=-1){
                        continue;
                    }
                    
                    hash[next.x][next.y] = hash[now.x][now.y]+1;
                    
                    d.add(next);
                    System.out.println(d);
                }
                System.out.println(d);
                
    
            }
            return -1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    四、 分割数组为连续子序列

    659. 分割数组为连续子序列

    class Solution {
        public boolean isPossible(int[] nums) {
            Map<Integer,PriorityQueue<Integer>> map = new HashMap<Integer,PriorityQueue<Integer>>();
            for(int x:nums){
                if(!map.containsKey(x)){
                    map.put(x, new PriorityQueue<Integer>());
                }
                if(map.containsKey(x-1)){
                    int prevLength = map.get(x-1).poll();
                    if(map.get(x-1).isEmpty()){
                        map.remove(x-1);
                    }
                    map.get(x).offer(prevLength+1);
                } else {
                map.get(x).offer(1);
                }
                
            }
            Set<Map.Entry<Integer, PriorityQueue<Integer>>> entrySet = map.entrySet();
            for( Map.Entry<Integer, PriorityQueue<Integer>> entry: entrySet){
                PriorityQueue<Integer> queue = entry.getValue();
                if( queue.peek()<3){
                    return false;
                }
            }
            return true;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    总结

  • 相关阅读:
    LINUX挂载远程服务器上的目录到本地服务器
    关于写文章怎样才能制作出优质封面?看完这篇博客就够了(数千字手把手教学)
    神经网络控制器设计步骤,神经网络控制系统设计
    Ubuntu20.04离线安装Vmware tools
    园区写字楼都在用的物业管理系统
    Kotlin学习之密封类
    学会C++之后,为什么学任何语言都会更加容易?
    跑男策划书
    【面试题精讲】Object类的常见方法有哪些?
    网安学习-Python3
  • 原文地址:https://blog.csdn.net/weixin_39348931/article/details/125399219
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号