Skip to content

Commit 734d24d

Browse files
committed
Iterate through r and c, calculate the distance and use that as comprator to sort the elements, return by removing distance from final array
1 parent d547296 commit 734d24d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

1030/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn all_cells_dist_order(r: i32, c: i32, r0: i32, c0: i32) -> Vec<Vec<i32>> {
3+
let mut return_value : Vec<Vec<i32>> = Vec::new();
4+
for item in 0..r {
5+
for item_second in 0..c{
6+
let dist = (r0-item).abs() + (c0-item_second).abs();
7+
return_value.push(vec![item,item_second,dist]);
8+
}
9+
}
10+
return_value.sort_by(|a,b| a[2].cmp(&b[2]));
11+
return_value.into_iter().map(|mut x| {
12+
x.pop();
13+
x
14+
}).collect()
15+
16+
}
17+
}

0 commit comments

Comments
 (0)