2022-1-14图day2(2022-1-14 figure Day2)

题1:

207. 课程表labuladong 题解思路

你这个学期必须选修  门课程,记为  到  。

numCourses
0
numCourses - 1

在选修某些课程之前需要一些先修课程。 先修课程按数组  给出,其中  ,表示如果要学习课程  则 必须 先学习课程   。

prerequisites
prerequisites[i] = [ai, bi]
ai
bi
  • 例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 。

请你判断是否可能完成所有课程的学习?如果可以,返回  ;否则,返回  。

true
false

示例 1:

输入:numCourses = 2, prerequisites = [[1,0]]
输出:true
解释:总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。

示例 2:

输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
输出:false
解释:总共有 2 门课程。学习课程 1 之前,你需要先完成课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。

提示:

  • 1 <= numCourses <= 105
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • prerequisites[i] 中的所有课程对 互不相同
 1 class Solution {
 2     boolean[] node;
 3     boolean[] visit;
 4     boolean hascircle=false;
 5     public boolean canFinish(int numCourses, int[][] prerequisites) {
 6         List<List<Integer>> list=build(numCourses,prerequisites);
 7         node = new boolean[numCourses];
 8         visit = new boolean[numCourses];
 9         for (int i=0;i<numCourses;i++) {
10             transer(i,list);
11             if (hascircle) return false;
12         }
13         return true;
14     }
15     //转化为邻接表
16     public List<List<Integer>> build(int n,int[][] prerequisites) {
17         List<List<Integer>> list=new ArrayList<>();
18         for (int i=0;i<n;i++) {
19             List<Integer> l=new ArrayList<>();
20             list.add(l);
21         }
22         for (int[] x:prerequisites) {
23             int start=x[0],end=x[1];
24             list.get(start).add(end);
25         }
26         return list;
27     }
28 
29     public void transer(int x,List<List<Integer>> list) {
30         if (node[x]) {
31             hascircle=true;
32         }
33         if (visit[x]||hascircle) {
34             return;
35         }
36         node[x]=true;
37         //遍历过x节点
38         visit[x]=true;
39         //经过x节点
40         for (int i=0;i<list.get(x).size();i++) {
41             transer(list.get(x).get(i),list);
42         }
43         node[x]=false;
44     }
45 }

思路:图的深度遍历,用visit数组记录遍历过的节点,node记录经过的节点。

题2:

210. 课程表 IIlabuladong 题解思路

现在你总共有  门课需要选,记为  到 。给你一个数组  ,其中  ,表示在选修课程  前 必须 先选修  。

numCourses
0
numCourses - 1
prerequisites
prerequisites[i] = [ai, bi]
ai
bi
  • 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示:[0,1] 。

返回你为了学完所有课程所安排的学习顺序。可能会有多个正确的顺序,你只要返回 任意一种 就可以了。如果不可能完成所有课程,返回 一个空数组 。

示例 1:

[0,1] 。

示例 2:

[0,1,2,3]
[0,2,1,3]

示例 3:

输入:numCourses = 1, prerequisites = []
输出:[0]
  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= numCourses * (numCourses - 1)
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • ai != bi
  • 所有[ai, bi] 互不相同
 1 class Solution {
 2     int[] in;
 3     public int[] findOrder(int numCourses, int[][] prerequisites) {
 4         in = new int[numCourses];
 5         List<List<Integer>> graph=build(numCourses,prerequisites);
 6         Queue<Integer> queue =new LinkedList<>();
 7         for (int i=0;i<numCourses;i++) {
 8             if (in[i]==0) queue.offer(i);
 9         }
10         int index=0;
11         int[] res=new int[numCourses];
12         while (!queue.isEmpty()) {
13             int temp=queue.poll();
14             res[index++]=temp;
15             for (int x:graph.get(temp)){
16                 in[x]--;
17                 if (in[x]==0) queue.offer(x);
18             }
19         }
20         return index==numCourses?res:new int[]{};
21     }
22 
23 
24     public List<List<Integer>> build(int n,int[][] prerequisites) {
25          List<List<Integer>> list=new ArrayList<>();
26         for (int i=0;i<n;i++) {
27              List<Integer> l=new ArrayList<>();
28              list.add(l);
29         }
30         for (int[] x:prerequisites) {
31             int start=x[0],end=x[1];
32             list.get(end).add(start);
33             in[start]++;
34         }
35         return list;
36     }
37 }

思路:拓扑排序。入度表+广度优先搜索。建立邻接表和入度表,将入度为0的进队列,每次出列并将连接的节点入度全部减1,如果入度为0就加入队列。

————————

Question 1:

207. 课程表labuladong 题解思路

You must take , courses this semester as , to.

numCourses
0
numCourses - 1

Some prerequisite courses are required before taking some courses. The prerequisite courses are given in the array , where , means that , if you want to learn the course , you must learn the course first.

prerequisites
prerequisites[i] = [ai, bi]
ai
bi
  • For example, the prerequisite course pair [0, 1] indicates that if you want to learn course {0, you need to complete course} 1 first.

Please judge whether it is possible to complete all courses? If possible, return; Otherwise, return.

true
false

Example 1:

输入:numCourses = 2, prerequisites = [[1,0]]
输出:true
解释:总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。

Example 2:

输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
输出:false
解释:总共有 2 门课程。学习课程 1 之前,你需要先完成课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。

Tips:

  • 1 <= numCourses <= 105
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • prerequisites[i] 中的所有课程对 互不相同
 1 class Solution {
 2     boolean[] node;
 3     boolean[] visit;
 4     boolean hascircle=false;
 5     public boolean canFinish(int numCourses, int[][] prerequisites) {
 6         List<List<Integer>> list=build(numCourses,prerequisites);
 7         node = new boolean[numCourses];
 8         visit = new boolean[numCourses];
 9         for (int i=0;i<numCourses;i++) {
10             transer(i,list);
11             if (hascircle) return false;
12         }
13         return true;
14     }
15     //转化为邻接表
16     public List<List<Integer>> build(int n,int[][] prerequisites) {
17         List<List<Integer>> list=new ArrayList<>();
18         for (int i=0;i<n;i++) {
19             List<Integer> l=new ArrayList<>();
20             list.add(l);
21         }
22         for (int[] x:prerequisites) {
23             int start=x[0],end=x[1];
24             list.get(start).add(end);
25         }
26         return list;
27     }
28 
29     public void transer(int x,List<List<Integer>> list) {
30         if (node[x]) {
31             hascircle=true;
32         }
33         if (visit[x]||hascircle) {
34             return;
35         }
36         node[x]=true;
37         //遍历过x节点
38         visit[x]=true;
39         //经过x节点
40         for (int i=0;i<list.get(x).size();i++) {
41             transer(list.get(x).get(i),list);
42         }
43         node[x]=false;
44     }
45 }

< strong > idea: for the depth traversal of the graph, use the visit array to record the traversed nodes, and node to record the passed nodes

Question 2:

210. 课程表 IIlabuladong 题解思路

Now you have a total of , courses to choose, remember , to. I’ll give you an array, in which, means that you must # take an elective course before # taking an elective course.

numCourses
0
numCourses - 1
prerequisites
prerequisites[i] = [ai, bi]
ai
bi
  • For example, to learn course 0, you need to complete course 1 first. We use a match to represent: [0,1].

Return to the learning sequence you arranged to complete all courses. There may be more than one correct order. You just need to return , any one of them. If it is not possible to complete all courses, an empty array is returned.

Example 1:

[0,1] 。

Example 2:

[0,1,2,3]
[0,2,1,3]

Example 3:

输入:numCourses = 1, prerequisites = []
输出:[0]
  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= numCourses * (numCourses - 1)
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • ai != bi
  • All [AI, Bi] are different from each other
 1 class Solution {
 2     int[] in;
 3     public int[] findOrder(int numCourses, int[][] prerequisites) {
 4         in = new int[numCourses];
 5         List<List<Integer>> graph=build(numCourses,prerequisites);
 6         Queue<Integer> queue =new LinkedList<>();
 7         for (int i=0;i<numCourses;i++) {
 8             if (in[i]==0) queue.offer(i);
 9         }
10         int index=0;
11         int[] res=new int[numCourses];
12         while (!queue.isEmpty()) {
13             int temp=queue.poll();
14             res[index++]=temp;
15             for (int x:graph.get(temp)){
16                 in[x]--;
17                 if (in[x]==0) queue.offer(x);
18             }
19         }
20         return index==numCourses?res:new int[]{};
21     }
22 
23 
24     public List<List<Integer>> build(int n,int[][] prerequisites) {
25          List<List<Integer>> list=new ArrayList<>();
26         for (int i=0;i<n;i++) {
27              List<Integer> l=new ArrayList<>();
28              list.add(l);
29         }
30         for (int[] x:prerequisites) {
31             int start=x[0],end=x[1];
32             list.get(end).add(start);
33             in[start]++;
34         }
35         return list;
36     }
37 }

< strong > idea: topological sorting. Entry table + breadth first search. Establish an adjacency table and a join degree table, queue the nodes with a join degree of 0, list them each time, and reduce the join degree of all connected nodes by 1. If the join degree is 0, join the queue