✅NOT SO EASY JAVA SOLUTION😂
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        List<Integer> row_sorting = topo_sort(rowConditions, k);\n        List<Integer> col_sorting = topo_sort(colConditions, k);\n        if (row_sorting.isEmpty() || col_sorting.isEmpty())\n            return new int[0][0];\n\n        Map<Integer, int[]> value_position = new HashMap<>();\n        for (int n = 1; n <= k; ++n) {\n            value_position.put(n, new int[2]);  \n        }\n        for (int ind = 0; ind < row_sorting.size(); ++ind) {\n            value_position.get(row_sorting.get(ind))[0] = ind;\n        }\n        for (int ind = 0; ind < col_sorting.size(); ++ind) {\n            value_position.get(col_sorting.get(ind))[1] = ind;\n        }\n\n        int[][] res = new int[k][k];\n        for (int value = 1; value <= k; ++value) {\n            int row = value_position.get(value)[0];\n            int column = value_position.get(value)[1];\n            res[row][column] = value;\n        }\n\n        return res;\n    }\n\n  \n    private boolean dfs(int src, Map<Integer, List<Integer>> graph, Set<Integer> visited, Set<Integer> cur_path, List<Integer> res) {\n        if (cur_path.contains(src)) return false; \n        if (visited.contains(src)) return true; \n\n        visited.add(src);\n        cur_path.add(src);\n\n        for (int neighbor : graph.getOrDefault(src, Collections.emptyList())) {\n            if (!dfs(neighbor, graph, visited, cur_path, res)) \n                return false;\n        }\n\n        cur_path.remove(src); \n        res.add(src);\n        return true;\n    }\n\n   \n    private List<Integer> topo_sort(int[][] edges, int k) {\n        Map<Integer, List<Integer>> graph = new HashMap<>();\n        for (int[] edge : edges) {\n            graph.computeIfAbsent(edge[0], x -> new ArrayList<>()).add(edge[1]);\n        }\n\n        Set<Integer> visited = new HashSet<>();\n        Set<Integer> cur_path = new HashSet<>();\n        List<Integer> res = new ArrayList<>();\n\n        for (int src = 1; src <= k; ++src) {\n            if (!dfs(src, graph, visited, cur_path, res))\n                return Collections.emptyList();\n        }\n\n        Collections.reverse(res); \n        return res;\n    }\n}\n```
['Java']