java solution using Topological sort | Graph ☑☑☑
\n// use for calculation index\nclass Node{\n    int i,j;\n    public Node(int i,int j){\n        this.i=i;\n        this.j=j;\n    }\n}\n\nclass Solution {\n  
```\n// use for calculation index\nclass Node{\n    int i,j;\n    public Node(int i,int j){\n        this.i=i;\n        this.j=j;\n    }\n}\n\nclass Solution {\n    \n    // detect cycle for directed graph\n    public boolean checkCycle(int u,Map<Integer,List<Integer>> adj,boolean []visited,boolean []curStack){\n        visited[u]=true;\n        curStack[u]=true;\n        // if u is not present in that time we hae not cycle that\'s why return false\n        if(!adj.containsKey(u)){\n            curStack[u]=false;\n            return false;\n        }\n        for(int v:adj.get(u)){\n            if(!visited[v] && checkCycle(v,adj,visited,curStack)){\n                return true;\n            }else if(curStack[v]){\n                return true;\n            }\n        }\n        curStack[u]=false;\n        return false;\n    }\n\n    // creating stack for topological sort list\n    public void solve(int u,Map<Integer,List<Integer>> adj,boolean visited[],Stack<Integer> store){\n        visited[u]=true;\n        if(adj.containsKey(u)){\n            for(int v:adj.get(u)){\n                if(!visited[v]){\n                    solve(v,adj,visited,store);\n                }\n            }\n        }\n        store.push(u);\n    }\n    \n    public List<Integer> topologicalSort(int[][] condition,int k){\n        Map<Integer,List<Integer>> adj = new HashMap<>();\n        boolean visited[]= new boolean[k+1];\n        Stack<Integer> store = new Stack<>();\n        \n        //create graph\n        for(int []arr:condition){\n            if(adj.containsKey(arr[0])){\n                adj.get(arr[0]).add(arr[1]);\n            }else{\n                List<Integer> temp = new ArrayList<>();\n                temp.add(arr[1]);\n                adj.put(arr[0],temp);\n            }\n        }\n        \n        // checking graph has cycle. if we have cycle return empty list\n        List<Integer> topoList = new ArrayList<>();\n        boolean curStack[] = new boolean[k+1];\n        for(int i=1;i<=k;i++){\n            if(!visited[i] && checkCycle(i,adj,visited,curStack)){\n                return topoList;\n            }\n        }\n        \n        //traversing for stack that use for topological sort List\n        visited= new boolean[k+1];\n        for(int i=1;i<=k;i++){\n            if(!visited[i]){\n                solve(i,adj,visited,store);\n            }\n        }\n        \n        //revering stack to get topological sorted list\n        while(store.size()>0){\n            topoList.add(store.pop());\n        }\n        return topoList;\n    }\n    \n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        int[][] ans = new int[k][k];\n        \n        // getting topological list\n        List<Integer> rowNums = topologicalSort(rowConditions,k);\n        List<Integer> colNums = topologicalSort(colConditions,k);\n        if(rowNums.isEmpty() || colNums.isEmpty()){\n            return new int[0][0];\n        }\n        System.out.println(rowNums.size());\n        System.out.println(colNums.size());\n        // calculating [row][col] for one element\n        HashMap<Integer,Node> index = new HashMap<>();\n        for(int i=0;i<k;i++){\n            index.put(rowNums.get(i),new Node(i,-1));\n        }\n        for(int i=0;i<k;i++){\n            index.get(colNums.get(i)).j=i;\n        }\n        for(int key:index.keySet()){\n            Node node = index.get(key);\n            ans[node.i][node.j]=key;\n        }\n        \n        return ans;\n    }\n}\n```
['Array', 'Graph', 'Topological Sort', 'Matrix', 'Java']