Rust - Kahn's Algorithm with Matrix Manipulation
Solution - github\n\nProblem List\n#KahnsAlgorithm - github\n#MatrixManipulation - github\n\n#DirectedGraph - github\n#DetectCycle-Graph - github\n#Graph - gith
Solution - [github](https://github.com/An7One/lc_soln_rust_leon/tree/main/src/leetcode/lvl4/lc2392)\n\n<b>Problem List</b>\n#KahnsAlgorithm - [github](https://github.com/An7One/leetcode-problems-by-tag-an7one/blob/main/txt/by_algorithm/search/breath_first_search/by_algorithm/kahns_algorithm.txt)\n#MatrixManipulation - [github](https://github.com/An7One/leetcode-problems-by-tag-an7one/tree/main/txt/by_data_structure/array/by_topic/manipulation/matrix)\n\n#DirectedGraph - [github](https://github.com/An7One/leetcode-problems-by-tag-an7one/tree/main/txt/by_data_structure/graph/by_data_structure/directed_graph)\n#DetectCycle-Graph - [github](https://github.com/An7One/leetcode-problems-by-tag-an7one/blob/main/txt/by_data_structure/graph/by_topic/detect_cycle.txt)\n#Graph - [github](https://github.com/An7One/leetcode-problems-by-tag-an7one/tree/main/txt/by_data_structure/graph)\n\n```\nuse std::collections::{HashMap, VecDeque};\n\n/// @author: Leon\n/// https://leetcode.com/problems/build-a-matrix-with-conditions/\n/// Time Complexity:    O(V + E) + O(`k`) ~ O(`k` + `_len_rcs` + `_len_ccs`)\n/// Space Complexity:   O(V + E) + O(`k`) ~ O(`k` + `_len_rcs` + `_len_ccs`)\nimpl Solution {\n    pub fn build_matrix(\n        k: i32,\n        row_conditions: Vec<Vec<i32>>,\n        col_conditions: Vec<Vec<i32>>,\n    ) -> Vec<Vec<i32>> {\n\t    let _len_rcs: usize = row_conditions.len();\n        let _len_ccs: usize = col_conditions.len();\n        let k: usize = k as usize;\n        let n: usize = k + 1;\n        let (is_cyclic_r, map_r) = {\n            let (graph, mut indegrees) = Self::build_graph(&row_conditions, n);\n            Self::is_cyclic(&graph, &mut indegrees)\n        };\n        if is_cyclic_r {\n            return Vec::new();\n        }\n        let (is_cyclic_c, map_c) = {\n            let (graph, mut indegrees) = Self::build_graph(&col_conditions, n);\n            Self::is_cyclic(&graph, &mut indegrees)\n        };\n        if is_cyclic_c {\n            return Vec::new();\n        }\n        return Self::build_mat_simple(&map_r, &map_c, k);\n    }\n    /// to be improved\n    /// this could pass for now,\n    /// because of test cases being weak\n    fn build_mat_simple(\n        map_r: &HashMap<i32, usize>,\n        map_c: &HashMap<i32, usize>,\n        k: usize,\n    ) -> Vec<Vec<i32>> {\n        let mut ans: Vec<Vec<i32>> = vec![vec![0; k]; k];\n        for num in 1..=k as i32 {\n            let r: usize = *map_r.get(&num).unwrap();\n            let c: usize = *map_c.get(&num).unwrap();\n            ans[r][c] = num;\n        }\n        return ans;\n    }\n    fn is_cyclic(graph: &Vec<Vec<usize>>, indegrees: &mut Vec<u16>) -> (bool, HashMap<i32, usize>) {\n        let len_vs: usize = indegrees.len();\n        let mut queue: VecDeque<usize> = {\n            let mut queue = VecDeque::with_capacity(len_vs);\n            for idx in 1..len_vs {\n                if indegrees[idx] == 0 {\n                    queue.push_back(idx);\n                }\n            }\n            queue\n        };\n        let mut sorted: Vec<i32> = Vec::with_capacity(len_vs);\n        while let Some(cur) = queue.pop_front() {\n            sorted.push(cur as i32);\n            for &nxt in &graph[cur] {\n                indegrees[nxt] -= 1;\n                if indegrees[nxt] == 0 {\n                    queue.push_back(nxt);\n                }\n            }\n        }\n        if sorted.len() < len_vs - 1 {\n            return (true, HashMap::new());\n        }\n        let num_to_idx: HashMap<i32, usize> = {\n            let mut map: HashMap<i32, usize> = HashMap::with_capacity(len_vs);\n            for (idx, num) in sorted.into_iter().enumerate() {\n                map.insert(num, idx);\n            }\n            map\n        };\n        return (false, num_to_idx);\n    }\n    fn build_graph(edges: &Vec<Vec<i32>>, n: usize) -> (Vec<Vec<usize>>, Vec<u16>) {\n        let mut graph: Vec<Vec<usize>> = vec![Vec::with_capacity(n); n];\n        let mut indegrees: Vec<u16> = vec![0; n];\n        for edge in edges {\n            let from: usize = edge[0] as usize;\n            let to: usize = edge[1] as usize;\n            graph[from].push(to);\n            indegrees[to] += 1;\n        }\n        return (graph, indegrees);\n    }\n}\n```
['Topological Sort', 'Rust']