First Completely Painted Row or Column - Counting [JS]
Solution: Counting
var firstCompleteIndex = function(arr, mat) {
let m = mat.length, n = mat[0].length;
let coordinates = Array(m * n + 1);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
coordinates[mat[i][j]] = [i, j];
}
}
let rowCount = Array(m).fill(n), colCount = Array(n).fill(m);
for (let i = 0; i < m * n; i++) {
let [row, col] = coordinates[arr[i]];
if (--rowCount[row] === 0 || --colCount[col] === 0) return i;
}
};
Comments
Post a Comment