|
1 | 1 | package g2601_2700.s2661_first_completely_painted_row_or_column; |
2 | 2 |
|
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%) |
6 | 4 |
|
7 | 5 | public class Solution { |
8 | 6 | 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]; |
10 | 8 | 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; |
18 | 10 | } |
19 | | - |
20 | 11 | 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); |
25 | 17 | } |
26 | | - ans = Math.min(ans, max); |
| 18 | + ans = Math.min(ans, rowMin); |
27 | 19 | } |
28 | | - |
29 | 20 | 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); |
33 | 25 | } |
34 | | - ans = Math.min(ans, max); |
| 26 | + ans = Math.min(ans, colMin); |
35 | 27 | } |
36 | | - |
37 | 28 | return ans; |
38 | 29 | } |
39 | 30 | } |
0 commit comments