File tree 2 files changed +41
-5
lines changed
main/java/com/fishercoder/solutions/secondthousand
test/java/com/fishercoder/secondthousand
2 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -37,4 +37,36 @@ private boolean luckyInRow(int number, int[] row) {
37
37
return true ;
38
38
}
39
39
}
40
+
41
+ public static class Solution2 {
42
+ public List <Integer > luckyNumbers (int [][] matrix ) {
43
+ List <Integer > rowMins = new ArrayList <>();
44
+ for (int i = 0 ; i < matrix .length ; i ++) {
45
+ int j = 0 ;
46
+ int rowMin = matrix [i ][j ++];
47
+ for (; j < matrix [0 ].length ; j ++) {
48
+ rowMin = Math .min (rowMin , matrix [i ][j ]);
49
+ }
50
+ rowMins .add (rowMin );
51
+ }
52
+ List <Integer > colMaxs = new ArrayList <>();
53
+ for (int j = 0 ; j < matrix [0 ].length ; j ++) {
54
+ int i = 0 ;
55
+ int colMax = matrix [i ++][j ];
56
+ for (; i < matrix .length ; i ++) {
57
+ colMax = Math .max (colMax , matrix [i ][j ]);
58
+ }
59
+ colMaxs .add (colMax );
60
+ }
61
+ List <Integer > result = new ArrayList <>();
62
+ for (int i = 0 ; i < matrix .length ; i ++) {
63
+ for (int j = 0 ; j < matrix [0 ].length ; j ++) {
64
+ if (matrix [i ][j ] == rowMins .get (i ) && matrix [i ][j ] == colMaxs .get (j )) {
65
+ result .add (matrix [i ][j ]);
66
+ }
67
+ }
68
+ }
69
+ return result ;
70
+ }
71
+ }
40
72
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .secondthousand ;
2
2
3
3
import com .fishercoder .solutions .secondthousand ._1380 ;
4
- import org .junit .BeforeClass ;
5
- import org .junit .Test ;
4
+ import org .junit .jupiter . api . BeforeEach ;
5
+ import org .junit .jupiter . api . Test ;
6
6
7
7
import java .util .Arrays ;
8
8
9
- import static org .junit .Assert .assertEquals ;
9
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
10
+
10
11
11
12
public class _1380Test {
12
13
private static _1380 .Solution1 solution1 ;
14
+ private static _1380 .Solution2 solution2 ;
13
15
private static int [][] matrix ;
14
16
15
- @ BeforeClass
16
- public static void setup () {
17
+ @ BeforeEach
18
+ public void setup () {
17
19
solution1 = new _1380 .Solution1 ();
20
+ solution2 = new _1380 .Solution2 ();
18
21
}
19
22
20
23
@ Test
@@ -25,6 +28,7 @@ public void test1() {
25
28
{15 , 16 , 17 }
26
29
};
27
30
assertEquals (Arrays .asList (15 ), solution1 .luckyNumbers (matrix ));
31
+ assertEquals (Arrays .asList (15 ), solution2 .luckyNumbers (matrix ));
28
32
}
29
33
30
34
}
You can’t perform that action at this time.
0 commit comments