码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【英雄哥六月集训】第 30天: 拓扑排序


    系列文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    【英雄哥六月集训】第 22天: 有序集合

    【英雄哥六月集训】第 23天: 字典树

    【英雄哥六月集训】第 24天: 线段树

    【英雄哥六月集训】第 25天: 树状数组

    【英雄哥六月集训】第 26天: 并查集

    【英雄哥六月集训】第 27天: 图

    【英雄哥六月集训】第 28天: 动态规划

    【英雄哥六月集训】第 29天: 分而治之

    【英雄哥六月集训】第 30天: 拓扑排序

    文章目录

    • 系列文章
    • 拓扑排序
    • 一、 从给定原材料中找到所有可以做出的菜
    • 二、 重建序列
    • 三、 课程表 IV
    • 四、 外星文字典
    • 总结


    拓扑排序

    对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边<u,v>∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。简单的说,由某个集合上的一个偏序得到该集合上的一个全序,这个操作称之为拓扑排序。

    一、 从给定原材料中找到所有可以做出的菜

    2115. 从给定原材料中找到所有可以做出的菜

    class Solution {
        Map<String,Boolean> has = new HashMap();
        public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
            int i,j;
            List<String> ans = new ArrayList<String>();
            for(i=0;i<supplies.length;++i){
                has.put(supplies[i],true);
            }
            boolean flag=true;
            while(flag){
                flag=false;
                for(i=0;i<recipes.length;i++){
                    if(has.containsKey(recipes[i])){
                        continue;
                    }
                    for(j=ingredients.get(i).size()-1;j>=0;--j){
                        if(!has.containsKey(ingredients.get(i).get(j))){
                            break;
                        }
                    }
                    if(j==-1){
                        has.put(recipes[i],true);
                        ans.add(recipes[i]);
                        flag=true;
    
                    }
                }
            }
            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

    二、 重建序列

    剑指 Offer II 115. 重建序列

    class Solution {
        int maxn=10010;
        int[] deg=new int[maxn];
        List<List<Integer>> edge = new ArrayList<List<Integer>>(maxn);
        public void initEdgeArray(){
            for(int i=0;i<maxn;i++){
                edge.add(new ArrayList<Integer>());
            }
        }
        public void addEdge(int u,int v){
            ++deg[v];
            edge.get(u).add(v);
        }
        public boolean sequenceReconstruction(int[] nums, int[][] seqs) {
            int i,j;
            Deque<Integer> q = new ArrayDeque<Integer>();
            List<Integer> ans = new ArrayList<Integer>();
            initEdgeArray();
            for(i=0;i<seqs.length;++i){
                for(j=1;j<seqs[i].length;++j){
                    addEdge(seqs[i][j-1],seqs[i][j]);
                }
            }
            
    
            for(i=1;i<=nums.length;++i){
                if(deg[i]==0){
                    q.add(i);
                    ans.add(i);
                }
            }
            
            if(q.size()>1){
                return false;
            }
            
            while(!q.isEmpty()){
                System.out.println(q);
                Integer u = q.removeFirst();
                for(i=0;i<edge.get(u).size();++i){
                    Integer v = edge.get(u).get(i);
                    if((--deg[v])==0){
                        q.add(v);
                        ans.add(v);
                    }
                }
                if(q.size()>1){
                    return false;
                }
            }
            //System.out.println(ans);
            if(nums.length>ans.size()){
                return false;
            }
            for(i=0;i<nums.length;++i){
                if(ans.get(i)!=nums[i]){
                    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
    • 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

    三、 课程表 IV

    1462. 课程表 IV
    先用拓扑排序写了一版,结果超时,实在不会改了

    class Solution {
        int maxn=110;
        Map<Integer, List<Integer>> edges = new HashMap<Integer,List<Integer>>();
        Map<Integer, List<Integer>> go = new HashMap<Integer,List<Integer>>();
        int[] vis = new int[maxn];
        
    
        void addEdge(int u,int v){
            edges.putIfAbsent(u,new ArrayList<Integer>());
            edges.get(u).add(v);
        }
        void dfs(int u,List<Integer> go){
            //{0=[1, 2], 2=[1]} 对于 0,1,2 来说的通路
            
            //vis[go.get(u)]=1;
                //System.out.println(u);
            go.add(u);
                
            if(edges.containsKey(u)){
                for(int i =0;i<edges.get(u).size();++i){
                    //if (vis[edges.get(u).get(i)]==0){
                        dfs(edges.get(u).get(i),go);
                    //}
                }
                
            }
        
        }
    
        public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
            int i,j;
            List<Boolean> ans=new ArrayList<Boolean>();
            for(i=0;i<prerequisites.length;++i){
                int u = prerequisites[i][0];
                int v = prerequisites[i][1];
                addEdge(v,u);
            }
            //System.out.println(edges); //{0=[1, 2], 2=[1]}
            for(i=0;i<numCourses;++i){
                go.putIfAbsent(i,new ArrayList<Integer>());
                dfs(i,go.get(i));  
            }
            System.out.println(go);
            for(i =0;i<queries.length;++i){
                int u = queries[i][0];
                int v = queries[i][1];
                Boolean flag = false;
                
                for(j=0;j<go.get(v).size();++j){
                    if(go.get(v).get(j)==u){
                        flag=true;
                    }
                }
                ans.add(flag);
            }
            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
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    最后还是用了 floyed 算法

    class Solution {
        public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
    
              /*
                   floyed[i][j] 
                   if there is path from i to j
    
              */
    
              boolean[][] floyed = new boolean[numCourses][numCourses];
    
              for(int [] pre : prerequisites){
                   floyed[pre[0]][pre[1]] = true;
              }
    
              for(int mid = 0; mid < numCourses; mid++){
    
                   for(int i = 0; i < numCourses; i++){
    
                        for(int j = 0; j < numCourses; j++){
    
                             floyed[i][j] = floyed[i][j]|| (floyed[i][mid] && floyed[mid][j]);
                        }
                   }
              }
    
              List<Boolean> result = new ArrayList<>();
    
              for(int[] query : queries){
    
                   int i = query[0];
                   int j = query[1];
    
                   result.add(floyed[i][j]);
              }
              return result;
    
        }
    }
    
    
    • 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

    四、 外星文字典

    剑指 Offer II 114. 外星文字典

    class Solution {
        static final int VISITING = 1,VISITED = 2;
        Map<Character, List<Character>> edges = new HashMap<Character,List<Character>>();
        Map<Character, Integer> states = new HashMap<Character,Integer>();
        boolean valid = true;
        char[] order;
        int index;
    
        void addEdge(String before, String after){
            int length1 = before.length(),length2 = after.length();
            int length = Math.min(length1,length2);
            int index = 0;
            while(index < length){
                char c1 = before.charAt(index), c2 = after.charAt(index);
                if(c1 != c2){
                    edges.get(c1).add(c2);
                    break;
                }
                index++;
            }
            if(index == length && length1>length2){
                valid = false;
            }
        }
        void dfs(char u){
            states.put(u,VISITING);
            List<Character> adjacent = edges.get(u);
            for(char v:adjacent){
                if(!states.containsKey(v)){
                    dfs(v);
                    if(!valid){
                        return ;
                    }
                } else if(states.get(v)==VISITING){
                    valid = false;
                    return ;
                }
            }
            states.put(u,VISITED);
            order[index]=u;
            index--;
        }
        public String alienOrder(String[] words) {
            int length = words.length;
            for( String word: words){
                int wordLength = word.length();
                for( int j=0;j<wordLength; j++){
                    char c = word.charAt(j);
                    edges.putIfAbsent(c,new ArrayList<Character>());
                }
            }
            for( int i=1;i<length&& valid ;i++){
                addEdge(words[i-1],words[i]);
            }
            order = new char[edges.size()];
            index = edges.size()-1;
            Set<Character> letterSet = edges.keySet();
            for(char u:letterSet){
                if(!states.containsKey(u)){
                    dfs(u);
                }
            }
            if(!valid){
                return "";
            }
            return new String(order);
    
        }
    }
    
    • 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

    总结

  • 相关阅读:
    windows nignx 常用操作命令(启动、停止、重启服务)
    FastAPI 学习之路(二十二)依赖项
    MSF后渗透总结
    pandas学习
    mysql学习笔记--单张表上的增删改查
    【SSM】Mybatis01
    Qt QtCreator调试Qt源码配置
    数据仓库之数据冗余规范
    算法排序之冒泡排序及优化
    CentOS - 安装 Elasticsearch
  • 原文地址:https://blog.csdn.net/weixin_39348931/article/details/125548661
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号