-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLuckyNumbersInAMatrix.java
More file actions
32 lines (27 loc) · 986 Bytes
/
LuckyNumbersInAMatrix.java
File metadata and controls
32 lines (27 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.dbc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LuckyNumbersInAMatrix {
public List<Integer> luckyNumbers(int[][] matrix) {
List<Integer> res = new ArrayList<>();
int[] minRow = new int[matrix.length];
Arrays.fill(minRow, Integer.MAX_VALUE);
int[] maxCol = new int[matrix[0].length];
Arrays.fill(maxCol, Integer.MIN_VALUE);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
minRow[i] = Math.min(minRow[i], matrix[i][j]);
maxCol[j] = Math.max(maxCol[j], matrix[i][j]);
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] <= minRow[i] && matrix[i][j] >= maxCol[j]) {
res.add(matrix[i][j]);
}
}
}
return res;
}
}