0

This is leetcode 210
There are a total of n courses you have to take labelled from 0 to n - 1.

Some courses may have prerequisites, for example, if prerequisites[i] = [ai, bi] this means you must take the course bi before the course ai.

Given the total number of courses numCourses and a list of the prerequisite pairs, 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.

Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] Output: [0,2,1,3]

When ever I submit this code below I get the runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h) Would appreciate if someone could help me with this!

class Solution {
public:
    vector<int> adj[2002];
    bool vis[2002];
    vector <int> myvect;
    void dfs(int node)
    {
        if(!vis[node])
        {
          for(int i = 0; i < (int)adj[node].size(); i++)
          {
              if(!vis[adj[node][i]])
                  dfs(vis[adj[node][i]]);
          }
           myvect.push_back(node);
        }
        
        
    }
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
    {
       
        vector<int> adj[2002];
        int n = prerequisites.size();
        int m = prerequisites[0].size(); 
        if(n == 0 && m == 0)
        {
            for(int i = 0; i < numCourses; i++){
                myvect.push_back(i);
            }
            return myvect;
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                adj[i].push_back(j);
        }
        
        for(int i = 0; i < numCourses; i++)
            dfs(i);
        return myvect;
        
        
    }
};
2
  • 1
    Hi, not sure if this might be of interest stackoverflow.com/questions/47947956/… Commented Sep 8, 2020 at 16:30
  • 1
    You never initialize vis, so reading from it has undefined behaviour. Commented Sep 8, 2020 at 17:16

1 Answer 1

1
  • You only have a small typo here: return vect to return myvect.

  • Another small bug is here: int m = n ? prerequisites[0].size() : 0;

  • After fixing these bugs, the algorithm partially works fine, does not work for all test cases though, not sure how to fix that. ( ˆ_ˆ )

class Solution {
    public:
        vector<int> adj[2002];
        bool vis[2002];
        vector <int> myvect;
        void dfs(int node) {
            if (!vis[node]) {
                for (int i = 0; i < (int)adj[node].size(); i++) {
                    if (!vis[adj[node][i]]) {
                        dfs(vis[adj[node][i]]);
                    }
                }

                myvect.push_back(node);
            }


        }
        vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {

            vector<int> adj[2002];
            int n = prerequisites.size();
            int m = n ? prerequisites[0].size() : 0;

            if (n == 0 && m == 0) {
                for (int i = 0; i < numCourses; i++) {
                    myvect.push_back(i);
                }

                return myvect;
            }

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    adj[i].push_back(j);
                }
            }

            for (int i = 0; i < numCourses; i++) {
                dfs(i);
            }

            return myvect;


        }
};
  • That set aside, we can also name our variables maybe a bit more descriptive:
// The following block might slightly improve the execution time;
// Can be removed;
static const auto __optimize__ = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    return 0;
}();

// Most of headers are already included;
// Can be removed;
#include <cstdint>
#include <vector>

using ValueType = std::uint_fast16_t;
using Graph = std::vector<std::vector<ValueType>>;

static const struct Solution {
        static const std::vector<int> findOrder(
            const int num_courses,
            const std::vector<std::vector<int>>& prerequisites
        ) {
            const Graph graph = buildGraph(num_courses, prerequisites);
            std::vector<ValueType> indegrees = getIndegrees(graph);
            std::vector<int> orders;

            for (ValueType i = 0; i < num_courses; ++i) {
                ValueType j = 0;

                for (; j < num_courses; j++) {
                    if (!indegrees[j]) {
                        orders.emplace_back(j);
                        break;
                    }
                }

                if (j == num_courses) {
                    return {};
                }

                --indegrees[j];

                for (const auto& edge : graph[j]) {
                    --indegrees[edge];
                }
            }

            return orders;
        }

    private:
        
        static const Graph buildGraph(
            const int num_courses,
            const std::vector<std::vector<int>>& prerequisites
        ) {
            Graph graph(num_courses);

            for (const auto& prerequisite : prerequisites) {
                graph[prerequisite[1]].emplace_back(prerequisite[0]);
            }

            return graph;
        }

        static const std::vector<ValueType> getIndegrees(
            const Graph& graph
        ) {
            std::vector<ValueType> indegrees(std::size(graph), 0);

            for (const auto& adj_edges : graph) {
                for (const auto& edge : adj_edges) {
                    ++indegrees[edge];
                }
            }

            return indegrees;
        }

};
Sign up to request clarification or add additional context in comments.

2 Comments

When I pasted your more descriptive variables code it got accepted!
Looking into the test cases! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.