当前位置:网站首页>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 .
边栏推荐
- The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
- 30. Concatenate substrings of all words
- The programmer's girlfriend gave me a fatigue driving test
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- Millet College wechat scanning code login process record and bug resolution
- 187. 重复的DNA序列
- [ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle
- JMeter学习笔记2-图形界面简单介绍
- Error: plug ins declaring extensions or extension points must set the singleton directive to true
- 谷粒学院微信扫码登录过程记录以及bug解决
猜你喜欢

Pytorch training deep learning network settings CUDA specified GPU visible

Binary tree god level traversal: Morris traversal

The combination of applet container technology and IOT

Blueprism registration, download and install -rpa Chapter 1

熊市下的Coinbase:亏损、裁员、股价暴跌
![[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

Valentine's Day is nothing.

206.反转链表

Future of NTF and trends in 2022

盘点华为云GaussDB(for Redis)六大秒级能力
随机推荐
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
Future of NTF and trends in 2022
“目标检测“+“视觉理解“实现对输入图像的理解
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
【TA-霜狼_may-《百人计划》】1.2.1 向量基础
389. 找不同
Analysis and case of pageobject mode
241. 为运算表达式设计优先级
AfxMessageBox和MessageBox的用法
Addition without addition, subtraction, multiplication and division
187. repeated DNA sequences
You cannot right-click F12 to view the source code solution on the web page
Unexpected token o in JSON at position 1, JSON parsing problem
MFC窗口滚动条用法
Idea plug-in backup table
盘点华为云GaussDB(for Redis)六大秒级能力
166. fractions to decimals
6. zigzag transformation
[EI search] important information conference of the 6th International Conference on materials engineering and advanced manufacturing technology (meamt 2022) in 2022 website: www.meamt Org meeting time
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)