Optimize saddle-points; use idiomatic rust
pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
- let mut saddle_points = Vec::new();
+ let (width, height) = match (input.len(), input[0].len()) {
+ (0, _) | (_, 0) => return Vec::new(),
+ (n, k) => (n, k),
+ };
- let width = input.len();
- let height = input[0].len();
+ let mut saddle_points = Vec::new();
for i in 0..width {
- for j in 0..height {
- let column = input.iter().map(|x| x[j]).collect::<Vec<u64>>();
- let row = &input[i];
+ let row_max = input[i].iter().max().unwrap();
- let max = row.iter().max().unwrap();
- let min = column.iter().min().unwrap();
+ for j in 0..height {
+ let column_min = input.iter().map(|x| x[j]).min().unwrap();
let value = input[i][j];
- if value >= *max && value <= *min {
+ if value == *row_max && value == column_min {
saddle_points.push((i, j));
}
}
}
+
saddle_points
}
