Tuesday, July 23, 2024
Find matrix elements that are simultaneously the minimum in their row and maximum in their column.
func luckyNumbers (matrix [][]int) []int {
rows := len(matrix)
columns := len(matrix[0])
rowMinNumbers := make([]int, rows)
columnsMaxNumbers := make([]int, columns)
// init rowMinNumbers with the first element of each row
for i := range rowMinNumbers {
rowMinNumbers[i] = matrix[i][0]
}
// init columnsMaxNumbers with the first element of each column
columnsMaxNumbers = matrix[0]
// dual sort to minimum in each row and maximum in each column
for i := 0; i < rows; i++ {
for j := 0; j < columns; j++ {
if matrix[i][j] < rowMinNumbers[i] {
rowMinNumbers[i] = matrix[i][j]
}
if matrix[i][j] > columnsMaxNumbers[j] {
columnsMaxNumbers[j] = matrix[i][j]
}
}
}
luckyNunmbers := []int{}
for i := 0; i < rows; i++ {
for j := 0; j < columns; j++ {
// check if it's a min in the row and max in the column
if matrix[i][j] == rowMinNumbers[i] && matrix[i][j] == columnsMaxNumbers[j] {
luckyNunmbers = append(luckyNunmbers, matrix[i][j])
}
}
}
return luckyNunmbers
}