diff options
Diffstat (limited to 'sort-the-matrix-diagonally')
-rw-r--r-- | sort-the-matrix-diagonally/Cargo.toml | 8 | ||||
-rw-r--r-- | sort-the-matrix-diagonally/src/main.rs | 26 |
2 files changed, 34 insertions, 0 deletions
diff --git a/sort-the-matrix-diagonally/Cargo.toml b/sort-the-matrix-diagonally/Cargo.toml new file mode 100644 index 0000000..8e05371 --- /dev/null +++ b/sort-the-matrix-diagonally/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "sort-the-matrix-diagonally" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/sort-the-matrix-diagonally/src/main.rs b/sort-the-matrix-diagonally/src/main.rs new file mode 100644 index 0000000..fa8d7e8 --- /dev/null +++ b/sort-the-matrix-diagonally/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + println!("Hello, world!"); +} + +struct Solution; + +use std::collections::HashMap; + +impl Solution { + pub fn diagonal_sort(mut mat: Vec<Vec<i32>>) -> Vec<Vec<i32>> { + let mut a = HashMap::new(); + + for (i, row) in mat.iter().enumerate() { + for (ii, value) in row.iter().enumerate() { + a.entry(i-ii).and_modify(|x: &mut Vec<&i32>| {x.push(value)}).or_insert(vec![value]); + } + } + + for i in a.values_mut() { + i.sort(); + println!("{i:?}"); + } + + mat + } +} |