-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1380. Lucky Numbers in a Matrix
More file actions
58 lines (32 loc) · 1.03 KB
/
1380. Lucky Numbers in a Matrix
File metadata and controls
58 lines (32 loc) · 1.03 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public List<Integer> luckyNumbers (int[][] matrix)
{
for (int[] row : matrix)
{
int minIndex = getMinIndex(row); //finding the col of the minimum element of thid row
if(row[minIndex] == maxNumOfColumn(matrix, minIndex)) // checking if this min is also max in its col
{
return List.of(row[minIndex]);
}
}
return new ArrayList<>();
}
public int getMinIndex(int[] row) // findimg our min col
{
int minIndex = 0;
for(int j = 0; j < row.length; j++)
{
if (row[j] < row[minIndex]) minIndex = j;
}
return minIndex;
}
public int maxNumOfColumn(int[][] matrix, int j) // finding if max in column
{
int result = 0;
for(int i = 0; i < matrix.length; i++)
{
result = Math.max(result, matrix[i][j]);
}
return result;
}
}