-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuckyNumber.java
64 lines (56 loc) · 1.48 KB
/
LuckyNumber.java
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
59
60
61
62
63
64
public class LuckyNumber {
public static void main(String[] args) {
// int[][] array = {
// {1, 10, 4, 2},
// {9, 3, 8, 7},
// {15, 16, 17, 12}
// };
// int[][] array = {
// {3, 7, 8},
// {9, 11, 13},
// {15, 16, 17}
// };
// int[][] array = {
// {7, 8},
// {1, 2}
// };
int[][] array = {
{3, 6},
{7, 1},
{5, 2},
{4, 8}
};
System.out.println(LuckyNumber.luckyNumber(array));
}
static int luckyNumber(int[][] nums) {
int min = 0;
int row = 0;
int col = 0;
for (int i = 0; i < nums.length; i++) {
min = nums[i][0];
row = i;
col = 0;
for (int j = 0; j < nums[i].length; j++) {
if (nums[i][j] < min) {
min = nums[i][j];
col = j;
}
}
if(!checkGreaterInCol(nums,row, col)){
continue;
}else{
return nums[row][col];
}
}
return -1;
}
static boolean checkGreaterInCol(int[][] nums,int row, int col){
int max = nums[row][col];
for (int i = 0; i < nums.length; i++) {
if(nums[i][col] > max){
return false;
}
}
return true;
}
}