Skip to content

Commit 4b25395

Browse files
committed
feat: add No.210,785
1 parent 24976d4 commit 4b25395

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

201-300/210. Course Schedule II.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# 210. Course Schedule II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Depth-First Search, Breadth-First Search, Graph, Topological Sort.
5+
- Similar Questions: Course Schedule, Alien Dictionary, Minimum Height Trees, Sequence Reconstruction, Course Schedule III, Parallel Courses, Find All Possible Recipes from Given Supplies, Build a Matrix With Conditions, Sort Array by Moving Items to Empty Space.
6+
7+
## Problem
8+
9+
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
10+
11+
12+
13+
- For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.
14+
15+
16+
Return **the ordering of courses you should take to finish all courses**. If there are many valid answers, return **any** of them. If it is impossible to finish all courses, return **an empty array**.
17+
18+
 
19+
Example 1:
20+
21+
```
22+
Input: numCourses = 2, prerequisites = [[1,0]]
23+
Output: [0,1]
24+
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
25+
```
26+
27+
Example 2:
28+
29+
```
30+
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
31+
Output: [0,2,1,3]
32+
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
33+
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
34+
```
35+
36+
Example 3:
37+
38+
```
39+
Input: numCourses = 1, prerequisites = []
40+
Output: [0]
41+
```
42+
43+
 
44+
**Constraints:**
45+
46+
47+
48+
- `1 <= numCourses <= 2000`
49+
50+
- `0 <= prerequisites.length <= numCourses * (numCourses - 1)`
51+
52+
- `prerequisites[i].length == 2`
53+
54+
- `0 <= ai, bi < numCourses`
55+
56+
- `ai != bi`
57+
58+
- All the pairs `[ai, bi]` are **distinct**.
59+
60+
61+
62+
## Solution
63+
64+
```javascript
65+
/**
66+
* @param {number} numCourses
67+
* @param {number[][]} prerequisites
68+
* @return {number[]}
69+
*/
70+
var findOrder = function(numCourses, prerequisites) {
71+
var requiredByMap = Array(numCourses).fill(0).map(() => []);
72+
var requiringMap = Array(numCourses).fill(0);
73+
for (var i = 0; i < prerequisites.length; i++) {
74+
requiringMap[prerequisites[i][0]]++;
75+
requiredByMap[prerequisites[i][1]].push(prerequisites[i][0]);
76+
}
77+
var queue = new MinPriorityQueue();
78+
for (var j = 0; j < numCourses; j++) {
79+
queue.enqueue(j, requiringMap[j]);
80+
}
81+
var res = [];
82+
while (queue.size()) {
83+
var item = queue.dequeue();
84+
if (requiringMap[item.element] !== item.priority) continue;
85+
if (item.priority !== 0) return [];
86+
res.push(item.element);
87+
for (var k = 0; k < requiredByMap[item.element].length; k++) {
88+
requiringMap[requiredByMap[item.element][k]]--;
89+
queue.enqueue(requiredByMap[item.element][k], requiringMap[requiredByMap[item.element][k]]);
90+
}
91+
}
92+
return res;
93+
};
94+
```
95+
96+
**Explain:**
97+
98+
nope.
99+
100+
**Complexity:**
101+
102+
* Time complexity : O(n * log(n)).
103+
* Space complexity : O(n).

701-800/785. Is Graph Bipartite.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 785. Is Graph Bipartite?
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Depth-First Search, Breadth-First Search, Union Find, Graph.
5+
- Similar Questions: Divide Nodes Into the Maximum Number of Groups.
6+
7+
## Problem
8+
9+
There is an **undirected** graph with `n` nodes, where each node is numbered between `0` and `n - 1`. You are given a 2D array `graph`, where `graph[u]` is an array of nodes that node `u` is adjacent to. More formally, for each `v` in `graph[u]`, there is an undirected edge between node `u` and node `v`. The graph has the following properties:
10+
11+
12+
13+
- There are no self-edges (`graph[u]` does not contain `u`).
14+
15+
- There are no parallel edges (`graph[u]` does not contain duplicate values).
16+
17+
- If `v` is in `graph[u]`, then `u` is in `graph[v]` (the graph is undirected).
18+
19+
- The graph may not be connected, meaning there may be two nodes `u` and `v` such that there is no path between them.
20+
21+
22+
A graph is **bipartite** if the nodes can be partitioned into two independent sets `A` and `B` such that **every** edge in the graph connects a node in set `A` and a node in set `B`.
23+
24+
Return `true`** if and only if it is **bipartite****.
25+
26+
 
27+
Example 1:
28+
29+
![](https://assets.leetcode.com/uploads/2020/10/21/bi2.jpg)
30+
31+
```
32+
Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
33+
Output: false
34+
Explanation: There is no way to partition the nodes into two independent sets such that every edge connects a node in one and a node in the other.
35+
```
36+
37+
Example 2:
38+
39+
![](https://assets.leetcode.com/uploads/2020/10/21/bi1.jpg)
40+
41+
```
42+
Input: graph = [[1,3],[0,2],[1,3],[0,2]]
43+
Output: true
44+
Explanation: We can partition the nodes into two sets: {0, 2} and {1, 3}.
45+
```
46+
47+
 
48+
**Constraints:**
49+
50+
51+
52+
- `graph.length == n`
53+
54+
- `1 <= n <= 100`
55+
56+
- `0 <= graph[u].length < n`
57+
58+
- `0 <= graph[u][i] <= n - 1`
59+
60+
- `graph[u]` does not contain `u`.
61+
62+
- All the values of `graph[u]` are **unique**.
63+
64+
- If `graph[u]` contains `v`, then `graph[v]` contains `u`.
65+
66+
67+
68+
## Solution
69+
70+
```javascript
71+
/**
72+
* @param {number[][]} graph
73+
* @return {boolean}
74+
*/
75+
var isBipartite = function(graph) {
76+
var map = {};
77+
for (var i = 0; i < graph.length; i++) {
78+
if (!dfs(graph, map, i)) return false;
79+
}
80+
return true;
81+
};
82+
83+
var dfs = function(graph, map, i, group) {
84+
if (map[i]) return !group || group === map[i];
85+
map[i] = group || 1;
86+
for (var j = 0; j < graph[i].length; j++) {
87+
if (!dfs(graph, map, graph[i][j], map[i] === 1 ? 2 : 1)) return false;
88+
}
89+
return true;
90+
};
91+
```
92+
93+
**Explain:**
94+
95+
DFS with memorize.
96+
97+
**Complexity:**
98+
99+
* Time complexity : O(n).
100+
* Space complexity : O(n).

0 commit comments

Comments
 (0)