1380. 矩阵中的幸运数(javascript) Lucky Numbers in a Matrix

给你一个 m * n 的矩阵,矩阵中的数字 各不相同 。请你按 任意 顺序返回矩阵中的所有幸运数。

幸运数 是指矩阵中满足同时下列两个条件的元素:

在同一行的所有元素中最小
在同一列的所有元素中最大

Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order.

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

示例 1:

输入:matrix = [[3,7,8],[9,11,13],[15,16,17]]
输出:[15]
解释:15 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。

示例 2:

输入:matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
输出:[12]
解释:12 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。

示例 3:

输入:matrix = [[7,8],[1,2]]
输出:[7]
解释:7是唯一的幸运数字,因为它是行中的最小值,列中的最大值。

根据题意
找出每行的最小值,组成数组
找出每列的最大值,组成数组
返回两个数组的交集

var luckyNumbers = function (matrix) {
    let m = matrix.length
    let n = matrix[0].length
    let minList = []
    let maxList = []
    for (let i = 0; i < m; i++) {
        minList.push(Math.min.apply(Math, matrix[i]))
    }
    for (let j = 0; j < n; j++) {
        let list = []
        for (let i = 0; i < m; i++) {
            list.push(matrix[i][j])
        }
        maxList.push(Math.max.apply(Math, list))
    }
    let result = maxList.filter((item) => {
        return minList.indexOf(item) > -1
    })
    return result
};

leetcode:https://leetcode.cn/problems/lucky-numbers-in-a-matrix/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值