当前位置:网站首页>LeetCode 1380. Lucky number in matrix
LeetCode 1380. Lucky number in matrix
2022-07-01 03:52:00 【Daylight629】
1380. The lucky number in the matrix
To give you one m * n Matrix , The number in the matrix Each are not identical . Please press arbitrarily Return all the lucky numbers in the matrix in order .
Lucky number refers to the elements in the matrix that meet the following two conditions at the same time :
- The smallest of all elements in the same row
- The largest of all elements in the same column
Example 1:
Input :matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output :[15]
explain :15 Is the only lucky number , Because it is the smallest value in its row , It is also the maximum value in the column .
Example 2:
Input :matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output :[12]
explain :12 Is the only lucky number , Because it is the smallest value in its row , It is also the maximum value in the column .
Example 3:
Input :matrix = [[7,8],[1,2]]
Output :[7]
Tips :
m == mat.lengthn == mat[i].length1 <= n, m <= 501 <= matrix[i][j] <= 10^5- All elements in the matrix are different
Two 、 Method 1
simulation
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
List<Integer> res = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
boolean isMin = true;
boolean isMax = true;
for (int k = 0; k < n; k++) {
if (matrix[i][k] < matrix[i][j]) {
isMin = false;
break;
}
}
if (!isMin) {
continue;
}
for (int k = 0; k < m; k++) {
if (matrix[k][j] > matrix[i][j]) {
isMax = false;
break;
}
}
if (isMax) {
res.add(matrix[i][j]);
}
}
}
return res;
}
}
Complexity analysis
Time complexity ::O(mn×(m+n)), among m and n These are matrices matrix The number of rows and columns . ergodic matrix matrix need O(mn), lookup 行 Minimum required O(n), Finding the maximum value of a column requires O(m).
Spatial complexity :O(1). The return value does not calculate the space complexity .
3、 ... and 、 Method 2
Storage
class Solution {
public List<Integer> luckyNumbers (int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[] minRow = new int[m];
int[] maxCol = new int[n];
Arrays.fill(minRow, Integer.MAX_VALUE);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
minRow[i] = Math.min(minRow[i], matrix[i][j]);
maxCol[j] = Math.max(maxCol[j], matrix[i][j]);
}
}
List<Integer> res = new ArrayList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (maxCol[j] == matrix[i][j] && minRow[i] == matrix[i][j]) {
res.add(matrix[i][j]);
}
}
}
return res;
}
}
Complexity analysis
Time complexity :O(mn), among m and n These are matrices matrix The number of rows and columns . Preprocessing minRow and maxCol need O(mn), Finding lucky numbers requires O(mn).
Spatial complexity :O(m+n). preservation minRow and maxCol need O(m+n) Extra space , The return value is not included in the space complexity .
边栏推荐
- 214. minimum palindrome string
- [party benefits] jsonobject to string, leave blank
- [TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
- Millet College wechat scanning code login process record and bug resolution
- 这可能是你进腾讯最后的机会了..
- 线程常用方法与守护线程
- 访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
- How keil displays Chinese annotations (simple with pictures)
- Blueprism registration, download and install -rpa Chapter 1
- 206.反转链表
猜你喜欢

idea插件备份表

SEM of C language_ Tvariable type

Idea plug-in backup table

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

How keil displays Chinese annotations (simple with pictures)

Visit the image URL stored by Alibaba cloud to preview the thumbnail directly on the web page instead of downloading it directly

Addition without addition, subtraction, multiplication and division

静态库使用MFC和共享库使用MFC的区别

【TA-霜狼_may-《百人计划》】1.2.1 向量基础
![[TA frost wolf \u may- hundred people plan] 1.2.1 vector basis](/img/94/99090ea91082a385968e071ef3766c.png)
[TA frost wolf \u may- hundred people plan] 1.2.1 vector basis
随机推荐
【TA-霜狼_may-《百人计划》】1.1 渲染流水线
AfxMessageBox和MessageBox的用法
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture
171. excel table column No
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
JMeter学习笔记2-图形界面简单介绍
72. 编辑距离
187. 重复的DNA序列
【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算
318. 最大单词长度乘积
Web components series (VIII) -- custom component style settings
Binary tree god level traversal: Morris traversal
bootsrap中的栅格系统
静态库使用MFC和共享库使用MFC的区别
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
程序员女友给我做了一个疲劳驾驶检测
PageObject模式解析及案例
6. Z 字形变换
Appium自动化测试基础 — APPium基本原理
The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context