Editor

Saddle Points

#288 · Grids and matrices · Intermediate · 65 XP · about 12 min

A height map is a list of rows of integers. A cell is a saddle point when it is at least as high as every other cell in its row and at most as high as every other cell in its column: like the middle of a horse's saddle, which is a high point going one way and a low point going the other.

Write saddle_points(grid) that returns the positions of all saddle points as a list of (row, col) tuples, sorted by row and then by column. Return an empty list when there are none.

Examples

saddle_points([[9, 8, 7],
               [5, 3, 2],
               [6, 6, 7]])   # returns [(1, 0)]
saddle_points([[1, 2],
               [3, 4]])      # returns [(0, 1)]
saddle_points([[4, 5],
               [5, 4]])      # returns []

In the first map, the 5 is the highest value in row 1 and the lowest in column 0 (which holds 9, 5 and 6).

Constraints

  • 0 to 200 rows, all of the same length (1 to 200); an empty map [] gives [].

Note

Equal values count as both highest and lowest, so a map can have several saddle points: in [[5, 5], [5, 5]] every cell is one.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.