-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lcof2 problem: No.113 (#2474)
No.113.课程顺序
- Loading branch information
Showing
5 changed files
with
88 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
public class Solution { | ||
public int[] FindOrder(int numCourses, int[][] prerequisites) { | ||
var g = new List<int>[numCourses]; | ||
for (int i = 0; i < numCourses; ++i) | ||
{ | ||
List<int>[] g = new List<int>[numCourses]; | ||
for (int i = 0; i < numCourses; i++) { | ||
g[i] = new List<int>(); | ||
} | ||
var indeg = new int[numCourses]; | ||
foreach (var p in prerequisites) | ||
{ | ||
int[] indeg = new int[numCourses]; | ||
foreach (var p in prerequisites) { | ||
int a = p[0], b = p[1]; | ||
g[b].Add(a); | ||
++indeg[a]; | ||
} | ||
var q = new Queue<int>(); | ||
for (int i = 0; i < numCourses; ++i) | ||
{ | ||
if (indeg[i] == 0) q.Enqueue(i); | ||
Queue<int> q = new Queue<int>(); | ||
for (int i = 0; i < numCourses; ++i) { | ||
if (indeg[i] == 0) { | ||
q.Enqueue(i); | ||
} | ||
} | ||
var ans = new int[numCourses]; | ||
var cnt = 0; | ||
while (q.Count > 0) | ||
{ | ||
int[] ans = new int[numCourses]; | ||
int cnt = 0; | ||
while (q.Count > 0) { | ||
int i = q.Dequeue(); | ||
ans[cnt++] = i; | ||
foreach (int j in g[i]) | ||
{ | ||
if (--indeg[j] == 0) q.Enqueue(j); | ||
foreach (int j in g[i]) { | ||
if (--indeg[j] == 0) { | ||
q.Enqueue(j); | ||
} | ||
} | ||
} | ||
return cnt == numCourses ? ans : new int[0]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,20 @@ | ||
function findOrder(numCourses: number, prerequisites: number[][]): number[] { | ||
let g = Array.from({ length: numCourses }, () => []); | ||
let indeg = new Array(numCourses).fill(0); | ||
for (let [a, b] of prerequisites) { | ||
const g: number[][] = Array.from({ length: numCourses }, () => []); | ||
const indeg: number[] = Array(numCourses).fill(0); | ||
for (const [a, b] of prerequisites) { | ||
g[b].push(a); | ||
++indeg[a]; | ||
} | ||
let q = []; | ||
for (let i = 0; i < numCourses; ++i) { | ||
if (!indeg[i]) { | ||
q.push(i); | ||
} | ||
} | ||
let ans = []; | ||
const q: number[] = indeg.map((v, i) => (v === 0 ? i : -1)).filter(v => v !== -1); | ||
const ans: number[] = []; | ||
while (q.length) { | ||
const i = q.shift(); | ||
const i = q.pop()!; | ||
ans.push(i); | ||
for (let j of g[i]) { | ||
if (--indeg[j] == 0) { | ||
for (const j of g[i]) { | ||
if (--indeg[j] === 0) { | ||
q.push(j); | ||
} | ||
} | ||
} | ||
return ans.length == numCourses ? ans : []; | ||
return ans.length === numCourses ? ans : []; | ||
} |