- Let's use set comprehensions to compute the minimum values in each row (
row_mins
) and the maximum values in each column (column_maxes
) - Then we can just return
list(row_mins.intersect(column_maxes))
- Since every element in the matrix is distinct (per the problem definition), we don't need to deal with the same value potentially appearing in different rows/columns (otherwise we'd also need to track the positions)
classSolution: defluckyNumbers (self, matrix: List[List[int]]) ->List[int]: # compute the minimum for each rowrow_mins= {min(r) forrinmatrix} # compute the maximum for each columnn=len(matrix[0]) column_maxes= {max([r[i] forrinmatrix]) foriinrange(n)} # return a list of the set intersection between the mins and maxesreturnlist(row_mins.intersection(column_maxes))
- Given test cases pass
- I don't think there are other special cases we need to account for that this solution won't cover; submitting...
Solved!