Why my solution does not work?


  • -4
    S

    public class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
    int[] res = new int[numCourses];
    int[][] courses = new int[numCourses][numCourses];
    int[] indegree = new int[numCourses];
    for (int i = 0; i < prerequisites.length; i++){
    int first = prerequisites[i][0];
    int second = prerequisites[i][1];
    if (courses[second][first] == 0) indegree[first]++;
    courses[second][first] = 1;
    }
    int count = 0;
    Queue<Integer> queue = new LinkedList();
    for (int i = 0; i < indegree.length; i++){
    if (indegree[i] == 0){
    queue.offer(i);
    }
    }
    while (!queue.isEmpty()){
    int c = queue.poll();
    res[count++] = c;
    for (int i = 0; i < numCourses; i++){
    if (courses[c][i] != 0){
    if (--indegree[i] == 0) queue.offer(i);
    }
    }
    }
    if (count < numCourses) return new int[0];
    else return res;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.