Leetcode Note: Go - Lucky Numbers in a Matrix

Lucky Numbers in a Matrix - LeetCode
https://leetcode.com/problems/lucky-numbers-in-a-matrix/

  • Go 言語で取り組んだメモ

回答

Go O(NM) sOluTIoN - Lucky Numbers in a Matrix - LeetCode
https://leetcode.com/problems/lucky-numbers-in-a-matrix/solutions/726440/go-o-nm-solution/

func luckyNumbers (matrix [][]int) []int {
    // Find min of each row.
    mins := make(map[int]struct{})
    for i := 0; i < len(matrix); i++ {
        min := matrix[i][0]
        for j := 1; j < len(matrix[0]); j++ {
            if min > matrix[i][j] {
                min = matrix[i][j]
            }
        }
        mins[min] = struct{}{}
    }
    
    // Find max of each column.
    maxs := make(map[int]struct{})
    for j := 0; j < len(matrix[0]); j++ {
        max := matrix[0][j]
        for i := 0; i < len(matrix); i++ {
            if max < matrix[i][j] {
                max = matrix[i][j]
            }
        }
        maxs[max] = struct{}{}
    }
    
    // Find the intersection of two maps.
    res := []int{}
    for key := range mins {
        if _, ok := maxs[key]; ok {
            res = append(res, key)
        }
    }
    return res
}