首页 > 其他分享 >[LeetCode] 1380. Lucky Numbers in a Matrix

[LeetCode] 1380. Lucky Numbers in a Matrix

时间:2024-07-19 13:19:04浏览次数:9  
标签:rows matrix int cols lucky LeetCode 1380 its Matrix

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.

Example 1:
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column.

Example 2:
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.

Example 3:
Input: matrix = [[7,8],[1,2]]
Output: [7]
Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column.

Constraints:
m == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 105.
All elements in the matrix are distinct.

矩阵中的幸运数。

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

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

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

思路

这是一道二维矩阵的基础题,正常遍历这个二维数组。然后我们需要用两个一维数组分别记录每一行的最大值和每一列的最大值。接着我们再次遍历每一行的最大值和每一列的最大值,如果某一行的最大值恰巧是某一列的最小值,则把这个值加入结果集。

复杂度

时间O(mn)
空间O(m + n)

代码

Java实现

class Solution {
    public List<Integer> luckyNumbers (int[][] matrix) {
        // corner case
        if (matrix == null || matrix.length == 0) {
            return new ArrayList<>();
        }

        // normal case
        int m = matrix.length;
        int n = matrix[0].length;
        int[] rows = new int[m];
        Arrays.fill(rows, Integer.MAX_VALUE);
        int[] cols = new int[n];
        Arrays.fill(cols, Integer.MIN_VALUE);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int val = matrix[i][j];
                rows[i] = Math.min(rows[i], val);
                cols[j] = Math.max(cols[j], val);
            }
        }

        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int cur = matrix[i][j];
                if (rows[i] == cur && cols[j] == cur) {
                    res.add(cur);
                }
            }
        }
        return res;
    }
}

标签:rows,matrix,int,cols,lucky,LeetCode,1380,its,Matrix
From: https://www.cnblogs.com/cnoodle/p/18311288

相关文章

  • 算法篇 滑动窗口 leetCode 水果成篮
    水果成蓝1.题目描述2.图形分析2.1原理解释2.2怎么想出使用滑动窗口2.3图形分析3.代码演示1.题目描述2.图形分析2.1原理解释2.2怎么想出使用滑动窗口2.3图形分析3.代码演示......
  • leetcode 每日1题
    3112.访问消失节点的最少时间fromheapqimportheappop,heappushfromtypingimportListclassSolution:defminimumTime(self,n:int,edges:List[List[int]],disappear:List[int])->List[int]:#创建邻接表adj=[[]for_inrange(n)]......
  • 代码随想录算法训练营第42期 第二天 | LeetCode977. 有序数组的平方、209. 长度最小的
    一、977.有序数组的平方学习链接:有序数组的平方状态:暴力解法与双指针都做出来了时间复杂度:暴力解法O()    双指针解法 O()细节之处:暴力解法1       双指针解法1  暴力解法classSolution{publicint[]sortedSquares(int[]nums){......
  • 代码随想录算法训练营第16天|LeetCode112路径总和LeetCode113路径总和iiLeetCode106.
    代码随想录算法训练营Day16代码随想录算法训练营第16天|LeetCode112路径总和LeetCode113路径总和iiLeetCode106.从中序与后序遍历序列构造二叉树LeetCode105.从前序与中序遍历序列构造二叉树目录代码随想录算法训练营前言LeetCode112路径总和,LeetCode113路径......
  • 代码随想录算法训练营第 15 天 |LeetCode110平衡二叉树 LeetCode257二叉树的所有路径
    代码随想录算法训练营Day15代码随想录算法训练营第15天|LeetCode110平衡二叉树LeetCode257二叉树的所有路径LeetCode404左叶子之和LeetCode222完全二叉树节点之和目录代码随想录算法训练营前言LeetCode110平衡二叉树LeetCode257二叉树的所有路径LeetCode404左......
  • leetcode 1459 矩形面积(postgresql)
    需求表:Points±--------------±--------+|ColumnName|Type|±--------------±--------+|id|int||x_value|int||y_value|int|±--------------±--------+id是该表主键每个点都用二维坐标(x_value,y_value)表示写一个SQL语句,报告由表......
  • (nice!!!)LeetCode 3112. 访问消失节点的最少时间(图论、边的dijkstra、堆优化)
    3112.访问消失节点的最少时间思路:节点n的个数非常大,用普通的dijkstra算法对节点进行枚举是会超时的,时间复杂度为0(n^2)。这里边的数量最大为10^5,可以对边使用dijkstra算法+堆优化操作,时间复杂度为0(mlogm)。节点消失问题,只需要加一个判断条件,判断到每个节点的最小时......
  • [leetcode] 字符串 重复的子字符串
    题目:给定一个非空的字符串s,检查是否可以通过由它的一个子串重复多次构成。代码:思路1(暴力算法):省略思路2(移动匹配):两个重复的字符串,肯定能组成一个新的s代码boolrepeatedSubstringPattern(strings){strings1=s+s;s1.erase(s1.begin());......
  • 【LeetCode 0051】【剪枝】N皇后
    N-QueensThen-queenspuzzleistheproblemofplacingnqueensonannxnchessboardsuchthatnotwoqueensattackeachother.Givenanintegern,returnalldistinctsolutionstothen-queenspuzzle.Youmayreturntheanswerinanyorder.Eachsolu......
  • leetcode_189. 轮转数组
    leetcode_189.轮转数组题目描述:给定一个整数数组nums,将数组中的元素向右轮转k个位置,其中k是非负数。示例1:输入:nums=[1,2,3,4,5,6,7],k=3输出:[5,6,7,1,2,3,4]解释:向右轮转1步:[7,1,2,3,4,5,6]向右轮转2步:[6,7,1,2,3,4,5]向右轮转3步:[......