Skip to content

Commit d13551c

Browse files
add a solution for 1380
1 parent 570a5b4 commit d13551c

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

Diff for: src/main/java/com/fishercoder/solutions/secondthousand/_1380.java

+32
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,36 @@ private boolean luckyInRow(int number, int[] row) {
3737
return true;
3838
}
3939
}
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+
}
4072
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package com.fishercoder.secondthousand;
22

33
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;
66

77
import java.util.Arrays;
88

9-
import static org.junit.Assert.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
1011

1112
public class _1380Test {
1213
private static _1380.Solution1 solution1;
14+
private static _1380.Solution2 solution2;
1315
private static int[][] matrix;
1416

15-
@BeforeClass
16-
public static void setup() {
17+
@BeforeEach
18+
public void setup() {
1719
solution1 = new _1380.Solution1();
20+
solution2 = new _1380.Solution2();
1821
}
1922

2023
@Test
@@ -25,6 +28,7 @@ public void test1() {
2528
{15, 16, 17}
2629
};
2730
assertEquals(Arrays.asList(15), solution1.luckyNumbers(matrix));
31+
assertEquals(Arrays.asList(15), solution2.luckyNumbers(matrix));
2832
}
2933

3034
}

0 commit comments

Comments
 (0)