JAVA easy using Kahn's algorithms
```\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.LinkedList;\nimport java.util.List;\nimport java.util.Map;\nimport java.util.Queue
```\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.LinkedList;\nimport java.util.List;\nimport java.util.Map;\nimport java.util.Queue;\n\npublic class Solution {\n\n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        List<Integer> rowOrder = kahn(rowConditions, k + 1); \n        List<Integer> colOrder = kahn(colConditions, k + 1); \n        \n        if (rowOrder.size() < k || colOrder.size() < k)\n            return new int[][] {};\n\n        Map<Integer, Integer> colMap = new HashMap<>();\n        for (int i = 0; i < k; i++) {\n            colMap.put(colOrder.get(i), i);\n        }\n\n        int[][] ans = new int[k][k];\n\n        for (int i = 0; i < k; i++) {\n            int columnPosition = colMap.get(rowOrder.get(i));\n            ans[i][columnPosition] = rowOrder.get(i);\n        }\n\n        return ans;\n    }\n\n    public List<Integer> kahn(int[][] conditions, int k) {\n        List<Integer>[] graph = new List[k];\n        int[] degree = new int[k];\n        List<Integer> ans = new ArrayList<>();\n        Queue<Integer> q = new LinkedList<>();\n\n        for (int i = 0; i < k; i++) {\n            graph[i] = new ArrayList<>();\n        }\n\n        for (int[] condition : conditions) {\n            graph[condition[0]].add(condition[1]);\n            degree[condition[1]]++;\n        }\n\n        for (int i = 1; i < k; i++) {\n            if (degree[i] == 0) {\n                q.offer(i);\n            }\n        }\n\n        while (!q.isEmpty()) {\n            int node = q.poll();\n            ans.add(node);\n            for (int neighbour : graph[node]) {\n                degree[neighbour]--;\n                if (degree[neighbour] == 0) {\n                    q.offer(neighbour);\n                }\n            }\n        }\n\n        return ans;\n    }\n}
[]