Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d9b03a2

Browse files
committed
Add solution #2661
1 parent 108a61f commit d9b03a2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 2661. First Completely Painted Row or Column
3+
* https://leetcode.com/problems/first-completely-painted-row-or-column/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array arr, and an m x n integer matrix mat. arr and mat
7+
* both contain all the integers in the range [1, m * n].
8+
*
9+
* Go through each index i in arr starting from index 0 and paint the cell in mat containing
10+
* the integer arr[i].
11+
*
12+
* Return the smallest index i at which either a row or a column will be completely painted
13+
* in mat.
14+
*/
15+
16+
/**
17+
* @param {number[]} arr
18+
* @param {number[][]} mat
19+
* @return {number}
20+
*/
21+
var firstCompleteIndex = function(arr, mat) {
22+
const rows = new Array(mat.length).fill(0);
23+
const columns = new Array(mat[0].length).fill(0);
24+
const map = new Map();
25+
26+
for (let i = 0; i < mat.length; i++) {
27+
for (let j = 0; j < mat[i].length; j++) {
28+
map.set(mat[i][j], [i, j]);
29+
}
30+
}
31+
32+
for (let i = 0; i < arr.length; i++) {
33+
const [rowIndex, columnIndex] = map.get(arr[i]);
34+
if (++rows[rowIndex] === mat[0].length || ++columns[columnIndex] === mat.length) {
35+
return i;
36+
}
37+
}
38+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@
457457
2649|[Nested Array Generator](./2649-nested-array-generator.js)|Medium|
458458
2650|[Design Cancellable Function](./2650-design-cancellable-function.js)|Hard|
459459
2657|[Find the Prefix Common Array of Two Arrays](./2657-find-the-prefix-common-array-of-two-arrays.js)|Medium|
460+
2661|[First Completely Painted Row or Column](./2661-first-completely-painted-row-or-column.js)|Medium|
460461
2665|[Counter II](./2665-counter-ii.js)|Easy|
461462
2666|[Allow One Function Call](./2666-allow-one-function-call.js)|Easy|
462463
2667|[Create Hello World Function](./2667-create-hello-world-function.js)|Easy|

0 commit comments

Comments
 (0)