210. Course Schedule II

time: O(V+E), refer to https://www.educative.io/edpresso/what-is-topological-sort

space: O(n)

same as 207's solution, use topology sort , just when

record the result

and at last check res'length == numCouses

這個例子 , 照理說 indegree 都會是 1 => indegree: [1, 1], so res[] 不應該有計算過, 但是要注意到...

int res[] = new int[numCourses]; ⇒ [0, 0]

因為初始化, int 是基本型台, 所以整個陣列就會是 0, 所以如果 res size 是 2, 就會變成 [0, 0]

所以最後才要加上這行, 不然無法呈現 [] 的結果

return (k == numCourses) ? res : new int[0];

better one

new version

time: O(V+E), refer to https://www.educative.io/edpresso/what-is-topological-sort

space: O(n)

Last updated

Was this helpful?