查看: 1969| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[树/链表/图] LC Course Schedule, 两种DFS,一种TLE,另一种没有,请教一下原因

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  1. // 1.DFS(HashMap to store graph)
  2.     public boolean canFinish(int numCourses, int[][] prerequisites) {
  3.         if(prerequisites == null || prerequisites.length == 0) return true;
  4.         
  5.         // Construct graph
  6.         HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
  7.         for(int i = 0; i < prerequisites.length; i++){
  8.             int from = prerequisites[i][1];
  9.             int to = prerequisites[i][0];
  10.             
  11.             if(!map.containsKey(from)){
  12.                 map.put(from, new ArrayList<Integer>());
  13.             }
  14.             
  15.             List<Integer> list = map.get(from);
  16.             list.add(to);
  17.             map.put(from, list);
  18.         }
  19.         
  20.         // detect cycle one by one using DFS: not visited && has cycle
  21.         int[] visited = new int[numCourses];
  22.         for(int from = 0; from < numCourses; from++){
  23.             if(visited[from] == 0 && hasCycle(from, visited, map)){
  24.                 return false;
  25.             }
  26.         }
  27.         return true;
  28.     }
  29.    
  30.     private boolean hasCycle(int from, int[] visited, HashMap<Integer, List<Integer>> map){
  31.         if(visited[from] == 1) return true;  //from has already visited
  32.         visited[from] = 1;
  33.    
  34.         // means from is several other courses' prerequisite
  35.         if(map.containsKey(from)){
  36.             for(Integer to : map.get(from)){
  37.                 if(visited[to] == 1) return true;   //to has already visited
  38.                 if(hasCycle(to, visited, map)) return true;    //to is in cycle
  39.             }
  40.         }
  41.         
  42.         visited[from] = -1;   // finish visit
  43.         return false;
  44.     }
复制代码
  1. // 2.DFS(Using Matrix to store Graph)
  2.     public boolean canFinish(int numCourses, int[][] prerequisites) {
  3.         if(prerequisites == null || prerequisites.length == 0) return true;
  4.         
  5.         // construct graph
  6.         int[][] graph = new int[numCourses][numCourses];
  7.         for(int i = 0; i < prerequisites.length; i++){
  8.             int from = prerequisites[i][1];
  9.             int to = prerequisites[i][0];
  10.             graph[from][to] = 1;
  11.         }
  12.         
  13.         // detect cycle recursive
  14.         int[] visited = new int[numCourses];
  15.         for(int from = 0; from < numCourses; from++){
  16.             if(visited[from] == 0 && hasCycle(from, graph, visited)){
  17.                 return false;   //there is cycle
  18.             }
  19.         }
  20.         
  21.         return true;
  22.     }
  23.    
  24.     private boolean hasCycle(int from, int[][] graph, int[] visited){
  25.         if(visited[from] == 1) return true;     //has already visited from, there is cycle
  26.         visited[from] = 1;
  27.         
  28.         for(int to = 0; to < graph[0].length; to++){
  29.             if(graph[from][to] == 1){
  30.                 if(visited[to] == 1) return true;   //has already visited
  31.                 if(hasCycle(to, graph, visited)) return true;
  32.             }
  33.         }
  34.         
  35.         visited[from] = -1; //finish visited
  36.         return false;
  37.     }
复制代码
这题我用两种方式构建有向图,用HashMap构建可以AC,用Matrix构建就会TLE。大家知道为什么吗?
原题: https://leetcode.com/problems/course-schedule/

上一篇:Valid Anagram 编码问题
下一篇:CC150+Leetcode刷题实时记录-欢迎加入
🔗
ryancooper 2016-2-3 00:06:49 | 只看该作者
全局:
稀疏图的话,matrix包含很多无用信息
回复

使用道具 举报

🔗
desperate500 2016-2-3 00:21:16 | 只看该作者
全局:
哟呵 小伙子 可以可以
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表