Improve based on the given feedback
pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
- let (width, height) = match (input.len(), input[0].len()) {
- (0, _) | (_, 0) => return Vec::new(),
- (n, k) => (n, k),
+ let height = if !input.is_empty() && !input[0].is_empty() {
+ input[0].len()
+ } else {
+ return Vec::new();
};
let mut saddle_points = Vec::new();
- for i in 0..width {
- let row_max = input[i].iter().max().unwrap();
+ let col_mins: Vec<u64> = (0..height)
+ .filter_map(|j| input.iter().map(|row| row[j]).min())
+ .collect();
- for j in 0..height {
- let column_min = input.iter().map(|x| x[j]).min().unwrap();
-
- let value = input[i][j];
+ for (i, row) in input.iter().enumerate() {
+ let row_max = *row.iter().max().unwrap();
- if value == *row_max && value == column_min {
+ for j in 0..height {
+ if row[j] == row_max && row[j] == col_mins[j] {
saddle_points.push((i, j));
}
}
