From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- sort-the-matrix-diagonally/src/main.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 sort-the-matrix-diagonally/src/main.rs (limited to 'sort-the-matrix-diagonally/src') 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> { + 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 + } +} -- cgit v1.2.3