Skip to content

Commit 4b8aaf7

Browse files
authored
Improved task 2661
1 parent c05b1bc commit 4b8aaf7

File tree

1 file changed

+14
-23
lines changed
  • src/main/java/g2601_2700/s2661_first_completely_painted_row_or_column

1 file changed

+14
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
11
package g2601_2700.s2661_first_completely_painted_row_or_column;
22

3-
// #Medium #Array #Hash_Table #Matrix #2023_09_09_Time_22_ms_(85.61%)_Space_64.7_MB_(58.25%)
4-
5-
import java.util.HashMap;
3+
// #Medium #Array #Hash_Table #Matrix #2025_02_25_Time_2_ms_(100.00%)_Space_57.98_MB_(96.93%)
64

75
public class Solution {
86
public int firstCompleteIndex(int[] arr, int[][] mat) {
9-
HashMap<Integer, Integer> map = new HashMap<>();
7+
int[] numMapIndex = new int[mat.length * mat[0].length + 1];
108
for (int i = 0; i < arr.length; i++) {
11-
map.put(arr[i], i);
12-
}
13-
14-
for (int i = 0; i < mat.length; i++) {
15-
for (int j = 0; j < mat[0].length; j++) {
16-
mat[i][j] = map.get(mat[i][j]);
17-
}
9+
numMapIndex[arr[i]] = i;
1810
}
19-
2011
int ans = Integer.MAX_VALUE;
21-
for (int[] ints : mat) {
22-
int max = 0;
23-
for (int j = 0; j < mat[0].length; j++) {
24-
max = Math.max(max, ints[j]);
12+
for (int i = 0; i < mat.length; i++) {
13+
int rowMin = Integer.MIN_VALUE;
14+
for (int i1 = 0; i1 < mat[i].length; i1++) {
15+
int index = numMapIndex[mat[i][i1]];
16+
rowMin = Math.max(rowMin, index);
2517
}
26-
ans = Math.min(ans, max);
18+
ans = Math.min(ans, rowMin);
2719
}
28-
2920
for (int i = 0; i < mat[0].length; i++) {
30-
int max = 0;
31-
for (int[] ints : mat) {
32-
max = Math.max(max, ints[i]);
21+
int colMin = Integer.MIN_VALUE;
22+
for (int i1 = 0; i1 < mat.length; i1++) {
23+
int index = numMapIndex[mat[i1][i]];
24+
colMin = Math.max(colMin, index);
3325
}
34-
ans = Math.min(ans, max);
26+
ans = Math.min(ans, colMin);
3527
}
36-
3728
return ans;
3829
}
3930
}

0 commit comments

Comments
 (0)